原文地址:http://www.fuchaoqun.com/2008/11/sphinx-on-windows-xp/
前一陣子嘗試使用了一下Sphinx,一個能夠被各種語言(PHP/Python/Ruby/etc)方便調用的全文檢索索引系統。網上的資料大多是在linux環境下的安裝使用,當然,作為生產環境很有必要部署在*nix環境下,作為學習測試,還是windows環境比較方便些。
本文旨在提供一種便捷的方式讓Sphinx在windows下安裝配置以支援中文全文檢索索引,配置部分在linux下通用。
一、關於Sphinx
Sphinx 是一個在GPLv2 下發布的一個全文檢索索引引擎,商業授權(例如, 嵌入到其他程式中)需要聯絡作者(Sphinxsearch.com)以獲得商業授權。
一般而言,Sphinx是一個獨立的搜尋引擎,意圖為其他應用提供高速、低空間佔用、高結果相關度的全文檢索搜尋功能。Sphinx可以非常容易的與SQL資料庫和指令碼語言整合。
當前系統內建MySQL和PostgreSQL 資料庫資料來源的支援,也支援從標準輸入讀取特定格式的XML資料。通過修改原始碼,使用者可以自行增加新的資料來源(例如:其他類型的DBMS的原生支援)。
搜尋API支援PHP、Python、Perl、Rudy和Java,並且也可以用作MySQL儲存引擎。搜尋API非常簡單,可以在若干個小時之內移植到新的語言上。
Sphinx特性:
- 高速的建立索引(在當代CPU上,峰值效能可達到10MB/秒);
- 高效能的搜尋(在2–4GB的文本資料上,平均每次檢索回應時間小於0.1秒);
- 可處理海量資料(目前已知可以處理超過100GB的文本資料,在單一CPU的系統上可處理100M文檔);
- 提供了優秀的相關度演算法,基於短語相似性和統計(BM25)的複合Ranking方法;
- 支援分布式搜尋;
- 提供檔案的摘錄產生;
- 可作為MySQL的儲存引擎提供搜尋服務;
- 支援布爾、短語、詞語相似性等多種檢索模式;
- 文檔支援多個全文檢索索引欄位(最大不超過32個);
- 文檔支援多個額外的屬性資訊(例如:分組資訊,時間戳記等);
- 停止詞查詢;
- 支援單一位元組編碼和UTF-8編碼;
- 原生的MySQL支援(同時支援MyISAM和InnoDB);
- 原生的PostgreSQL支援.
中文手冊可以在這裡獲得,感謝譯者的辛勤工作。
二、Sphinx在windows上的安裝
1.直接在http://www.sphinxsearch.com/downloads.html找到最新的windows版本,我這裡下的是Win32 release binaries with MySQL support,下載後解壓在D:\sphinx目錄下;
2.在D:\sphinx\下建立一個data目錄用來存放索引檔案,一個log目錄方記錄檔,複製D:\sphinx\sphinx.conf.in到D:\sphinx\bin\sphinx.conf(注意修改檔案名稱);
3.修改D:\sphinx\bin\sphinx.conf,我這裡列出需要修改的幾個:
type = mysql # 資料來源,我這裡是mysql
sql_host = localhost # 資料庫伺服器
sql_user = root # 資料庫使用者名稱
sql_pass = '' # 資料庫密碼
sql_db = test # 資料庫
sql_port = 3306 # 資料庫連接埠
sql_query_pre = SET NAMES utf8 # 去掉此行前面的注釋,如果你的資料庫是uft8編碼的
index test1
{
# 放索引的目錄
path = D:/sphinx/data/
# 編碼
charset_type = utf-8
# 指定utf-8的編碼錶
charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
# 簡單分詞,只支援0和1,如果要搜尋中文,請指定為1
ngram_len = 1
# 需要分詞的字元,如果要搜尋中文,去掉前面的注釋
ngram_chars = U+3000..U+2FA1F
}
# index test1stemmed : test1
# {
# path = @CONFDIR@/data/test1stemmed
# morphology = stem_en
# }
# 如果沒有分布式索引,注釋掉下面的內容
# index dist1
# {
# 'distributed' index type MUST be specified
# type = distributed
# local index to be searched
# there can be many local indexes configured
# local = test1
# local = test1stemmed
# remote agent
# multiple remote agents may be specified
# syntax is 'hostname:port:index1,[index2[,...]]
# agent = localhost:3313:remote1
# agent = localhost:3314:remote2,remote3
# remote agent connection timeout, milliseconds
# optional, default is 1000 ms, ie. 1 sec
# agent_connect_timeout = 1000
# remote agent query timeout, milliseconds
# optional, default is 3000 ms, ie. 3 sec
# agent_query_timeout = 3000
# }
# 搜尋服務需要修改的部分
searchd
{
# 日誌
log = D:/sphinx/log/searchd.log
# PID file, searchd process ID file name
pid_file = D:/sphinx/log/searchd.pid
# windows下啟動searchd服務一定要注釋掉這個
# seamless_rotate = 1
}
4.匯入測試資料
C:\Program Files\MySQL\MySQL Server 5.0\bin>mysql -uroot test<d:/sphinx/example.sql
5.建立索引
D:\sphinx\bin>indexer.exe –all
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
using config file ‘./sphinx.conf’…
indexing index ‘test1′…
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.101 sec, 1916.30 bytes/sec, 39.72 docs/sec
D:\sphinx\bin>
6.搜尋’test’試試
D:\sphinx\bin>search.exe test
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
using config file ‘./sphinx.conf’…
index ‘test1′: query ‘test ‘: returned 3 matches of 3 total in 0.000 sec
displaying matches:
1. document=1, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
id=1
group_id=1
group_id2=5
date_added=2008-11-26 14:58:59
title=test one
content=this is my test document number one. also checking search within
phrases.
2. document=2, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
id=2
group_id=1
group_id2=6
date_added=2008-11-26 14:58:59
title=test two
content=this is my test document number two
3. document=4, weight=1, group_id=2, date_added=Wed Nov 26 14:58:59 2008
id=4
group_id=2
group_id2=8
date_added=2008-11-26 14:58:59
title=doc number four
content=this is to test groups
words:
1. ‘test’: 3 documents, 5 hits
D:\sphinx\bin>
都所出來了吧。
6.測試中文搜尋
修改test資料庫中documents資料表,
UPDATE `test`.`documents` SET `title` = ‘測試中文’, `content` = ‘this is my test document number two,應該搜的到吧’ WHERE `documents`.`id` = 2;
重建索引:
D:\sphinx\bin>indexer.exe –all
搜尋’中文’試試:
D:\sphinx\bin>search.exe 中文
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
using config file ‘./sphinx.conf’…
index ‘test1′: query ‘中文 ‘: returned 0 matches of 0 total in 0.000 sec
words:
D:\sphinx\bin>
貌似沒有搜到,這是因為windows命令列中的編碼是gbk,當然搜不出來。我們可以用程式試試,在D:\sphinx\api下建立一個foo.php的檔案,注意utf-8編碼
<?php
require ‘sphinxapi.php’;
$s = new SphinxClient();
$s->SetServer(‘localhost’,3312);
$result = $s->Query(‘中文’);
var_dump($result);
?>
啟動Sphinx searchd服務
D:\sphinx\bin>searchd.exe
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
WARNING: forcing –console mode on Windows
using config file ‘./sphinx.conf’…
creating server socket on 0.0.0.0:3312
accepting connections
執行PHP查詢:
php d:/sphinx/api/foo.php
結果是不是出來?剩下的工作就是去看手冊,慢慢摸索高階的配置
原文地址:http://www.fuchaoqun.com/2008/11/sphinx-on-windows-xp/