sphinx 安装与测试 linux 环境

在开发的sphinx 0.9.10版本已可存储这些文本数据,作者曾试过,性能和存储上的效果都不佳,毕竟还没出正式版

本文采用的是第一种方式

在*nix系统下安装,首先需要以下一些软件支持

软件环境:

* 操作系统:Centos-5.2
* 数据库:mysql-5.0.77-3.el5 mysql-devel(如果要使用sphinxSE插件存储请使用mysql-5.1以上版本)
* 编译软件:gcc gcc-c++ autoconf automake
* Sphinx :Sphinx-0.9.9 (最新稳定版 )

安装:

* [root@localhost ~]# yum install -y mysql mysql-devel
* [root@localhost ~]# yum install -y automake autoconf
* [root@localhost ~]# cd /usr/local/src/
* [root@localhost src]# wget http://www.sphinxsearch.com/downloads/sphinx-0.9.9.tar.gz
* [root@localhost src]# tar zxvf sphinx-0.9.9.tar.gz
* [root@localhost local]# cd sphinx-0.9.9
* [root@localhost sphinx-0.9.9]# ./configure –prefix=/usr/local/sphinx #注意:这里sphinx已经默认支持了mysql
* [root@localhost sphinx-0.9.9]# make && make install # 其中的“警告”可以忽略

安装完毕后查看一下/usr/local/sphinx下是否有 三个目录 bin etc var,如有,则安装无误!

ubuntu 下安装

先安装 g++ mysqlserver5

svn checkout  sphinx 的版本

[root@localhost sphinx-0.9.9]# ./configure –prefix=/usr/local/sphinx #注意:这里sphinx已经默认支持了mysql
    [root@localhost sphinx-0.9.9]# make && make install # 其中的“警告”可以忽略  步骤同centos

安装完毕后查看一下/usr/local/sphinx下是否有 三个目录 bin etc var,如有,则安装无误!

3.配置实例
3.1、数据源。

这里我们采用 mysql的数据源。具体情况如下:

Mysql server:192.168.1.10

Mysql db :test

Mysql 表:test.sphinx_article

mysql> desc sphinx_article;
+———–+———————+——+—–+———+—————-+
| Field | Type | Null | Key | Default | Extra |
+———–+———————+——+—–+———+—————-+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(255) | NO | | | |
| cat_id | tinyint(3) unsigned | NO | MUL | | |
| member_id | int(11) unsigned | NO | MUL | | |
| content | longtext | NO | | | |
| created | int(11) | NO | MUL | | |
+———–+———————+——+—–+———+—————-+
6 rows in set (0.00 sec)
3.2、配置文件

* [root@localhost ~]#cd /usr/local/sphinx/etc #进入sphinx的配置文件目录
* [root@localhost etc]# cp sphinx.conf.dist sphinx.conf #新建Sphinx配置文件
* [root@localhost etc]# vim sphinx.conf #编辑sphinx.conf

具体实例配置文件:

##### 索引源 ###########
source article_src
{
type = mysql #####数据源类型
sql_host = 192.168.1.10 ######mysql主机
sql_user = root ########mysql用户名
sql_pass = pwd############mysql密码
sql_db = test #########mysql数据库名
sql_port= 3306 ###########mysql端口
sql_query_pre = SET NAMES UTF8 ###mysql检索编码,特别要注意这点,很多人中文检索不到是数据库的编码是GBK或其他非UTF8
sql_query = SELECT id,title,cat_id,member_id,content,created FROM sphinx_article ####### 获取数据的sql

#####以下是用来过滤或条件查询的属性############

sql_attr_uint = cat_id ######## 无符号整数属性
sql_attr_uint = member_id
sql_attr_timestamp = created ############ UNIX时间戳属性

sql_query_info = select * from sphinx_article where id=$id ######### 用于命令界面端(CLI)调用的测试

}

### 索引 ###

