设为首页 收藏本站
查看: 1189|回复: 0

[经验分享] Sphinx+Mysql+中文分词安装-实现中文全文搜索

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-2-12 09:27:31 | 显示全部楼层 |阅读模式
1、什么是Sphinx
Sphinx 是一个在GPLv2 下发布的一个全文检索引擎,商业授权(例如, 嵌入到其他程序中)
需要联系我们(Sphinxsearch.com)以获得商业授权。
一般而言,Sphinx是一个独立的搜索引擎,意图为其他应用提供高速、低空间占用、高结果
相关度的全文搜索功能。Sphinx可以非常容易的与SQL数据库和脚本语言集成。
当前系统内置MySQL和PostgreSQL 数据库数据源的支持,也支持从标准输入读取特定格式
的XML数据。通过修改源代码,用户可以自行增加新的数据源(例如:其他类型的DBMS
的原生支持)。
搜索API支持PHP、Python、Perl、Rudy和Java,并且也可以用作MySQL存储引擎。搜索
API非常简单,可以在若干个小时之内移植到新的语言上。
Sphinx 是SQL Phrase Index的缩写,但不幸的和CMU的Sphinx项目重名



2、Sphinx的特性

高速的建立索引(在当代CPU上,峰值性能可达到10 MB/秒);
?

高性能的搜索(在2 – 4GB 的文本数据上,平均每次检索响应时间小于0.1秒);
?

可处理海量数据(目前已知可以处理超过100 GB的文本数据, 在单一CPU的系统上可
处理100 M 文档);
?

提供了优秀的相关度算法,基于短语相似度和统计(BM25)的复合Ranking方法;
?

支持分布式搜索;
?

provides document exceprts generation;
?

可作为MySQL的存储引擎提供搜索服务;
?

支持布尔、短语、词语相似度等多种检索模式;
?

文档支持多个全文检索字段(最大不超过32个);
?

文档支持多个额外的属性信息(例如:分组信息,时间戳等);
?

停止词查询;
?

支持单一字节编码和UTF-8编码;
?

原生的MySQL支持(同时支持MyISAM 和InnoDB );
?

原生的PostgreSQL 支持.




一、安装所需文件mmseg-0.7.3.tar.gz 中文分词
http://www.coreseek.com/uploads/sources/mmseg-0.7.3.tar.gz

mysql-5.1.26-rc.tar.gz mysql-5.1.26源代码



sphinx-0.9.8-rc2.tar.gz sphinx-0.9.8-rc2源代码
http://www.sphinxsearch.com/downloads/sphinx-0.9.8-rc2.tar.gz

mmseg   
Coreseek COS(tm) MM Segment 1.0   
Copyright By Coreseek.com All Right Reserved.   
Usage: mmseg <option> <file>   
-u <unidict>           Unigram Dictionary   
-r           Combine with -u, used a plain text build Unigram Dictionary, default Off   
-b <Synonyms>           Synonyms Dictionary   
-h            print this help and exit  




fix-crash-in-excerpts.patch sphinx支持分词补丁
http://www.coreseek.com/uploads/ ... h-in-excerpts.patch



sphinx-0.98rc2.zhcn-support.patch sphinx支持分词补丁
http://www.coreseek.com/uploads/ ... .zhcn-support.patch



二、开始安装
1.安装libmmseg

安装mmseg完成,测试一下

tar -zxvf mmseg-0.7.3.tar.gz   
cd mmseg-0.7.3   
./configure --prefix=/usr/local/mmseg   
make   
make install   
cd ..  




有问题可以尝试执行下面命令


echo '/usr/local/mmseg/lib' >> /etc/ld.so.conf   
ldconfig -v   
ln -s /usr/local/mmseg/bin/mmseg /bin/mmseg  




2.重新编译mysql
在安装之前先打两个补丁,这个是支持中文必须打的补丁


tar -zxvf sphinx-0.9.8-rc2.tar.gz   
cd sphinx-0.9.8   
patch -p1 < ../sphinx-0.98rc2.zhcn-support.patch   
patch -p1 < ../fix-crash-in-excerpts.patch  




3.安装sphinx

cd /root/lemp/sphinx-0.9.8-rc2   
./configure --prefix=/usr/local/sphinx --with-mysql=/opt/mysql /   
--with-mysql-includes=/opt/mysql/include/mysql --with-mysql-libs=/opt/mysql/lib/mysql /   
--with-mmseg-includes=/usr/local/mmseg/include --with-mmseg-libs=/usr/local/mmseg/lib --with-mmseg   
make  

