有這麼一種常見的情況:整個數據集非常大,以至於難於經常性的重建索引,但是每次新增的記錄卻相當地少。一個典型的例子是:一個論壇有1000000個已經歸檔的帖子,但每天只有1000個新帖子。
在這種情況下可以用所謂的“主索引+增量索引”(main+delta)模式來實現“近實時”的索引更新。
這種方法的基本思路是設置兩個數據源和兩個索引,對很少更新或根本不更新的數據建立主索引,而對新增文檔建立增量索引。在上述例子中,那1000000個已經歸檔的帖子放在主索引中,而每天新增的1000個帖子則放在增量索引中。增量索引更新的頻率可以非常快,而文檔可以在出現幾分種內就可以被檢索到。
確定具體某一文檔的分屬那個索引的分類工作可以自動完成。一個可選的方案是,建立一個計數表,記錄將文檔集分成兩部分的那個文檔ID,而每次重新構建主索引時,這個表都會被更新。(上述是摘自Coreseek 4.1 參考手冊 / Sphinx 2.0.1-beta裏的一段話)
從上述我們知道增量索引的必要性。廢話不說,開始實現:
1,首先我們要新建一張表,我命名爲email_delta,字段有兩個:counter_id,max_doc_id。這張表是用來存放執行增量索引時,記錄此時記錄最大值爲max_doc_id,那麼下次執行增量索引時就只需執行id>max_doc_id即可。
2,修改配置文件:e/coreseek/bin/sphinx.conf
source email
{
sql_query_pre = SET NAMES utf8
sql_query_pre =replace into email_delta select 1,max(emailid) from email
sql_query = SELECT emailid,fromid,toid,subject,title,content,sendtime,attachement FROM email where
emailid<=(select max_doc_id from email_delta where counter_id=1)
#主索引:重新建立索引
}
source email_delta:email
{
sql_query_pre =replace into email_delta select 1,max(emailid) from email
sql_query = SELECT emailid,fromid,toid,subject,title,content,sendtime,attachement FROM email where
emailid>(select max_doc_id from email_delta where counter_id=1)
#增量索引:只是對增加的數據建立索引
}
3,增加了增量索引還不夠,當執行完增量索引還要將其和主索引進行合併:要用到--merge
4,主索引可以一天重建一次,增量索引可以每幾分鐘執行一次,這裏就要用到腳本。下面就要寫這兩個腳本:
a)主索引腳本:main.bat(window)
e:\coreseek\bin\searchd -c e:\coreseek\bin\sphinx.conf –-stop #停止服務
e:\coreseek\bin\indexer -c e:\coreseek\bin\sphinx.conf email --all #重建主索引
e:\coreseek\bin\searchd -c e:\coreseek\bin\sphinx.conf --console #啓動服務
b)增量腳本:delta.bat(window)
e:\coreseek\bin\searchd -c e:\coreseek\bin\sphinx.conf --stop
e:\coreseek\bin\indexer -c e:\coreseek\bin\sphinx.conf email_delta --all #重建增量索引
e:\coreseek\bin\indexer -c e:\coreseek\bin\sphinx.conf -–merge email email_delta --rotate#合併索引
e:\coreseek\bin\searchd -c e:\coreseek\bin\sphinx.conf --console
c)windows下實現開機自啓動coreseek:在doc鍵入:searchd –-install -–config e:/coreseek/bin/sphinx.conf
5,在計算機:管理工具->任務計劃程序中創立兩個任務,使其定時執行這兩個腳本。
6,按上述步驟,沒什麼問題可以成功實現!
https://blog.csdn.net/hxy_jci/article/details/9129693