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

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

Sphinx 详细介绍可参考sphinx 中文站

本文以 wget http://files.opstool.com/man/coreseek-4.1-beta.tar.gz 为安装版本

遇到Makefile.in错误执行以下操作
aclocal
libtoolize –force
automake –add-missing
autoconf
autoheader
make clean

第一步:安装升级autoconf

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

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

第二步:安装mmseg(coreseek所使用的词典)安装的过程一定要注意是否是在当前目录

tar -zxvf coreseek-4.1-beta.tar.gz
cd coreseek-4.1-beta
cd mmseg-4.1-beta/
./bootstrap    #输出的warning信息可以忽略,如果出现error则需要解决
./configure --prefix=/usr/local/mmseg3
make
make install

# 检测是否安装完成
mmseg
提示:-bash: mmseg: command not found

# 问题解决方法
ln -s /usr/local/mmseg3/bin/mmseg /bin/mmseg

# 再测试下
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
-t <thesaurus>          Thesaurus Dictionary
-h            print this help and exit
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

第三步:安装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/ \

make
make install

测试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.txt
vim etc/mmseg.ini

[mmseg]
merge_number_and_ascii=0;    ;合并英文和数字 abc123/x
number_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
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

第五步:配置sphinx

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

 
 
  • 1
  • 2
  • 3
  • 4

sphinx.conf 官方最简配置文件内容

配置的数据库文件:/usr/local/sphinx/etc/example.sql

source src1
{
        type                    = mysql
        sql_host                = localhost
        sql_user                = test
        sql_pass                =
        sql_db                  = test
        sql_port                = 3306  # optional, default is 3306
        sql_query               = \
                SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
                FROM documents
        sql_attr_uint           = group_id   #搜索后显示的int字段
        sql_attr_timestamp      = date_added #搜索后显示的时间戳字段
        sql_query_info          = SELECT * FROM documents WHERE id=$id
}
index test1
{
        source                  = src1
        path                    = /usr/local/sphinx/var/data/test1
        docinfo                 = extern
        charset_type            = sbcs
}
index testrt  # 索引的配置
{
        type                    = rt
        rt_mem_limit            = 32M
        path                    = /usr/local/sphinx/var/data/testrt
        charset_type            = utf-8
        rt_field                = title
        rt_field                = content
        rt_attr_uint            = gid
}
indexer  索引的内存
{
        mem_limit               = 32M
}
searchd  # 守护进程
{
        listen                  = 9312
        listen                  = 9306:mysql41
        log                     = /usr/local/sphinx/var/log/searchd.log
        query_log               = /usr/local/sphinx/var/log/query.log
        read_timeout            = 5
        max_children            = 30
        pid_file                = /usr/local/sphinx/var/log/searchd.pid
        max_matches             = 1000
        seamless_rotate         = 1
        preopen_indexes         = 1
        unlink_old              = 1
        workers                 = threads # for RT to work
}

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

# 结束所有索引
#pkill -9 search

1、#停止正在运行的searchd
/usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/conf.d/sphinx.conf  --stop

2、# 创建索引
# /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

4、# 合并索引跟增量索引
# /usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/conf.d/sphinx.conf --merge baby_idx baby_merge --rotate


# 错误解决
# 解决:复制Mysql客户端文件到所有用户可以放的目录去
cp /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/

# 64位系统需要在创建一个软连接
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
服务器所用MySQL在当时编译时并没有编译Sphinx扩展,而重新编译mysql并加入Sphinx暂时又无法实现(项目用到了多台服务器,在不影响现有业务的情况下不可能去重新编译MySQL的),所以采用的是程序通过API来外部调用Sphinx.Sphinx自带的API有PHP,Python,Ruby,Java等众多版本,所以基本也够用了,本人使用的编程语言是php所以下文的条用示例采用的是PHP版的API. 
 
 
  • 1

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

第六步:安装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/libsphinxclient
make
make install
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

第七步:安装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=tgz

tar -zxvf sphinx-1.3.0.tgz
cd 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目录下,有份测试数据和配置的样本
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

第八步:配置PHP支持Sphinx

编辑php.ini文件

vim /usr/local/php/etc/php.ini

# 修改以下内容
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/"

# 增加扩展到php
extension = sphinx.so

# 重新启动php-fpm
/etc/init.d/php-fpm restart
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

vim index.php

# 写入一下内容:
<?php
echo phpinfo();
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

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

第九步(简易版):php使用sphinx

$s = new SphinxClient;
$a = $s->setServer("127.0.0.1", 9312);
$res = $s->query("体验版抢先试用",'test1');

查询结果:

Array
(
    [error] => 
    [warning] => 
    [status] => 0
    [fields] => Array
        (
            [0] => title
        )

    [attrs] => Array
        (
            [lv] => 1
            [fansnum] => 1
            [visible] => 1
            [lon] => 5
            [lat] => 5
            [show_reg_client] => 1073741825
        )

    [total] => 0
    [total_found] => 0
    [time] => 0.002
    [words] => Array
        (
            [体验] => Array
                (
                    [docs] => 114
                    [hits] => 114
                )

            [版] => Array
                (
                    [docs] => 1279
                    [hits] => 1280
                )

            [抢先] => Array
                (
                    [docs] => 0
                    [hits] => 0
                )

            [试用] => Array
                (
                    [docs] => 4
                    [hits] => 4
                )

        )

)

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





参考:
sphinx中文站
博客参考
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值