Sphinx (及衍生專案 Manticore Search) 建立全文索引的核心管線如下:
文檔收集 (Documents)
doc_id (正整數) 及多個欄位 (text/string/attr)。sql_query、xmlpipe_command 等定義資料來源。分詞 (Tokenizer)
charset_table 定義字符屬性,ngram_len 處理中日韓無分隔符語言。jieba、mmseg、scws 等外掛分詞器 (需編譯支援)。語言處理 (Linguistic Processor)
morphology (stemmer/lemmatize)、stopwords、wordforms、exceptions。索引建立 (Indexer)
.spa (屬性)、.spi (索引)、.spd (文檔)、.sph (標頭)、.spk (kill-list)、.spm (MVA)。source src_main
{
type = mysql
sql_host = localhost
sql_user = indexer
sql_pass = secret
sql_db = mydb
sql_query = SELECT id, title, content, category_id, UNIX_TIMESTAMP(created_at) AS created_ts FROM articles
sql_attr_uint = category_id
sql_attr_timestamp = created_ts
}
index idx_main
{
source = src_main
path = /var/lib/sphinx/data/idx_main
morphology = stem_en, stem_zh # 需編譯支援
min_word_len = 1
ngram_len = 1 # 中文單字切分
charset_table = 0..9, A..Z->a..z, _, a..z, U+4E00..U+9FFF # 保留中文
}
indexer
{
mem_limit = 256M
}
searchd
{
listen = 9312
listen = 9306:mysql41
log = /var/log/sphinx/searchd.log
query_log = /var/log/sphinx/query.log
pid_file = /var/run/sphinx/searchd.pid
}
# 全量重建
indexer --all --rotate -c /etc/sphinx/sphinx.conf
# 僅重建單一索引
indexer idx_main --rotate -c /etc/sphinx/sphinx.conf
# RT 索引即時寫入 (需 searchd 運行)
# INSERT INTO idx_rt (id, title, content) VALUES (1, '標題', '內容');
jieba 或 scws,精度遠優於 ngram。sql_attr_*,查詢時用 SetFilter 而非全文搜尋。--merge 合併。distributed 索引聚合多分片,水平擴展。