sphinx 增量索引 实现近实时

来源:互联网 发布:手机wifi控制器软件 编辑:程序博客网 时间:2024/05/19 17:08

当数据总数太大而不能从头开始时经常出现频繁的情况,但是新记录的数量相当小。示例:拥有1,000,000个存档帖子的论坛,但每天只有1,000个新帖。
在这种情况下,可以使用所谓的“主+ delta”方案实现“实时”(几乎实时)索引更新。

简单的实现 “主索引+增量索引”方法
在数据库中增加一个计数表,记录每次重新构建主索引时,被索引表的最后一个数据id,这样在增量索引时只需要索引这个id以后的数据即可,每次重新构建主索引时都更新这个表。

1.先在mysql中插入一个计数表和两个索引表
CREATE TABLE f12_counter(    id INTEGER PRIMARY KEY NOT NULL,    productid INTEGER NOT NULL);
2. sphinx 配置
source search
{
    type = mysql
    sql_host = localhost
    sql_user = root
    sql_pass =
    sql_db   = db_hs12fr
    sql_port = 3306
    sql_query_pre = SET NAMES utf8
    sql_query_pre = SET SESSION query_cache_type=OFF
    sql_query_pre = REPLACE INTO f12_counter SELECT 1, MAX(productid) FROM f12_product_search  #数据源查询前的操作
    sql_query = SELECT productid,categoryid,sortvalue,attribute,shop_price FROM f12_product_search where productid<=(SELECT productid FROM f12_counter WHERE id=1)
    sql_attr_uint  = sortvalue
    sql_attr_uint  = categoryid
    sql_attr_float  = shop_price
}

source delta_search:search
{
    type = mysql
    sql_host = localhost
    sql_user = root
    sql_pass =
    sql_db   = db_hs12fr
    sql_port = 3306
    sql_query_pre = SET NAMES utf8
    sql_query_pre = SET SESSION query_cache_type=OFF
    sql_query = SELECT productid,categoryid,sortvalue,attribute,shop_price FROM f12_product_search WHERE productid>( SELECT productid FROM f12_counter WHERE id=1 )
    sql_attr_uint  = sortvalue
    sql_attr_uint  = categoryid
    sql_attr_float  = shop_price
}
index search
{ #主索引
    source       = search
    path         = /usr/local/sphinx2.2/var/search
    docinfo      = extern
    mlock        = 0
    morphology   = none
    min_word_len = 1
    charset_type = sbcs
    html_strip   = 0
}


index delta_search:search
{ #增量索引
    source       = delta_search
    path         = /usr/local/sphinx2.2/var/delta_search
    docinfo      = extern
    mlock        = 0
    morphology   = none
    min_word_len = 1
    charset_type = sbcs
    html_strip   = 0
}

启动 sphinx 
/usr/local/sphinx/bin/indexer --all --config /usr/local/sphinx/etc/sphinx.conf
/usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf

索引自动更新
可做定时脚本执行 一主索引更新脚本 一增量索引更新脚本


增量索引更新
/usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf delta_search --rotate
/usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf --stop
/usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf --merge search delta_search  (索引合并)

主索引更新
/usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf --stop
/usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf search

这里上将一些新添加数据写进增量索引中,通过增量索引将数据与主索引数据合并,这样我们就不必去频繁更新有关大量数据的主索引。在 PHP API 中的 Query(keyword,index) ,这第二索引名称
是设置多个索引名,也就没有必要一定将两个索引合并

原创粉丝点击