Sphinx 在 windows 下安装使用
前一阵子尝试使用了一下 Sphinx ,一个能够被各种语言 (PHP/Python/Ruby/etc) 方便调用的全文检索系统。网上的资料大多是在 linux 环境下的安装使用,当然,作为生产环境很有必要部署在 *nix 环境下,作为学习测试,还是 windows 环境比较方便些。
本文旨在提供一种便捷的方式让 Sphinx 在 windows 下安装配置以支持中文全文检索,配置部分在 linux 下通用。
一、关于 Sphinx
Sphinx 是一个在 GPLv2 下发布的一个全文检索引擎,商业授权(例如 , 嵌入到其他程序中)需要联系作者( Sphinxsearch.com )以获得商业授权。
一般而言, Sphinx 是一个独立的搜索引擎,意图为其他应用提供高速、低空间占用、高结果相关度的全文搜索功能。 Sphinx 可以非常容易的与 SQL 数据库和脚本语言集成。
当前系统内置 MySQL 和 PostgreSQL 数据库数据源的支持,也支持从标准输入读取特定格式的 xm
搜索 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 版本,我这里下的是<a href="http://www.sphinxsearch.com/downloads/sphinx-0.9.8.1-win32.zip" target="_blank"> Win32 release binaries with MySQL support</a>,下载后解压在 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
}
# 搜索服务需要修改的部分
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. 导入测试数据
将sql文件导入数据库(sql 文件在 D:/sphinx/example.sql)
C:/Program Files/MySQL/MySQL Server 5.0/bin>mysql -uroot blog<d:/sphinx/example.sql
说明:
(1)由于我的数据库名称叫blog,因此这里我写的是blog,blog数据库编码设置为utf8
(2)example.sql中默认的数据库名为test,在导入前修改为统一替换为blog即可,若本身就是在test数据库下测试,就不用修改了
5. 建立索引
打开cmd窗口,进入目录D:\sphinx\bin
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’ 试试
D:/sphinx/bin>search.exe 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
ti
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
ti
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
ti
content=this is to test groups
words:
1. ‘test’: 3 documents, 5 hits
6. 测试中文搜索
修改 blog数据库中 documents 数据表,
UPDATE `blog`.`documents` SET `ti
重建索引:
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>
貌似没有搜到,这是因为 windows 命令行中的编码是 gbk ,当然搜不出来。我们可以用程序试试,在 D:/sphinx/api 下新建一个 foo.php 的文件,注意 utf-8 编码
<?php
require ’sphinxapi.php’;
$s = new SphinxClient();
$s->SetServer(’localhost’,9312);
$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:9312
accepting connections
执行 PHP 查询:
访问 http://www.test.com/sphinx/api/foo.php ( 自己配置的虚拟主机 )
sphinx-0.9.8.1-win32(1).zip
( 1.97 MB 下载:29 次 )
