php中文全文搜索引擎-coreseek

项目需要用到全文搜索引擎,最终选择了coreseek,coreseek是一个基于sphinx的中文全文搜索引擎项目,而单纯的sphinx是不支持中文分词的。

coreseek项目地址:http://www.coreseek.com/    最新版本为3.2.13

 

选择coreseek的原因很简单:

1、很好的中文分词支持

2、文档支持

3、易于安装和配置

4、负载低、响应迅速

5、丰富的搜索结果展现方式(未用到,应该不算)

 

coreseek的不足:

最新的3.2.13版本的sphinx是0.9.9版本的,不支持实时索引,考虑升级sphinx到1.10。

安装

Linux下的安装,请参见 http://www.coreseek.cn/products-install/install_on_bsd_linux/

 

配置

我们所用到的配置很简单,仅仅是对商品名称、属性、说明等文字进行全文索引,并且根据命中率进行排序,下面仅列出部分配置文件内容:

 

source mysql
{
        type                                    = mysql

        sql_host                                =127.0.0.1
        sql_user                                =user
        sql_pass                                =pass
        sql_db                                  =db_name
        sql_port                                =3306
	sql_query_pre		            = SET NAMES utf8

        sql_query                               = SELECT .. from ..
}

index goods
{
        source                  = mysql
        path                    = /usr/local/coreseek/var/data/mysql
        docinfo                 = extern
        mlock                   = 0
        morphology              = none
        min_word_len            = 2
        html_strip                              = 1
        charset_dictpath = /usr/local/mmseg3/etc/
        charset_type            = zh_cn.utf-8
}

... ...

使用

生成索引

/usr/local/coreseek/bin/indexer -c /data0/coreseek/csft.conf --all

 

启动服务

/usr/local/coreseek/bin/searchd -c /data0/coreseek/csft.conf

 

停止服务

/usr/local/coreseek/bin/searchd -c /data0/coreseek/csft.conf --stop

 

php使用

        include('sphinxapi.php');
        $mode = SPH_MATCH_ANY;
        $index = "goods";//索引名称
        $sortMode = SPH_SORT_RELEVANCE;
        $ranker = SPH_RANK_PROXIMITY_BM25;
        $cl = new SphinxClient();
        $cl->SetMatchMode ( $mode );
        $cl->SetRankingMode ( $ranker );
        $cl->SetArrayResult ( true );
        $cl->SetSortMode($sortMode);
        $res    = $cl->Query($keywords, $index);
        if($res) {
            if(!empty($res['matches'])){
                foreach($res['matches'] as $v) {
                    //do something with query result items
                }
            }
        }

你可能感兴趣的:(php)