Sphinx的介紹
Sphinx是一個以GPLv2許可發布的全文搜索引擎,如果需要在商業項目中使用(例如嵌入到其他程序中),則需要聯繫Sphinx Technologies Inc.獲取商業授權。
一般來說,Sphinx是一個獨立的搜索引擎,旨在爲其他應用程序提供高速、低空間佔用和高相關性的全文搜索功能。Sphinx可以與SQL數據庫和腳本語言輕松集成。
目前,Sphinx内置了對MySQL和PostgreSQL數據庫的支持,並且還支持從標準輸入讀取特定格式的XML數據。通過修改源代碼,用戶可以自行添加新的數據源,例如對其他類型的數據庫管理系統的原生支持。
Sphinx的搜索API支持PHP、Python、Perl、Ruby和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. 下載sphinx包
2. 建立索引文档
3. 修改配置
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 } # 搜索服務需要修改的部分 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 blog<d:/sphinx/example.sql
5. 建立索引
D:/sphinx/bin>indexer.exe test1 ( 備注 :test1 爲 sphinx.conf 的 index test1() ) 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
6. 搜索“test”試試
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 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 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
6. 測試中文搜索
D:/sphinx/bin>indexer.exe test1
搜索關鍵字: 中文 試試:
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>
<?php require ’sphinxapi.php’; $s = new SphinxClient(); $s->SetServer(’localhost’,9312); $result = $s->Query(’ 中文 ’); var_dump($result);
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:9312 accepting connections