这个sphinx版本支持中文分词,是在sphinx的基础上修改而来的,可以手动添加词库,相比 coreseek 省去了安装mmseg,而且coreseek好像已经没人维护了,且最后的版本是基于sphinx-0.9的版本,sphinx已经到3.0的版本了,增加了很多新功能。


安装

安装编译工具:

yum -y install make gcc g++ gcc-c++ libtool autoconf automake imake php-devel mysql-devel libxml2-devel expat-devel

安装mysql:

yum install mysql-server mysql-devel

下载安装包:

https://github.com/chenenkou/sphinx-for-chinese

解压:

bzip2 -d sphinx-for-chinese-2.1.0-dev-r3361.tar.bz2
tar xf sphinx-for-chinese-2.1.0-dev-r3361.tar
cd sphinx-for-chinese-2.1.0-dev-r3361

安装Sphinx:

        --prefix    指定安装路径

        --with-mysql    编译mysql支持,使用mysql需要预先安装

        --with-pgsql     编译pgsql支持

./configure --prefix=/usr/local/sphinx --with-mysql
make
make install


设置词库:

        在下载页面下还有一个名称为 xdict_1.1.tar.gz 的文件,这个文件里面是 txt 的词库文件,可以手动添加。也可以通过下面的地址wget到。

cd /usr/local/sphinx/etc
wget https://raw.githubusercontent.com/chenenkou/sphinx-for-chinese/master/downloads/xdict_1.1.tar.gz
tar -xf xdict_1.1.tar.gz
/usr/local/sphinx/bin/mkdict xdict_1.1.txt xdict

这个 xdict 文件的位置要在索引配置文件中指定。

index index_name{
        ...
    charset_type = utf-8
    chinese_dictionary = /usr/local/sphinx/etc/xdict
}


配置数据源


配置文件:原来的配置文件比较长,不用管只要按照下面的配置即可。

source src
{
	type			= mysql

	sql_host		= localhost
	sql_user		= root
	sql_pass		= 123456
	sql_db			= test
	sql_port		= 3306

	sql_query_pre		= set names utf8
	sql_query		= select id,title from article
}
index index_name
{
	source			= src
	path			= /usr/local/sphinx/var/data/index_name
	docinfo			= extern
	charset_type		= utf-8
	chinese_dictionary = /usr/local/sphinx/etc/xdict
}
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
	binlog_path		= /usr/local/sphinx/var/data
}

source:

        source      配置段用来配置数据源。

        type         用来定义数据源的类型,如:type=mysql

        sql_host   等都是mysql数据源需要配置的信息。

        sql_query_pre   在sql_query之前执行的sql语句,如 set names utf8 。

        sql_query          数据源的数据,如 select id, title from article 这里的id必须要有。

index:

        index配置段是用来创建索引所需要的配置信息,index 后面的名称就是索引的名称。

        source  指定数据源

        path     创建索引后索引文件存放的位置

        charset_type    设置字符集

        chinese_dictionary字典存放的位置

indexer:

        用来配置创建索引时相关的属性。

        men_limit        创建索引时所需的最大的内存

searchd:

        配置运行为守护进程时的相关信息。

        listen     监听端口

        log         searchd 进程的日志文件

        query_log    搜索关键词的日志文件


向mysql中创建表和导入数据:

CREATE TABLE `article` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(45) NOT NULL COMMENT '标题',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

插入数据:

insert into article(id, title) values(1, '夏季连衣裙'),(2, '秋季连衣裙');


简单使用


添加环境变量:

vi /etc/profile.d/sphinx.sh
echo "export PATH=$PATH:/usr/local/sphinx/bin" > /etc/profile.d/sphinx.sh
source /etc/profile.d/sphinx.sh

创建索引:

indexer -c /usr/local/sphinx/etc/sphinx.conf index_name

        -c        指定配置文件

        --rotate    在启动进程时创建索引

命令行搜索:

search -i index_name '连衣裙'

        -i     指定在那个索引中搜索,默认在全部索引中搜索

启动进程:

searchd -c /usr/local/sphinx/etc/sphinx.conf

停止进程:

searchd --stop



为PHP安装Sphinx的API接口


        PHP调用sphinx有两种方法,一种是包含sphinxapi.php文件的api,还有一种是为php安装sphinx扩展模块,这里演示第二种,为php安装sphinx模块。

安装PHP扩展所需的库文件libsphinxclient:

cd sphinx-for-chinese-2.1.0-dev-r3361/api/libsphinxclient
./configure --prefix=/usr/local/sphinx/libsphinxclient
make && make install

安装Sphinx的PHP扩展:

wget -c http://pecl.php.net/get/sphinx-1.3.3.tgz
tar xf sphinx-1.3.3.tgz
cd sphinx-1.3.3
phpize
./configure --with-sphinx=/usr/local/sphinx/libsphinxclient/ --with-php-config=/usr/bin/php-config
make && make install

成功后会提示:这个目录就是安装后生成的php模块

Installing shared extensions: /usr/lib64/php/modules/

开启php的这个模块:

echo "[Sphinx]" >> /etc/php.ini
echo "extension = sphinx.so" >> /etc/php.ini

重启httpd或重启php-fpm:

service httpd restart

调用API接口:

<?php
header('content-type:text/html;charset=utf-8');
$sphinx = new SphinxClient ();
$sphinx->SetServer ( '127.0.0.1', 9312);
var_dump($sphinx);
$sphinx->SetConnectTimeout ( 3 );
$result = $sphinx->Query ( '连衣裙', 'index_name' );
echo '<pre>';
var_dump($result);

如有搜索结果说明api安装成功。