Sphinx 配置採用段落式結構,核心三大塊:source(數據源)、index(索引)、searchd(搜索服務)。本文聚焦前兩者。
source test
{
type = mysql
sql_host = 127.0.0.1
sql_user = root
sql_pass = root
sql_db = test
sql_port = 3306
sql_query_pre = SET NAMES utf8
sql_query = SELECT id, name, add_time FROM tbl_test
sql_attr_timestamp = add_time
sql_query_info_pre = SET NAMES utf8
sql_query_info = SELECT * FROM tbl_test WHERE id=$id
}
sql_query:核心抓取 SQL,第一列必須為唯一整數 IDsql_attr_*:定義屬性(整數、浮點、時間戳、字符串、MVA),用於過濾、排序、分組sql_query_info:僅供命令行調試用,搜索 API 不使用index test_index
{
source = test
path = /usr/local/coreseek/var/data/test
docinfo = extern
charset_dictpath = /usr/local/mmseg3/etc/
charset_type = zh_cn.utf-8
ngram_len = 1
ngram_chars = U+3000..U+2FA1F
}
path:索引文件前綴,生成 .spa/.spi/.spd 等文件docinfo:extern 將屬性單獨存儲(推薦),inline 混存charset_type:zh_cn.utf-8 啟用內置中文分詞,utf-8 需配合 ngramcharset_type = utf8
ngram_len = 1
ngram_chars = U+3000..U+2FA1F
無需詞典,索引體積大、召回率高、精準度低,適合通用搜索。
charset_type = utf8
charset_dictpath = /usr/local/mmseg3/etc/
ngram_len = 0
需 uni.lib 詞典,精準度高、索引小,適合垂直領域。
tbl_test(id, name, add_time)indexer --all 構建索引search -c sphinx.conf "關鍵詞" 命令行測試$cl->Query("關鍵詞", "test_index")sql_query_pre、charset_type、數據庫連接字符集indexer --rotate 或實時索引 (RT Index)