在利用 Sphinx 作搜索引擎的时候,通常他的索引创建构成有以下几个部分:sql
1. 固定不变的主索引
2. 增量索引重建
3. 索引数据合并shell
在实际操做中,须要须要为增量索引的创建建立辅助表,这样才能够记住最后创建索引的记录ID,作实际的增量部分的索引创建。ide
CREATE TABLE search_counter
(
counterid INTEGER PRIMARY KEY NOT NULL,
max_doc_id INTEGER NOT NULL
);post
在主索引的数据源中做以下方式的取数据设置
sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type=OFF
sql_query_pre = REPLACE INTO search_counter SELECT 1,MAX(pid) FROM cdb_posts #建立主索引前更改标识位置
sql_query =
SELECT pid, fid,tid,authorid, dateline,subject, message
FROM cdb_posts
WHERE pid > $start AND pid <= $endui
sql_query_range = SELECT 1, max_doc_id FROM search_counter WHERE counterid = 1
sql_range_step = 1000
sql_ranged_throttle = 1000
sql_query_info = SELECT * FROM cdb_posts WHERE pid=$id搜索引擎
在增量索引的数据源中做以下方式的取数据设置blog
sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type=OFF
sql_query =
SELECT pid, fid,tid,authorid, dateline,
subject, message
FROM cdb_posts
WHERE pid >
(SELECT max_doc_id FROM search_counter WHERE counterid=1)
#增量索引是id大于标识位置的部分索引
在创建好配置后首先对sphinx中配置的所有索引作初始化crontab
/usr/local/sphinx/bin/indexer –config –all /usr/local/sphinx/etc/sphinx.confit
为建立2个shell脚本,一个用来建立主索引、一个用来建立增量索引(此步能够省略)
1.建立主索引脚本build_main_index.sh
#!/bin/sh
/usr/local/sphinx/bin/searchd –stop >> /var/log/sphinx/searchdlog
/usr/local/sphinx/bin/indexer discuz –config /usr/local/sphinx/etc/sphinx.conf >> /var/log/sphinx/mainindexlog
/usr/local/sphinx/bin/searchd >> /var/log/sphinx/searchdlog
2.建立增量索引脚本build_delta_index.sh
#!/bin/sh
/usr/local/sphinx/bin/searchd –stop >> /var/log/sphinx/searchdlog
/usr/local/sphinx/bin/indexer discuz_delta –config /usr/local/sphinx/etc/sphinx.conf >> /var/log/sphinx/deltaindexlog
#/usr/local/sphinx/bin/indexer –merge discuz discuz_delta –config /usr/local/sphinx/etc/sphinx.conf >> /var/log/sphinx/deltaindexlog
/usr/local/sphinx/bin/searchd >> /var/log/sphinx/searchdlog
在crontab 中添加定时脚本,按照本身指望的策略按期执行重建索引的操做。
好比能够天天凌晨2点执行主索引重建,其余每10分钟创建一次增量索引重建。
转载自 http://th9988.blog.163.com/blog/static/4888243220113207210871/