Sphinx 是高效能開源全文檢索引擎,支援中文分詞、分散式搜尋、MySQL/PostgreSQL 原生整合。本文記錄在 Windows 環境從下載、設定、建立索引到中文測試的完整流程,配置檔參數詳細註解,適合快速上手測試。
D:\sphinx。D:\sphinx\data(索引)、D:\sphinx\log(日誌)資料夾。sphinx.conf.in 為 bin\sphinx.conf 並修改關鍵段落:# 資料來源
source src1
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass =
sql_db = test
sql_port = 3306
sql_query_pre = SET NAMES utf8
}
# 索引設定(關鍵:中文分詞)
index test1
{
path = D:/sphinx/data/
charset_type = utf-8
charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
ngram_len = 1
ngram_chars = U+3000..U+2FA1F
}
# searchd 服務
searchd
{
log = D:/sphinx/log/searchd.log
pid_file = D:/sphinx/log/searchd.pid
# Windows 必須註解 seamless_rotate
}
mysql -uroot test < d:/sphinx/example.sqlindexer.exe test1(或 indexer.exe --all 重建所有)search.exe test(英文正常)Windows CMD 預設編碼為 GBK,直接輸入中文會查不到。解法:寫 PHP 測試腳本(儲存為 UTF-8):
<?php
require 'sphinxapi.php';
$s = new SphinxClient();
$s->SetServer('localhost', 3312);
$result = $s->Query('中文');
var_dump($result);
?>
啟動服務 searchd.exe,再跑 php d:/sphinx/api/foo.php 即可看到中文搜尋結果。後續請參考官方手冊調整進階參數。