站内全文搜索引擎 Sphinx/coreseek 安装使用教程

来源:互联网 发布:大连育知同创怎么样 编辑:程序博客网 时间:2024/05/17 20:03
Sphinx是开源的搜索引擎,它支持英文的全文检索。所以如果单独搭建Sphinx,你就已经可以使用全文索引了。但是往往我们要求的是中文索引,怎么做呢?国人提供了一个可供企业使用的,基于Sphinx的中文全文检索引擎。也就是说Coreseek实际上的内核还是Sphinx。
sphinx可以通过设置为“一元切分模式”来支持搜索中文在实际使用中,搜索非中文的话,sphinx比coreseek要快;搜索短中文字符串的话,开启了“一元切分模式”的sphinx比coreseek要快;只有在搜索长中文字串时,coreseek的分词优势才能显现,比sphinx要快
    根据你的应用场景来选择用哪个,如果是索引英文、数字、字符较多的数据,就用源生sphinx;如果是索引中文非常多非常长的数据,还是用coreseek吧。Coreseek是基于Sphinx开发的一款软件,对Sphinx做了一些改动,在中文方面支持得比Sphinx好

安装注意
Coreseek发布了3.2.14版本和4.1版本等多个版本

3.2.14版本是2010年发布的,它是基于Sphinx0.9.9搜索引擎的。4.1版本是2011年发布的,它是基于Sphinx2.0.2以后的

本文章以安装 coreseek-4.1-beta 版本为例:
安装包下载地址 :

http://dl.download.csdn.net/down11/20160530/de90462e2cf8350cbd4ad0f758105c9d.gz?response-content-disposition=attachment%3Bfilename%3D%22coreseek-4.1-beta.tar.gz%22&OSSAccessKeyId=9q6nvzoJGowBj4q1&Expires=1474535438&Signature=8jae9H6dIfmd7zHRs72qcgi4Q1k%3D

3.2.14 稳定版(线上环境用此版本)

http://124.202.166.45/file3.data.weipan.cn/76885328/d22215f7ac79a8eff90a6eec0d7ccdc7f86048b6?ip=1490457292,118.144.20.162&ssig=z3aZVW70%2FY&Expires=1490457892&KID=sae,l30zoo1wmz&fn=coreseek-3.2.14.tar.gz&skiprd=2&se_ip_debug=118.144.20.162&corp=2&from=1221134&wshc_tag=0&wsts_tag=58d68e1c&wsid_tag=75647862&wsiphost=ipdbm

第一步:安装升级autoconf

因为coreseek需要autoconf 2.64以上版本,因此需要升级autoconf,不然会报错
安装方法如下:

yum -y install glibc-common libtool autoconf automake mysql-devel expat-devel

第二步:安装mmseg(coreseek所使用的词典)

tar -zxvf coreseek-4.1-beta.tar.gzcd coreseek-4.1-betacd mmseg-4.1-beta/./bootstrap    #输出的warning信息可以忽略,如果出现error则需要解决./configure --prefix=/usr/local/mmseg3makemake install# 检测是否安装完成mmseg提示:-bash: mmseg: command not found# 问题解决方法ln -s /usr/local/mmseg3/bin/mmseg /bin/mmseg# 再测试下mmseg# 显示以下内容表示安装成功Coreseek COS(tm) MM Segment 1.0Copyright 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-t <thesaurus>          Thesaurus Dictionary-h            print this help and exit

第三步:安装csft-4.1-beta

cd csft-4.1-beta/sh buildconf.sh    #输出的warning信息可以忽略,如果出现error则需要解决 ./configure \--prefix=/usr/local/sphinx \--without-unixodbc \--with-mmseg \--with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ \--with-mmseg-libs=/usr/local/mmseg3/lib/ \--with-mysql=/usr/local/mysql/ \makemake install#./configure --prefix=/usr/local/sphinx/ --with-mysql=/usr/local/mysql/ --enable-id64 make make install # 注意1:采用这种方式安装不支持中文分词。# 注意2:--enable-id64 系统是64位,开启后Sphinx存储模块可以支持4-5G,不开启支持2G左右,不开启检索效率会高些,一般情况下建议不开启# 错误:collect2: ld 返回 1 make[2]: *** [indexer] 错误 1 # 修改configure文件中:LIBS="$LIBS -L/usr/local/lib"# 改为:LIBS="$LIBS -liconv -L/usr/local/lib"

测试mmseg分词和coreseek搜索

