1、创建一张表,用于存放主数据源的数据最大id。
mysql> create table sph_count( id int (10) not null primary key auto_increment, max_id int (10) not null ); Query OK, 0 rows affected (0.04 sec)2、修改csft.conf配置文件,支持增量索引和主索引的数据sql语句修改
#在主数据中 source main { #每次生成主数据索引的时候,都保存当前表的最大id到sph_count表中 sql_query_pre=replace into sph_count select 1,max( id ) from post #数据源的数据从小于等于最大id中取 sql_query= select id ,title,content from post where id <= ( select max_id from sph_count where id = 1) } #开启增量数据源,source src1throttled : src1改成 source delta : main source delta: main { sql_query_pre= set names utf8 #增量的数据在id大于最大id中取 sql_query= select id ,title,content from post where id >( select max_id from sph_count where id = 1) } #开启增量索引index test1stemmed : test1 改成 index delta : main index delta : main { source =delta path = /usr/local/coreseek/var/data/delta morphology = stem_en }3、数据测试
先查看下mysql目前表里的数据
mysql> select * from post; + ----+---------------+--------------------------------------+ | id | title | content | + ----+---------------+--------------------------------------+ | 3 | Linux sphinx2 | Linux php sphinx2 吴泽鑫 | | 4 | Linux sphinx2 | Linux php sphinx2 吴泽鑫的博客 | | 5 | Linux sphinx2 | Linux php sphinx2 jensen的博客 | | 6 | Linux sphinx2 | Linux php sphinx2 中文分词测试 | | 7 | Linux sphinx2 | Linux php sphinx2 中文分词查找 | | 8 | Linux sphinx2 | Linux php sphinx2 中文分词查找 | | 9 | linux java | java | + ----+---------------+--------------------------------------+ 7 rows in set (0.00 sec)生成下主索引
[root@promote bin] # ./indexer main --rotate Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)] Copyright (c) 2007-2011, Beijing Choice Software Technologies Inc (http: //www .coreseek.com) using config file '/usr/local/coreseek/etc/csft.conf' ... indexing index 'main' ... collected 7 docs, 0.0 MB sorted 0.0 Mhits, 100.0% done total 7 docs, 296 bytes total 0.021 sec, 13711 bytes /sec , 324.25 docs /sec total 1 reads, 0.000 sec, 0.3 kb /call avg, 0.0 msec /call avg total 5 writes, 0.000 sec, 0.2 kb /call avg, 0.0 msec /call avg rotating indices: succesfully sent SIGHUP to searchd (pid=6413).这时候表sph_count的最大mx_id是9
mysql> select * from sph_count; + ----+--------+ | id | max_id | + ----+--------+ | 1 | 9 | + ----+--------+ 1 row in set (0.00 sec)接着,再模拟往表中插入一条数据,id为10
mysql> select * from post; + ----+---------------+--------------------------------------+ | id | title | content | + ----+---------------+--------------------------------------+ | 3 | Linux sphinx2 | Linux php sphinx2 吴泽鑫 | | 4 | Linux sphinx2 | Linux php sphinx2 吴泽鑫的博客 | | 5 | Linux sphinx2 | Linux php sphinx2 jensen的博客 | | 6 | Linux sphinx2 | Linux php sphinx2 中文分词测试 | | 7 | Linux sphinx2 | Linux php sphinx2 中文分词查找 | | 8 | Linux sphinx2 | Linux php sphinx2 中文分词查找 | | 9 | linux java | java | | 10 | jensen blog | jensen 博客 | + ----+---------------+--------------------------------------+ 8 rows in set (0.00 sec)这时候,我们去搜索关键字'blog'是搜索不到的
[root@promote bin] # ./search 'blog' Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)] Copyright (c) 2007-2011, Beijing Choice Software Technologies Inc (http: //www .coreseek.com) using config file '/usr/local/coreseek/etc/csft.conf' ... index 'main' : query 'blog ' : returned 0 matches of 0 total in 0.003 sec words: 1. 'blog' : 0 documents, 0 hits index 'delta' : query 'blog ' : returned 0 matches of 0 total in 0.000 sec words: 1. 'blog' : 0 documents, 0 hits这时候再生成增量索引,增量索引的数据源是来自id大于sph_count表中max_id的数据
[root@promote bin] # ./indexer delta --rotate Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)] Copyright (c) 2007-2011, Beijing Choice Software Technologies Inc (http: //www .coreseek.com) using config file '/usr/local/coreseek/etc/csft.conf' ... indexing index 'delta' ... collected 1 docs, 0.0 MB sorted 0.0 Mhits, 100.0% done total 1 docs, 24 bytes total 0.004 sec, 5016 bytes /sec , 209.03 docs /sec total 1 reads, 0.000 sec, 0.0 kb /call avg, 0.0 msec /call avg total 5 writes, 0.000 sec, 0.0 kb /call avg, 0.0 msec /call avg rotating indices: succesfully sent SIGHUP to searchd (pid=6413).可以看出,生成的确实是 ‘total 1 docs’ 一个文档,这时候我们再search下关键字'blog',就可以找到数据了
[root@promote bin] # ./search 'blog' Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)] Copyright (c) 2007-2011, Beijing Choice Software Technologies Inc (http: //www .coreseek.com) using config file '/usr/local/coreseek/etc/csft.conf' ... index 'main' : query 'blog ' : returned 0 matches of 0 total in 0.002 sec words: 1. 'blog' : 0 documents, 0 hits index 'delta' : query 'blog ' : returned 1 matches of 1 total in 0.000 sec displaying matches: 1. document=10, weight=1 id =10 title=jensen blog content=jensen ?? words: 1. 'blog' : 1 documents, 1 hits这样我们就测试成功了,完成了‘主索引+增量索引’的结合。我们就可以在linux里面设定个计划任务,每隔几分钟生成一个增量索引,来达到数据增量搜索的作用,就不用再一次性去生成主索引了。当然主索引我们也可以设定个计划任务,让凌晨人少的时候来自动生成,这样就达到更新的效果了。
