阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Sphinx安装配置应用

141次阅读
没有评论

共计 4604 个字符,预计需要花费 12 分钟才能阅读完成。

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 数据秒查  http://www.linuxidc.com/Linux/2015-04/116679.htm

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-06/132319.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计4604字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中