項目中文章的信息內容由於持續有新增,而文章總量的基數又比較大,因此作搜索的時候,用了主索引+增量索引這種方式來實現索引的實時更新。html
實現原理:mysql
1. 新建一張表,記錄一下上一次已經建立好索引的最後一條記錄的ID
2. 當索引時,而後從數據庫中取出全部ID大於上面那個sphinx中的那個ID的數據, 這些就是新的數據,而後建立一個小的索引文件
3. 把上邊咱們建立的增量索引文件合併到主索引文件上去
4. 把最後一條記錄的ID更新到第一步建立的表中sql
值得注意的兩點:數據庫
1)當合並索引的時候,只是把增量的索引合併進主索引中,增量索引自己並不會變化,也不會被刪除;網絡
2)當重建主索引的時候,增量索引就會被刪除;post
具體操做實現流程:spa
1. 新建一張表,用於存儲已經建過索引的最大的doc_id命令行
CREATE TABLE `sph_counter` ( `counter_id` int(11) NOT NULL COMMENT '標識不一樣的數據表', `max_doc_id` int(11) NOT NULL COMMENT '每一個索引表的最大ID,會實時更新', PRIMARY KEY (`counter_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
2. 配置索引文件code
#主索引數據源定義 source article_main { type = mysql sql_host =xxx.xxx.xxx.xx sql_user = sql_pass = sql_db = sql_port = 3306 sql_query_pre = SET NAMES utf8 sql_query_pre = REPLACE INTO sph_counter SELECT 1, MAX(id) FROM documents sql_query_range = sql_range_step = 10000 sql_query = \ SELECT *\ FROM documents WHERE id>=$start AND id<=$end sql_attr_timestamp = pubtime #從SQL讀取到的值必須爲整數,做爲時間屬性 sql_query_info_pre = SET NAMES utf8 #命令行查詢時,設置正確的字符集 sql_query_info = SELECT * FROM documents WHERE id=$id #命令行查詢時,從數據庫讀取原始數據信息 }
# 增量索引數據源定義 source article_delta : article_main { sql_query_pre = SET NAMES utf8 sql_query_range = sql_range_step = 10000 sql_query = \ SELECT *\ FROM documents WHERE id>=$start AND id<=$end sql_attr_timestamp = pubtime #從SQL讀取到的值必須爲整數,做爲時間屬性 # 增量索引建立完成以後,更新最大的doc_id sql_query_post = UPDATE sph_counter SET max_doc_id=(SELECT MAX(id) FROM documents) where counter_id=1 # REPLACE INTO sph_counter SELECT 1, MAX(id) FROM documents sql_query_info_pre = SET NAMES utf8 #命令行查詢時,設置正確的字符集 sql_query_info = SELECT * FROM article_info WHERE id=$id #命令行查詢時,從數據庫讀取原始數據信息 }
# 主索引index定義 index article_main { source = article_main #對應的source名稱 path = /data/... #請修改成實際使用的絕對路徑,例如:/usr/local/coreseek/var/... docinfo = extern mlock = 0 morphology = none min_word_len = 1 html_strip = 0 #中文分詞配置,詳情請查看:http://www.coreseek.cn/products-install/coreseek_mmseg/ charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux環境下設置,/符號結尾 #charset_dictpath = etc/ #Windows環境下設置,/符號結尾,最好給出絕對路徑,例如:C:/usr/local/coreseek/etc/... charset_type = zh_cn.utf-8 } # 增量索引index定義 index article_delta : article_main { source = article_delta path = /data/.... docinfo = extern mlock = 0 morphology = none min_word_len = 1 html_strip = 0 #中文分詞配置,詳情請查看:http://www.coreseek.cn/products-install/coreseek_mmseg/ charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux環境下設置,/符號結尾 #charset_dictpath = etc/ #Windows環境下設置,/符號結尾,最好給出絕對路徑,例如:C:/usr/local/coreseek/etc/... charset_type = zh_cn.utf-8 }
配置文件中修改好本部分以後,須要從新啓動一下searchd進程,讓其加載新的配置文件htm
sudo /usr/local/coreseek4/bin/searchd -c /usr/local/coreseek4/etc/xxxx.conf --stop
sudo nohup /usr/local/coreseek4/bin/searchd -c /usr/local/coreseek4/etc/xxxx.conf &
新建主索引 :
sudo /usr/local/coreseek4/bin/indexer -c /usr/local/coreseek4/etc/xxxx.conf article_main --rotate
過一段時間再新建增量索引(須要將此命令放到定時任務中,跑的頻率按照實際需求來定)
sudo /usr/local/coreseek4/bin/indexer -c /usr/local/coreseek4/etc/xxxx.conf article_delta --rotate
下一步,本身能夠用命令行的search來查詢增量索引的內容
/usr/local/coreseek4/bin/search -c /usr/local/coreseek4/etc/xxxx.conf 網絡文章