index article
{
source = article_src ####声明索引源
path = /usr/local/sphinx/var/data/article #######索引文件存放路径及索引的文件名
docinfo = extern ##### 文档信息存储方式
mlock = 0 ###缓存数据内存锁定
morphology = none #### 形态学(对中文无效)
min_word_len = 1 #### 索引的词最小长度
charset_type = utf-8 #####数据编码

##### 字符表,注意:如使用这种方式,则sphinx会对中文进行单字切分,
##### 即进行字索引,若要使用中文分词,必须使用其他分词插件如 coreseek,sfc

charset_table = U+FF10..U+FF19->0..9, 0..9, U+FF41..U+FF5A->a..z, U+FF21..U+FF3A->a..z,\
A..Z->a..z, a..z, U+0149, U+017F, U+0138, U+00DF, U+00FF, U+00C0..U+00D6->U+00E0..U+00F6,\
U+00E0..U+00F6, U+00D8..U+00DE->U+00F8..U+00FE, U+00F8..U+00FE, U+0100->U+0101, U+0101,\
U+0102->U+0103, U+0103, U+0104->U+0105, U+0105, U+0106->U+0107, U+0107, U+0108->U+0109,\
U+0109, U+010A->U+010B, U+010B, U+010C->U+010D, U+010D, U+010E->U+010F, U+010F,\
U+0110->U+0111, U+0111, U+0112->U+0113, U+0113, U+0114->U+0115, U+0115, \
U+0116->U+0117,U+0117, U+0118->U+0119, U+0119, U+011A->U+011B, U+011B, U+011C->U+011D,\
U+011D,U+011E->U+011F, U+011F, U+0130->U+0131, U+0131, U+0132->U+0133, U+0133, \
U+0134->U+0135,U+0135, U+0136->U+0137, U+0137, U+0139->U+013A, U+013A, U+013B->U+013C, \
U+013C,U+013D->U+013E, U+013E, U+013F->U+0140, U+0140, U+0141->U+0142, U+0142, \
U+0143->U+0144,U+0144, U+0145->U+0146, U+0146, U+0147->U+0148, U+0148, U+014A->U+014B, \
U+014B,U+014C->U+014D, U+014D, U+014E->U+014F, U+014F, U+0150->U+0151, U+0151, \
U+0152->U+0153,U+0153, U+0154->U+0155, U+0155, U+0156->U+0157, U+0157, U+0158->U+0159,\
U+0159,U+015A->U+015B, U+015B, U+015C->U+015D, U+015D, U+015E->U+015F, U+015F, \
U+0160->U+0161,U+0161, U+0162->U+0163, U+0163, U+0164->U+0165, U+0165, U+0166->U+0167, \
U+0167,U+0168->U+0169, U+0169, U+016A->U+016B, U+016B, U+016C->U+016D, U+016D, \
U+016E->U+016F,U+016F, U+0170->U+0171, U+0171, U+0172->U+0173, U+0173, U+0174->U+0175,\
U+0175,U+0176->U+0177, U+0177, U+0178->U+00FF, U+00FF, U+0179->U+017A, U+017A, \
U+017B->U+017C,U+017C, U+017D->U+017E, U+017E, U+0410..U+042F->U+0430..U+044F, \
U+0430..U+044F,U+05D0..U+05EA, U+0531..U+0556->U+0561..U+0586, U+0561..U+0587, \
U+0621..U+063A, U+01B9,U+01BF, U+0640..U+064A, U+0660..U+0669, U+066E, U+066F, \
U+0671..U+06D3, U+06F0..U+06FF,U+0904..U+0939, U+0958..U+095F, U+0960..U+0963, \
U+0966..U+096F, U+097B..U+097F,U+0985..U+09B9, U+09CE, U+09DC..U+09E3, U+09E6..U+09EF, \
U+0A05..U+0A39, U+0A59..U+0A5E,U+0A66..U+0A6F, U+0A85..U+0AB9, U+0AE0..U+0AE3, \
U+0AE6..U+0AEF, U+0B05..U+0B39,U+0B5C..U+0B61, U+0B66..U+0B6F, U+0B71, U+0B85..U+0BB9, \
U+0BE6..U+0BF2, U+0C05..U+0C39,U+0C66..U+0C6F, U+0C85..U+0CB9, U+0CDE..U+0CE3, \
U+0CE6..U+0CEF, U+0D05..U+0D39, U+0D60,U+0D61, U+0D66..U+0D6F, U+0D85..U+0DC6, \
U+1900..U+1938, U+1946..U+194F, U+A800..U+A805,U+A807..U+A822, U+0386->U+03B1, \
U+03AC->U+03B1, U+0388->U+03B5, U+03AD->U+03B5,U+0389->U+03B7, U+03AE->U+03B7, \
U+038A->U+03B9, U+0390->U+03B9, U+03AA->U+03B9,U+03AF->U+03B9, U+03CA->U+03B9, \
U+038C->U+03BF, U+03CC->U+03BF, U+038E->U+03C5,U+03AB->U+03C5, U+03B0->U+03C5, \
U+03CB->U+03C5, U+03CD->U+03C5, U+038F->U+03C9,U+03CE->U+03C9, U+03C2->U+03C3, \
U+0391..U+03A1->U+03B1..U+03C1,U+03A3..U+03A9->U+03C3..U+03C9, U+03B1..U+03C1, \
U+03C3..U+03C9, U+0E01..U+0E2E,U+0E30..U+0E3A, U+0E40..U+0E45, U+0E47, U+0E50..U+0E59, \
U+A000..U+A48F, U+4E00..U+9FBF,U+3400..U+4DBF, U+20000..U+2A6DF, U+F900..U+FAFF, \
U+2F800..U+2FA1F, U+2E80..U+2EFF,U+2F00..U+2FDF, U+3100..U+312F, U+31A0..U+31BF, \
U+3040..U+309F, U+30A0..U+30FF,U+31F0..U+31FF, U+AC00..U+D7AF, U+1100..U+11FF, \
U+3130..U+318F, U+A000..U+A48F,U+A490..U+A4CF
min_prefix_len = 0 #最小前缀
min_infix_len = 1 #最小中缀
ngram_len = 1 # 对于非字母型数据的长度切割

#加上这个选项,则会对每个中文,英文字词进行分割,速度会慢
#ngram_chars = U+4E00..U+9FBF, U+3400..U+4DBF, U+20000..U+2A6DF, U+F900..U+FAFF,\
#U+2F800..U+2FA1F, U+2E80..U+2EFF, U+2F00..U+2FDF, U+3100..U+312F, U+31A0..U+31BF,\
#U+3040..U+309F, U+30A0..U+30FF, U+31F0..U+31FF, U+AC00..U+D7AF, U+1100..U+11FF,\
#U+3130..U+318F, U+A000..U+A48F, U+A490..U+A4CF

}

