sphinx增量索引使用

     sphinx在使用过程中如果表的数据量很大,新增加的内容在sphinx索引没有重建之前都是搜索不到的。

这时可以通过建立sphinx增量索引,通过定时更新增量索引,合并主索引的方式,来实现伪实时更新。(使用定时任务,定时更新增量索引,例如10分钟一次)

在利用 Sphinx 做搜索引擎的时候,一般他的索引建立构成有如下几个部分:

  1. 固定不变的主索引
  2. 增量索引重建
  3. 索引数据合并



1、创建增量索引记录表   (记录每次增量索引创建时最大的id,下次从此id往后继续创建增量索引)

create table sphinx_counter(
  counter_id int primary key not null ,
  max_doc_id int not null )engine myisam charset utf8;


2、索引文件设置

# in MySQL
CREATE TABLE sph_counter
(
counter_id INTEGER PRIMARY KEY NOT NULL,
max_doc_id INTEGER NOT NULL
);
# in sphinx.conf
source main
{
# ...
sql_query_pre = REPLACE INTO sph_counter SELECT 1, MAX(id) FROM documents
sql_query = SELECT id, title, body FROM documents 
WHERE id<=( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 )
}
source delta : main
{
sql_query_pre =
sql_query = SELECT id, title, body FROM documents 
WHERE id>( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 )
}
index main
{
source = main
path = /path/to/main
# ... all the other settings
}
# note how all other settings are copied from main,
# but source and path are overridden (they MUST be)
index delta : main
{
source = delta
path = /path/to/delta
}


3、创建更新所有索引
c:wampappssphinxin>indexer -c c:/wamp/apps/sphinx/etc/sphinx.conf --all --rotate
如果配置正确的话,现在辅助表sph_counter中已经添加了一条数据

4、更新增量索引

c:wampappssphinxin>indexer delta -c c:/wamp/apps/sphinx/etc/sphinx.conf --rotate


5、合并增量索引到主索引
c:wampappssphinxin>indexer --merge -c /usr/local/coreseek/dict/csft_mysql.conf --rotate

原文地址:https://www.cnblogs.com/gophper/p/4419884.html