Coreseek + Sphinx + Mysql + PHP構建中文檢索引擎


首先明確幾個概念

Sphinx是開源的搜索引擎,它支持英文的全文檢索。所以如果單獨搭建Sphinx,你就已經可以使用全文索引了。但是往往我們要求的是中文索引,怎么做呢?國人提供了一個可供企業使用的,基於Sphinx的中文全文檢索引擎。也就是說Coreseek實際上的內核還是Sphinx。那么他們的版本對應呢?

 

Coreseek發布了3.2.14版本和4.1版本,其中的3.2.14版本是2010年發布的,它是基於Sphinx0.9.9搜索引擎的。而4.1版本是2011年發布的,它是基於Sphinx2.0.2的。Sphinx從0.9.9到2.0.2還是有改變了很多的,有很多功能,比如sql_attr_string等是在0.9.9上面不能使用的。所以在安裝之前請判斷清楚你需要安裝的是哪個版本,在google問題的時候也要弄清楚這個問題的問題和答案是針對哪個版本的。我個人強烈建議使用4.1版本。

 

網上有一篇文章說的是Sphinx和Coreseek是怎么安裝的,其中它的coreseek安裝這部分使用coreseek-4.1來替換就可以使用了。

 

詳細步驟看上面篇文章就理解了,這里說一下我在安裝過程中遇到的幾個問題:

安裝mmseg的時候,./configure出現錯誤:config.status: error: cannot find input file: src/Makefile.in

這個時候需要先運行下automake

結果我運行的時候竟然提示automake的版本不對

所以這個時候,你可能需要去官網下個對應的版本(有可能是需要老版本)再來運行

在安裝csrf的時候,文檔提示需要指定mysql,但是我的mysql是yum安裝的,找不到安裝路徑

./configure 

--prefix=/usr/local/coreseek --with-mysql=/usr/local/mysql

 --with-mmseg=/usr/local/mmseg --with-mmseg-includes=/usr/local/mmseg/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg/lib/

 

 

yum安裝的mysql的include和libs文件夾一般是安裝在/usr/include/mysql和/usr/lib64/mysql下面

所以這里的--with-mysql可以使用--with-mysql-includes和--with-mysql-libs來進行替換。

./configure 

--prefix=/usr/local/coreseek --with-mysql-includes=/usr/includes/mysql --with-mysql-libs=/usr/lib64/mysql/

 --with-mmseg=/usr/local/mmseg --with-mmseg-includes=/usr/local/mmseg/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg/lib/

 

配置文件提示unknown key: sql_attr_string

如上文,就需要檢查下自己的sphinx版本了

如何安裝php的sphinx擴展

可以在這里(http://pecl.php.net/package/sphinx)找到sphinx的php擴展源碼

注意,使用phpize,configure的時候可能會要求要安裝libsphinxclient,它在coreseek-4.1-beta/csft-4.1/api/libsphinxclient/里面能找到,編譯安裝它以后就可以configure,make,生成動態so文件了。

如何配置sphinx.conf配置文件

最復雜的部分就是sphinx.conf配置文件的配置了,里面的注釋代碼非常多,我建議使用的時候把注釋代碼去掉,我貼出自己使用的最簡單的一個成功的配置文件:

source src1
{
        type                    = mysql

        sql_host                = localhost
        sql_user                = yejianfeng
        sql_pass                = test
        sql_db                  = mysite
        sql_port                = 3306  # optional, default is 3306

        sql_query_pre           = SET NAMES utf8
        sql_query_pre           = SET SESSION query_cache_type=OFF

        sql_query               = select id, id AS id_new,name, name AS name_query,descr, descr AS descr_query,city FROM account
        sql_attr_string = name
        sql_attr_string = descr

        sql_query_info          = SELECT * FROM account WHERE id=$id
}

source src1throttled : src1
{
        sql_ranged_throttle     = 100
}

index test1
{
        source                  = src1
        path                    = /home/yejianfeng/instance/coreseek/var/data/test1
        docinfo                 = extern
        mlock                   = 0
        morphology              = none
        min_word_len            = 1
        charset_type = zh_cn.utf-8
        charset_dictpath  = /home/yejianfeng/instance/mmseg/etc/
        html_strip              = 0
}



indexer
{
        mem_limit               = 256M
}

searchd
{
        listen                  = 9312
        listen                  = 9306:mysql41

        log                     = /home/yejianfeng/instance/coreseek/var/log/searchd.log
        query_log               = /home/yejianfeng/instance/coreseek/var/log/query.log
        read_timeout            = 5
        client_timeout          = 300
        max_children            = 30
        pid_file                = /home/yejianfeng/instance/coreseek/var/log/searchd.pid
        max_matches             = 1000
        seamless_rotate         = 1
        preopen_indexes         = 1
        unlink_old              = 1
        mva_updates_pool        = 1M
        max_packet_size         = 8M
        max_filters             = 256
        max_filter_values       = 4096
}

php調用SphinxClient的例子如下:

首先要確保已經啟動了searchd

[yejianfeng@AY130416142121702aac etc]$ ps aux|grep searchd
501      30897  0.0  0.0  60824  1396 pts/2    S    17:19   0:00 /home/yejianfeng/instance/coreseek/bin/searchd -c /home/yejianfeng/instance/coreseek/etc/sphinx.conf
501      30999  0.0  0.0 103232   856 pts/2    S+   18:10   0:00 grep searchd

php提供的調用SphinxClient的接口

<?php
$s = new SphinxClient;
$s->setServer("localhost", 9312);
$s->setArrayResult(true);
$s->setSelect();
$s->setMatchMode(SPH_MATCH_ALL);

$result = $s->query('美女', 'test1');
print_r($result);

參考文章:

http://www.coreseek.cn/docs/coreseek_4.1-sphinx_2.0.1-beta.html

http://www.coreseek.cn/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM