標籤:sphinx
Sphinx mysql 增量索引
一、增量索引的理解:
向一個資料表插入資料時,這些新插入的資料,就是增量了,sphinx是根據索引來尋找資料的,如果索引沒有更新,新增資料是查不出來的,所以我們要更新主索引,更新增量索引,增量條件的設定就比較重要了。
二、sphinx增量索引的設定:資料庫中的已有資料很大,又不斷有新資料加入到資料庫中,也希望能 夠檢索到。全部重建立立索引很消耗資源,因為我們需要更新的資料相比較而言很少。例如。 原來的資料有幾百萬條,而新增的只是幾千條。這樣就可以使用“主索引+增量索引”的模式來實 現近乎即時更新的功能。
三、原理:這個模式實現的基本原理是設定兩個資料來源和兩個索引,為那些基本不更新的資料建立主 索引,而對於那些新 增的資料建立增量索引。主索引的更新頻率可以設定的長一些(例如設定 在每天的午夜進行),而增量索引的更新頻率,我們可以將時間設定的很短(幾分鐘左 右),這樣 在使用者搜尋的時候,我們可以同時查詢這兩個索引的資料。
使用增量索引需要用到一張計數表,記錄每次重新構建主索引時,被索引表的最後一個資料id。 每次構建都要更新。表以example.sql為例
四、開始配置
1.建立一張計數表和兩張索引表
create table sph_counter(counter_id integer primary key not null,max_doc_id integer not null);
2.修改設定檔csft.conf
source src1
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = root
sql_db = test
sql_port = 3306 # optional, default is 3306
sql_sock = /tmp/mysql.sock
sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type=OFF
#有代替sph_counter中的max_doc_id,沒有會添加,更新計數表的索引
sql_query_pre =replace into sph_counter select 1,max(id) from news
sql_query =select id,title,content from news where id<= (select max_doc_id from sph_counter where counter_id=1)
}
#注意:ddj中的sql_query_pre的個數需和src1對應,否則可能搜尋不出相應結果
#增量資料來源
source ddj: src1
{
sql_ranged_throttle = 100
sql_query_pre=set names utf8
#查詢增量資料
sql_query=select id,title,content from news where id > (select max_doc_id from sph_counter where counter_id=1)
}
index test1//主索引
{
source = src1
path = /usr/local/coreseek/var/data/test1
}
index ddj:test1//增量索引
{
source =ddj
path = /usr/local/coreseek/var/data/ddj
morphology = stem_en
}
注意:在400行左右配置charset=zh_cn.utf8charset_dictpath=/usr/local/mmseg3/etc
3.重建立立索引
如果sphinx正在運行,停止sphinx服務,然後根據設定檔來建立索引 /usr/local/sphinx/bin/indexer --all /usr/local/sphinx/bin/indexer test1 /usr/local/sphinx/bin/indexer 重索引名字 /usr/local/sphinx/bin/searchd --stop
/usr/local/sphinx/bin/indexer -c
/usr/local/sphinx/etc/sphinx.conf --all
/usr/local/sphinx/bin/searchd -c
/usr/local/sphinx/etc/sphinx.conf
/usr/local/sphinx/bin/indexer -c
/usr/local/sphinx/etc/sphinx.conf --all --rotate
4.索引合并
例如:將delta合并到main中
indexer --merge main delta
5.索引自動更新
需要使用到指令碼
指令碼的運行需要許可權,利用crontab -e 來編輯
*/30 * * * * /bin/sh /usr/local/sphinx/etc/build_delta_index.sh > /dev/null 2>&1
30 2 * * * /bin/sh /usr/local/sphinx/etc/build_main_index.sh > /dev/null 2>&1
第一條是表示每30分鐘運行 /usr/local/sphinx/etc/下的build_delta_index.sh 指令碼,輸出重新導向。
第二條是表示 每天的 淩晨2:30分運行 /usr/local/sphinx/etc下的build_main_inde.sh 指令碼,輸出重新導向。
sphinx mysql 增量索引