tokenizer_zhcn.cpp:1:30: SegmenterManager.h: 没有那个文件或目录   
tokenizer_zhcn.cpp:2:23: Segmenter.h: 没有那个文件或目录  

make clean   
./configure --prefix=/usr/local/sphinx --with-mysql=/opt/mysql /   
--with-mysql-includes=/usr/local/mysql/include/mysql --with-mysql-libs=/opt/mysql/lib/mysql /   
--with-mmseg-includes=/usr/local/mmseg/include/mmseg --with-mmseg-libs=/usr/local/mmseg/lib --with-mmseg  
/root/sphinx/sphinx-0.9.8-rc2/src/tokenizer_zhcn.cpp:34: undefined reference to `libiconv_close'   
collect2: ld returned 1 exit status  




官网解决办法:
In the meantime I've change the configuration file and set
#define USE_LIBICONV 0 in line 8179.
修改configure 文件把 #define USE_LIBICONV 0 最后的数值由1改为0
重新编译。

make clean   
./configure --prefix=/usr/local/sphinx --with-mysql=/opt/mysql /   
--with-mysql-includes=/usr/local/mysql/include/mysql --with-mysql-libs=/usr/local/mysql/lib/mysql /   
--with-mmseg-includes=/usr/local/mmseg/include/mmseg --with-mmseg-libs=/usr/local/mmseg/lib --with-mmseg  




vi configure
输入/define USE_LIBICONV 找到目标行
按i键后将1改成0,按esc,输入:wq保存退出


make   
make install  


cd /usr/local/sphinx/etc   
cp sphinx.conf.dist sphinx.conf  






4.配置sphinx



修改/usr/local/sphinx/etc/sphinx.conf



type = mysql   
# some straightforward parameters for SQL source types   
sql_host = localhost   
sql_user = root   
sql_pass =   
sql_db = test   
sql_port = 3306 # optional, default is 3306  
address = 127.0.0.1 #安全点可以只监听本机  




5.索引建立



装好sphinx后在sphinx的目录中有三个目录 分别为bin etc var
bin中 存有sphinx用到的一些执行文件 包括 indexer 索引建立 search 查询工具 searchd 查询服务器 等

为了下面测试方便,我们先将sphinx.conf自带的example.sql脚本倒入的mysql中
//创建了一个test库,并建立了documents测试表和数据



/usr/local/mysql/bin/mysql -uroot -p < /usr/local/sphinx/etc/example.sql  






索引的建立方法为



/usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf test1  




test1为资源名称 不写为默认所有都建立索引
附:
建立索引期间可能由于不同版本的数据库导致indexer找不到共享库libmysqlclient.so.16
需要把/opt/mysql/lib/mysql/libmysqlclient.so.16.0.0 这个文件复制到/usr/lib下 或者作软连接即可



6.查询服务器



/usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf 为开启

/usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf --stop 为关闭

sphinx的查询 可以大致分为三种

1 数据库引擎中的查询

2 通过search工具查询
/usr/local/sphinx/bin/search --config /usr/local/sphinx/etc/sphinx.conf test

3 通过php的接口查询 详见sphinxapi.php

三、在mysql中的使用SphinxSE方式调用Sphinx
1.在mysql中的使用SphinxSE方式调用Sphinx
首先建立一张索引专用表:
CREATE TABLE `sphinx` (     
`id` int(11) NOT NULL,     
`weight` int(11) NOT NULL,     
`query` varchar(255) NOT NULL,     
`CATALOGID` INT NOT NULL,     
`EDITUSERID` INT NOT NULL,     
`HITS` INT NULL,     
`ADDTIME` INT NOT NULL,   KEY   
`Query` (`Query`)   
) ENGINE=SPHINX DEFAULT CHARSET=utf8 CONNECTION='sphinx://localhost:3312/test1'  


test1:索引的名称,可以在sphinx.conf中查找到
建立完索引专用表后,我们就可以在mysql中使用它了,例如,在mysql中输入

SELECT doc. *  FROM documents doc JOIN sphinx ON ( doc.id = sphinx.id ) WHERE query = 'doc;mode= any'  










运行后,结果记录中将会显示包含doc字符串的所用记录行

关于query的语法,还有sphinx的配置等信息你可以请参考:
http://www.sphinxsearch.com/doc.html





2.中文分词应用
生成词典
进入到mmseg的源代码目录后



cd data   
mmseg -u unigram.txt  


在data下边会产生一个文件unigram.txt.uni
这个就是生成的字典 然后把字典改名为uni.lib放在可以读取的目录中即可


cp unigram.txt.uni /usr/local/sphinx/uni.lib  




修改 sphinx.conf(/usr/local/sphinx/etc/sphinx.conf)配置文件
在索引中加入



charset_type = zh_cn.utf-8   
charset_dictpath = /usr/local/sphinx/  



INSERT INTO `test`.`documents` (   
`id` ,   
`group_id` ,   
`group_id2` ,   
`date_added` ,   
`title` ,   
`content`   
)   
VALUES (   
NULL , '3', '9', NOW( ) , 'sphinx中文搜索', 'Sphinx是一个基于SQL的全文检索引擎,可以结合MySQL,PostgreSQL做全文搜索,它可以提供比数据库本身更专业的搜索功能,使得应用程序更容易实现专业化的全文检索。Sphinx特别为一些脚本语言设计搜索API接口,如PHP,Python,Perl,Ruby等,同时为MySQL也设计了一个存储引擎插件。 '   
);  




(如果searchd已经再运行,先kill 它再运行)
注意:在添加完数据后,需要重新加载索引,这样新的数据才能被缓存进去
重建索引,成功后,开启索引监听
/usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf --all   
/usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf  


在ide硬盘上建索引的速度
indexing index 'test1'...   
collected 423228 docs, 637.2 MB   
sorted 125.5 Mhits, 100.0% done   
total 423228 docs, 637201412 bytes   
total 753.401 sec, 845766.13 bytes/sec, 561.76 docs/sec  




这样,就可以在phpmyadmin中进行测试了。
SELECT doc. *  FROM documents doc JOIN sphinx ON ( doc.id = sphinx.id )   
WHERE query = '设计;mode= any '  






好像没出来。。。。

修改 sphinx.conf
去掉下面的注释
sql_query_pre = SET NAMES utf8  






再重启searchd就可以搜出结果了



使用service 控制sphinx



先增加一sphinx用户并属于website组(事先已存在),并修改sphinx目录的属主
useradd -d /usr/local/sphinx -g website -s /sbin/nologin sphinx   
chown -R sphinx:website /usr/local/sphinx  




创建/etc/init.d/sphinx脚本
#!/bin/sh   
# sphinx: Startup script for Sphinx search   
#   
# chkconfig: 345 86 14   
# description:  This is a daemon for high performance full text /   
#               search of MySQL and PostgreSQL databases. /   
#               See http://www.sphinxsearch.com/ for more info.   
#   
# processname: searchd   
# pidfile: $sphinxlocation/var/log/searchd.pid   

# Source function library.   
. /etc/rc.d/init.d/functions   

processname=searchd   
servicename=sphinx   
username=sphinx   
sphinxlocation=/usr/local/sphinx   
pidfile=$sphinxlocation/var/log/searchd.pid   
searchd=$sphinxlocation/bin/searchd   

RETVAL=0   

PATH=$PATH:$sphinxlocation/bin   

start() {   
    echo -n $"Starting Sphinx daemon: "   
    daemon --user=$username --check $servicename $processname   
    RETVAL=$?   
    echo   
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$servicename   
}   

stop() {   
    echo -n $"Stopping Sphinx daemon: "   

    $searchd --stop   
    #killproc -p $pidfile $servicename -TERM   
    RETVAL=$?   
    echo   
    if [ $RETVAL -eq 0 ]; then   
        rm -f /var/lock/subsys/$servicename   
        rm -f $pidfile   
    fi   
}   

# See how we were called.   
case "$1" in   
    start)   
        start   
        ;;   
    stop)   
        stop   
        ;;   
    status)   
        status $processname   
        RETVAL=$?   
        ;;   
    restart)   
        stop   
sleep 3   
        start   
        ;;   
    condrestart)   
        if [ -f /var/lock/subsys/$servicename ]; then   
            stop   
    sleep 3   
            start   
        fi   
        ;;   
    *)   
        echo $"Usage: $0 {start|stop|status|restart|condrestart}"   
        ;;   
esac   
exit $RETVAL  


修改权限并加入到service,机器启动时自动启动
chmod 755 /etc/init.d/sphinx   
chkconfig --add sphinx   
chkconfig --level 345 sphinx on   
chkconfig --list|grep sphinx #检查下  
service sphinx start #运行   
service sphinx stop  #停止,官方的脚本在我的as4上有点问题,所以粗鲁的改了下   
service sphinx restart #重启   
service sphinx status #查看是否运行  
#检查下已用sphinx用户运行  
ps aux |grep searchd   
sphinx   24612  0.0  0.3 11376 6256 pts/1    S    14:07   0:00 searchd  


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-14889-1-1.html 上篇帖子: MySQL5.6新特性之GTID、多线程复制 下篇帖子: mysql的字符编码 中文
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表