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