cd testpack cat var/test/test.xml  #此时应该正确显示中文 /usr/local/mmseg3/bin/mmseg -d /usr/local/mmseg3/etc var/test/test.xml /usr/local/sphinx/bin/indexer -c etc/csft.conf --all /usr/local/sphinx/bin/search -c etc/csft.conf 网络搜索 #此时正确的应该返回 words: 1. '网络': 1 documents, 1 hits 2. '搜索': 2 documents, 5 hits 

第四步:配置mmseg中文分词

目前这里不用操作,安装时已经帮我配置好了

cd /usr/local/mmseg3/# 生成unigram.txt.uni./bin/mmseg -u /usr/local/mmseg/etc/unigram.txtvim etc/mmseg.ini[mmseg]merge_number_and_ascii=0;    ;合并英文和数字 abc123/xnumber_and_ascii_joint=;    ;定义可以连接英文和数字的字符compress_space=1;    ;暂不支持seperate_number_ascii=0;    ;将字母和数字打散# 复制到sphinx/dict目录cp etc/mmseg.ini /usr/local/sphinx/dict/mmseg.ini# 复制到sphinx/dict目录cp etc/unigram.txt /usr/local/sphinx/dict/uni.lib

第五步:配置sphinx

# 创建配置文件mkdir -p /usr/local/sphinx/etc/conf.d/vim /usr/local/sphinx/etc/conf.d/sphinx.conf

sphinx.conf 配置文件内容

#配置数据源,可以有多个source src_blog{    #数据库类型    type = mysql    #是否去掉html标签    strip_html = 0    sql_host = 127.0.0.1    sql_user = root    sql_pass = woshishui    sql_db = yphp_tutiantian    sql_port = 3006    #在执行sql_query前执行的sql命令, 可以有多条    sql_query_pre= SET NAMES utf8    #全文检索要显示的内容,在这里尽可能不使用where或group by,将where与groupby的内容交给sphinx,    #由sphinx进行条件过滤与groupby效率会更高    #注意:select 出来的字段必须至少包括一个唯一主键(ARTICLESID)以及要全文检索的字段,    #你计划原本在where中要用到的字段也要select出来    #这里不用使用orderby    sql_query = SELECT * FROM tu_pic    #开头的表示一些属性字段,你原计划要用在where,orderby,groupby中的字段要在这里定义    #根据我们原先的SQL:    #select * from eht_articles where title like ? and catalogid=? And                edituserid=?  And addtime between ? and ? order by hits desc    #我们需要对catalogid,edituserid,addtime,hits进行属性定义(这四个字段也要在select的字段列表中),    #定义时不同的字段类型有不同的属性名称,具体可以见sphinx.conf.in中的说明    sql_attr_uint= pr    sql_attr_uint = big_id    sql_attr_uint = small_id    sql_attr_uint= uid    sql_attr_uint= is_del    sql_ranged_throttle = 0}# 在这里要注意,rt_field是检索字段,rt_attr_uint是返回字段#定义索引名称index sphinx_blog{    #数据源名    source = src_blog    #索引记录存放目录    path = /usr/local/sphinx/var/data/sphinx_blog    docinfo = extern    mlock = 0    stopwords =    min_prefix_len = 0    min_infix_len = 0    morphology = none    min_word_len = 2    #字符集    charset_type = zh_cn.utf-8    #指明分词法读取词典文件的位置,当启用分词法时,为必填项。在使用LibMMSeg作为分词 库时,    #需要确保词典文件uni.lib在指定的目录下    charset_dictpath = /usr/local/mmseg3/etc/    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    html_strip = 0}#全局index定义indexer{    mem_limit = 256M}#sphinx守护进程配置searchd{    port = 9312    read_timeout = 5    max_children = 10    compat_sphinxql_magics = 0    pid_file = /usr/local/sphinx/var/log/searchd.pid    #全文检索日志    log = /usr/local/sphinx/var/log/searchd.log    #查询日志    query_log = /usr/local/sphinx/var/log/query_log.log    #最大匹配数,也就是查找的数据再多也只返回这里设置的1000条    max_matches = 1000    seamless_rotate = 1}

建立sphinx 索引(若日常更新sphinx数据,只需执行以下1、2、3步即可,可写入sh文件执行)

