在利用 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/