######### 索引器配置 #####
indexer
{
mem_limit = 256M ####### 内存限制
}

############ sphinx 服务进程 ########
searchd
{
#listen = 9312 ### 监听端口,在此版本开始,官方已在IANA获得正式授权的9312端口,以前版本默认的是3312

log = /usr/local/sphinx/var/log/searchd.log #### 服务进程日志 ,一旦sphinx出现异常,基本上可以从这里查询有效信息,轮换(rotate)出的问题一般可在此寻到答案
query_log = /usr/local/sphinx/var/log/query.log ### 客户端查询日志,笔者注:若欲对一些关键词进行统计,可以分析此日志文件
read_timeout = 5 ## 请求超时
max_children = 30 ### 同时可执行的最大searchd 进程数
pid_file = /usr/local/sphinx/var/log/searchd.pid #######进程ID文件
max_matches = 1000 ### 查询结果的最大返回数
seamless_rotate = 1 ### 是否支持无缝切换,做增量索引时通常需要
}
3.3、建立索引文件

[root@localhost sphinx]# bin/indexer -c etc/sphinx.conf article ### 建立索引文件的命令
Sphinx 0.9.9-release (r2117)
Copyright (c) 2001-2009, Andrew Aksyonoff

using config file ‘etc/sphinx.conf’…
indexing index ‘article’…
collected 1000 docs, 0.2 MB
sorted 0.4 Mhits, 99.6% done
total 1000 docs, 210559 bytes
total 3.585 sec, 58723 bytes/sec, 278.89 docs/sec
total 2 reads, 0.031 sec, 1428.8 kb/call avg, 15.6 msec/call avg
total 11 writes, 0.032 sec, 671.6 kb/call avg, 2.9 msec/call avg
[root@localhost sphinx]#
出现以上代表已经索引成功,若不成功的话请根据提示的错误修改配置文件,或到这里提问,我看到后会尽快解决
4.应用
4.1 在CLI上测试