# 结束所有索引#pkill -9 search1、#停止正在运行的searchd/usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/conf.d/sphinx.conf  --stop2、# 创建索引# /usr/local/sphinx/bin/indexer --c --config /usr/local/sphinx/etc/conf.d/sphinx.conf --all#如果只想对某个数据源进行索引,则可以这样:#/usr/local/sphinx/bin/indexer --c --config /usr/local/sphinx/etc/conf.d/sphinx.conf 索引名称3、# 启动索引# /usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/conf.d/sphinx.conf# 错误解决# 解决:复制Mysql客户端文件到所有用户可以放的目录去cp /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/# 64位系统需要在创建一个软连接ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64
服务器所用MySQL在当时编译时并没有编译Sphinx扩展,而重新编译mysql并加入Sphinx暂时又无法实现(项目用到了多台服务器,在不影响现有业务的情况下不可能去重新编译MySQL的),所以采用的是程序通过API来外部调用Sphinx.Sphinx自带的API有PHP,Python,Ruby,Java等众多版本,所以基本也够用了,本人使用的编程语言是php所以下文的条用示例采用的是PHP版的API. 

以下步骤不需要执行,原因如上:

第六步:安装sphinxclient

php模块依赖于libsphinxclient包

# 进入sphinx 源码包的api目录cd coreseek-4.1/csft-4.1/api/libsphinxclient./configure --prefix=/usr/local/libsphinxclient#若报一下错误config.status: error: cannot find input file: Makefile.in   #报错configure失败      #处理configure报错,运行下列指令再次编译:  aclocal  libtoolize --force  automake --add-missing  autoconf  autoheader  make clean  //重新configure编译  ./configure --prefix=/usr/local/libsphinxclientmakemake install

第七步:安装php的Sphinx扩展

# PHP7以前的版本使用下面扩展wget http://pecl.php.net/get/sphinx-1.3.0.tgz# PHP7 使用下面的扩展wget http://git.php.net/?p=pecl/search_engine/sphinx.git;a=snapshot;h=9a3d08c67af0cad216aa0d38d39be71362667738;sf=tgztar -zxvf sphinx-1.3.0.tgzcd sphinx-1.3.0/usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config --with-sphinx=/usr/local/libsphinxclient/make && make install# 安装好后,在安装目录下etc目录下,有份测试数据和配置的样本

第八步:配置PHP支持Sphinx

编辑php.ini文件

vim /usr/local/php/etc/php.ini# 修改以下内容extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/"# 增加扩展到phpextension = sphinx.so# 重新启动php-fpm/etc/init.d/php-fpm restart

查看Sphinx是否安装成功
新建PHP文件

vim index.php# 写入一下内容:<?phpecho phpinfo();

浏览器输入地址访问
看到sphinx表示扩展安装成功
这里写图片描述

第九步:php使用sphinx

<?php// --------------------------------------------------------------------------// File name   : test_coreseek.php// Description : coreseek中文全文检索系统测试程序// Requirement : PHP5 (http://www.php.net)//// Copyright(C), HonestQiao, 2011, All Rights Reserved.//// Author: HonestQiao (honestqiao@gmail.com)//// 最新使用文档,请查看:http://www.coreseek.cn/products/products-install///// --------------------------------------------------------------------------#若SphinxClient是php默认类,则不需要引入sphinxapi.php#sphinxapi.php文件在/coreseek-3.2.14/testpack/api/ 中require ( "sphinxapi.php" );$cl = new SphinxClient ();$cl->SetServer ( '127.0.0.1', 9312);$cl->SetConnectTimeout ( 3 );$cl->SetArrayResult ( true );$cl->SetMatchMode ( SPH_MATCH_ANY);$cl->setLimits(0,300);$keyword = $_GET['keyword'];// // 执行查询,第一个参数查询的关键字,第二个查询的索引名称,多个索引名称以,分开,也可以用*表示所有索引。$res = $cl->Query ( $keyword, "sphinx_blog" );print_r($res);

查询结果:

Array(    [error] =>     [warning] =>     [status] => 0    [fields] => Array        (            [0] => search_word            [1] => actors            [2] => directors            [3] => wktag            [4] => pinyin            [5] => pinyin_first        )    [attrs] => Array        (            [xid] => 1        )    [matches] => Array        (            [0] => Array                (                    #matches中的id就是指配置文件中sql_query SELECT语句中的第一个字段                    [id] => 33514269                    [weight] => 1                    [attrs] => Array                        (                            [xid] => 33514269                        )                )        )    [total] => 1    [total_found] => 1    [time] => 0.002    [words] => Array        (            [兔先生] => Array                (                    [docs] => 1                    [hits] => 1                )        ))

Matches中就是查询的结果了,但是仿佛不是我们想要的数据,比如titile,content字段的内容就没有查询出来,根据官方的说明是Sphinx并没有连接到MySQL去取数据,只是根据它自己的索引内容进行计算,因此如果想用Sphinx提供的API去取得我们想要的数据,还必须以查询的结果为依据,再次查询MySQL从而得到我们想要的数据。

0 0
原创粉丝点击