实现原理:
首先创建一个计数器:
1.先在mysql中插入一个计数表
CREATE TABLE sph_counter(
counter_id INTEGER PRIMARY KEY NOT NULL,
max_doc_id INTEGER NOT NULL
);
2.再次修改配置文件,在主数据源里面我们要改预查询语句:
Vi /usr/local/coreseek/etc/csft.conf
source main{
sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type=OFF
sql_query_pre = REPLACE INTO sph_counter SELECT 1, MAX(id) FROM documents
sql_query = SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content FROM documents /
WHERE id<=( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 )
… //其他可以默认
}
source delta : main //继承数据源
{
sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type=OFF
sql_query_pre =
sql_query = SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content FROM documents /
WHERE id>( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 )
}
index mian //主索引
{
source = main
Path=/usr/local/coreseek/var/data/main
}
index delta : main //增量索引
{
source = delta
Path=/usr/local/coreseek/var/data/delta
}
3.重建增量索引
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft.conf delta
用/usr/local/coreseek/bin/search工具来检索,查询主索引中检索结果为0,而新数据库在增量索引中检索到。
4.实时更新索引
建2个shell脚本,一个主索引脚本、一个增量索引脚本
Vi main.sh
#!/bin/sh
/usr/local/coreseek/bin/indexer main –c /usr/local/coreseek/etc/csft.conf >> /usr/local/coreseek/var/log/main.log
Vi delta.sh
#!/bin/sh
/usr/local/coreseek/bin/indexer delta –c /usr/local/coreseek/etc/csft.conf –rotate>> /usr/local/coreseek/var/log/delta.log
加入到计划任务里面:每5分钟重新增量索引;每天凌晨2点重建主索引
*/5 * * * * /usr/local/coreseek/etc/delta.sh > /dev/null 2>&1
0 2 * * * /usr/local/coreseek/etc/main.sh > /dev/null 2>&1