在上一步中,我们建立了索引,现在我们对刚建立的索引进行测试。测试有两种方式:CLI端和API调用

在CLI端上命令测试是使用sphinx自带的搜索命令:search

###### 在article索引上检索 “北京”关键词 ########
[root@localhost sphinx]# bin/search -c etc/sphinx.conf 北京
Sphinx 0.9.9-release (r2117)
Copyright (c) 2001-2009, Andrew Aksyonoff

using config file ‘etc/sphinx.conf’…
index ‘article’: query ‘北京 ‘: returned 995 matches of 995 total in 0.008 sec

displaying matches:
1. document=76, weight=2, cat_id=1, member_id=2, created=Sat Jan 23 19:05:09 2010
id=76
title=??????????
cat_id=1
member_id=2
content=????????????????????????????????
created=1264244709
2. document=85, weight=2, cat_id=1, member_id=2, created=Sat Jan 23 19:05:09 2010
id=85
title=????????????
cat_id=1
member_id=2
content=???????????????????????????????????????????????????????????
created=1264244709
…..这里省略….
20. document=17, weight=1, cat_id=1, member_id=2, created=Sat Jan 23 19:05:09 2010
id=17
title=????????????
cat_id=1
member_id=2
content=??????????????????????????????????????????????????????????
created=1264244709

words:
1. ‘北京’: 995 documents, 999 hits

至此,可以看到,我们已经检索出所有有关“北京”的信息

注意:这里我使用的是putty的客户端,在客户端编码设置的是utf-8,这个是测试的前提条件
4.2 API调用

在本例中,我使用PHP的api来测试,在测试前,先启动sphinx服务进程,并对centos的防火墙做好9312端口的开放

[root@localhost sphinx]# bin/searchd -c etc/sphinx.conf & ### 使sphinx在后台运行
[1] 5759
[root@localhost sphinx]# Sphinx 0.9.9-release (r2117)
Copyright (c) 2001-2009, Andrew Aksyonoff

using config file ‘etc/sphinx.conf’…
listening on all interfaces, port=9312

[1]+ Done bin/searchd -c etc/sphinx.conf

php测试代码:



SetServer(’192.168.1.150′, 9312); //注意这里的主机
#$cl->SetMatchMode(SPH_MATCH_EXTENDED); //使用多字段模式
//dump($cl);
$index=”article”;
$res = $cl->Query($keyword, $index);
$err = $cl->GetLastError();
dump($res);
function dump($var)
{
echo ‘

';  var_dump($var);  echo '

‘;
}
?>

检索“北京”dump后的结果是如下:

array(10) {
["error"]=>
string(0) “”
["warning"]=>
string(0) “”
["status"]=>
int(0)
["fields"]=>
array(2) {
[0]=>
string(5) “title”
[1]=>
string(7) “content”
}
["attrs"]=>
array(3) {
["cat_id"]=>
int(1)
["member_id"]=>
int(1)
["created"]=>
int(2)
}
["matches"]=>
array(20) {
[76]=>
array(2) {
["weight"]=>
string(1) “2″
["attrs"]=>
array(3) {
["cat_id"]=>
string(1) “1″
["member_id"]=>
string(1) “2″
["created"]=>
string(10) “1264244709″
}
}
…..这里省略…..
[17]=>
array(2) {
["weight"]=>
string(1) “1″
["attrs"]=>
array(3) {
["cat_id"]=>
string(1) “1″
["member_id"]=>
string(1) “2″
["created"]=>
string(10) “1264244709″
}
}
}
["total"]=>
string(3) “995″
["total_found"]=>
string(3) “995″
["time"]=>
string(5) “0.008″
["words"]=>
array(1) {
["北京"]=>
array(2) {
["docs"]=>
string(3) “995″
["hits"]=>
string(3) “999″
}
}
}

至此PHP已可调用出结果!


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值