萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

Sphinx 全文檢索索引建立流程深度解析

Sphinx 索引建立四階段管線

Sphinx(及其分支 Manticore Search)建立全文索引的核心流程可抽象為四個階段的管線處理,每階段輸出傳遞給下一階段。

階段一:文檔獲取

輸入:資料來源(MySQL/PostgreSQL/XML/TSV/JSON/自定義 XMLPipe2)

動作:

  • 執行 `sql_query` 獲取原始文檔集合
  • 每行文檔必須包含唯一整數 `id`(文檔 ID)
  • 支援多值屬性 (MVA)、JSON 屬性、字串屬性
# sphinx.conf 片段
source src_main {
    type = mysql
    sql_host = localhost
    sql_user = search
    sql_pass = secret
    sql_db = myapp
    sql_query = SELECT id, title, content, category_id, tags, created_at FROM articles WHERE status=1
    sql_attr_uint = category_id
    sql_attr_timestamp = created_at
    sql_attr_multi = uint tag from query; SELECT article_id, tag_id FROM article_tags
}

階段二:分詞

輸入:原始文檔文本

核心組件:Tokenizer(分詞器)

處理步驟:

  1. 字元篩選:`charset_table` 定義哪些字元屬於「詞」的一部分,其餘視為分隔符
  2. 分詞邏輯:
    • 英文/數字:按非字母數字字元分割
    • 中文/日文/韓文:需配置 `ngram_len=1` 或 `ngram_chars`,或整合第三方分詞器(如 `jieba`、MeCab、IK Analyzer)
  3. 輸出:Token 流(位置資訊、原始詞彙)
# 中文分詞配置範例
index idx_main {
    source = src_main
    path = /var/lib/sphinx/idx_main
    charset_table = 0..9, A..Z->a..z, _, a..z, U+4E00..U+9FFF  # 保留中文字元
    ngram_len = 1  # 單字切分(粗糙)
    # 或整合 jieba:
    # morphology = libstemmer_zh  # 需編譯支援
}

階段三:語言處理

輸入:Token 流

核心組件:Linguistic Processor(語言處理器)

處理步驟:

  1. 詞幹提取/詞形還原:`morphology = stem_en, stem_ru, libstemmer_sv` 等
  2. 停用詞過濾:`stopwords = /path/to/stopwords.txt` 移除無語義高頻詞
  3. 同義詞展開:`wordforms = /path/to/wordforms.txt`(如 `iphone > apple phone`)
  4. 例外詞保護:`exceptions = /path/to/exceptions.txt` 保護特定詞不被分詞/詞幹化
  5. 最小/最大詞長:`min_word_len = 1`, `max_word_len = 84`

輸出:標準化 Term(詞項),帶有位置、文檔 ID、字段資訊

階段四:索引建立

輸入:Term 序列

核心組件:Indexer(索引器)

核心資料結構:

  • 倒排索引:Term → {DocID, Position, Field} 映射
  • 文檔列表:儲存屬性值(用於過濾、排序、分組)
  • 詞彙表:Term 字典,支援前綴/通配符查詢
  • 位置資訊:支援語句查詢、鄰近度查詢
# 索引器關鍵配置
index idx_main {
    # ... 前述配置 ...
    
    # 索引類型
    type = plain  # 或 rt (Real-Time 即時索引)
    
    # 記憶體限制(索引建立時使用)
    indexer {
        mem_limit = 256M
    }
    
    # 壓縮選項
    # docinfo = extern  # 屬性外部儲存(預設)
    # inplace_enable = 1 # 就地更新(RT 索引)
}

索引建立命令與流程

# 1. 全量重建(首次或結構變更)
indexer --all --rotate --config /etc/sphinx/sphinx.conf

# 2. 增量更新(主索引 + 增量索引架構)
# 主索引:每日重建
indexer --rotate idx_main --config /etc/sphinx/sphinx.conf

# 增量索引:每 5 分鐘更新
indexer --rotate idx_delta --config /etc/sphinx/sphinx.conf

# 搜尋時合併查詢
# index idx_main idx_delta

# 3. 合併索引(離線合併增量至主索引)
indexer --merge idx_main idx_delta --rotate --config /etc/sphinx/sphinx.conf

關鍵配置參數速查

參數作用建議值
charset_table字元分類表,決定分詞邊界含中文範圍 U+4E00..U+9FFF
ngram_lenN-gram 分詞窗口大小中文 1 或 2;英文 0(關閉)
morphology詞幹提取器列表stem_en, stem_ru, libstemmer_*
stopwords停用詞檔案路徑每行一詞,UTF-8 編碼
wordforms同義詞/詞形映射格式:原詞 > 目標詞
min_word_len最小索引詞長度1 (中文) 或 2 (英文)
html_strip是否剝離 HTML 標籤1 (開啟)
index_exact_words是否索引原始詞(支援精確匹配)1
overshort_step過短詞位置增量1
stopword_step停用詞位置增量1

Real-Time (RT) 索引:即時更新架構

# RT 索引配置(無需重建,支援即時 INSERT/REPLACE/DELETE)
index rt_main {
    type = rt
    path = /var/lib/sphinx/rt_main
    rt_mem_limit = 512M
    
    # 欄位定義(對應 SQL 欄位)
    rt_field = title
    rt_field = content
    rt_attr_uint = category_id
    rt_attr_timestamp = created_at
    rt_attr_multi = uint tag
}

# PHP 客戶端即時寫入
$client = new SphinxClient();
$client->SetServer('127.0.0.1', 9312);
$doc = [
    'id' => 12345,
    'title' => '新文章標題',
    'content' => '文章內容...',
    'category_id' => 5,
    'created_at' => time(),
    'tag' => [10, 20, 30],
];
$client->AddDocument($doc); // 即時可搜尋

總結

Sphinx 索引建立是**文檔 → 分詞 → 語言處理 → 索引**的管線流程。關鍵成功因素:

  1. 字元表正確:中文必須納入 `charset_table`
  2. 分詞策略對路:中文建議整合 jieba/SCWS 而非純 n-gram
  3. 屬性設計精準:僅索引需過濾/排序/分組的欄位為屬性
  4. 增量+合併策略:主索引日更 + 增量索引分鐘更 + 定期合併
  5. RT 索引選型:高即時性需求(電商、IM、日誌)首選 RT 索引
copyright © 萬盛學電腦網 all rights reserved