Sphinx安装配置应用


Sphinx 是由俄罗斯人Andrew Aksyonoff开发的一个全文搜索引擎。意图为其他应用提供高速、地空间占用、高结果相关度的全文搜索功能。Sphinx可以非常容易的与SQL数据库和脚本语言集成。当前系统内置的MysqL和PostgreSQL数据库数据源的支持,也支持从标准输入读取特定格式的xml数据。通过修改源码,可以自行增加新的数据源(例如:其他类型的DBMS的原生支持)

1、Sphinx中文分词

中文的全文检索是根据语义来分词,目前大多数数据库尚未支持中文全文检索,如Mysql。Sphinx如果需要对中文进行全文检索,也得需要一些插件来补充,比如coreseek和sfc。

  • Coreseek是现在用的最多的sphinx中文全文检索,它提供了为Sphinx设计的中文分词包LibMMSeg 。并提供了多个系统的二进制发行版,其中有rpm,deb及windows下的二进制包。
  • sfc(sphinx-for-chinese)是由网友happy兄提供的另外一个中文分词插件。其中文词典采用的是xdict。据其介绍,经过测试,目前版本在索引速度上(Linux 测试平台)基本上能够达到索引UTF-8英文的一半,即官方宣称速度的一半。(时间主要是消耗在分词上)。

 2、安装

Sphinx在mysql上的应用有两种方式:
(1)采用API调用,如使用PHP、java等的API函数或方法查询。优点是可不必对mysql重新编译,服务端进程“低耦合”,且程序可灵活、方便的调用;缺点是如已有搜索程序的条件下,需修改部分程序。推荐程序员使用。
(2)使用插件方式(sphinxSE)把sphinx编译成一个mysql插件并使用特定的sql语句进行检索。其特点是,在sql端方便组合,且能直接返回数据给客户端不必二次查询(注),在程序上仅需要修改对应的sql,但这对使用框架开发的程序很不方便,比如使用了ORM。另外还需要对mysql进行重新编译,且需要mysql-5.1以上版本支持插件存储。系统管理员可使用这种方式。

采用第一种方式进行安装:

软件环境:

  • 操作系统: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,如有,则安装无误!

sfc 安装

coreseek安装

3、配置

  • [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

}

######### 索引器配置 #####
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 ### 是否支持无缝切换,做增量索引时通常需要
}

4、建索引

[root@localhost sphinx]# bin/indexer -c etc/sphinx.conf article ### 建立索引文件的命令,article替换成 --all可以建配置文件的所有索引
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

5、应用

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

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

[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 'mdmLoginLog': query '刘利 ': returned 6 matches of 6 total in 0.000 sec

displaying matches:
1. document=2, weight=2
2. document=3, weight=2
3. document=4, weight=2
4. document=5, weight=2
5. document=7, weight=2
6. document=8, weight=2

words:
1. '刘': 6 documents, 6 hits
2. '利': 6 documents, 6 hits

(2)使用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

Sphinx+MySQL+PHP 12亿DNS数据秒查 

本文永久更新链接地址

相关内容

    暂无相关文章