php7+laravel+coreseek(sphinx)中文搜索初步实现(Linux)

  • 安装coreseek
  • 安装php的sphinx扩展
  • 安装laravel的sphinx组件

声明:借鉴的文章在下方会列出来,本文如有错误的地方,请看原文章,谢谢作者的无私分享。

安装coreseek

1、下载
官网www.coreseek.cn已不能下载,所以需从网上找资源,
注意的一点是,笔者安装coreseek-3.2.14版本后,使用时提示client版本高于server版本的错误,
php的sphinx扩展,为使用者,为client;coreseek是系统服务,为server

因笔者环境>php7,
而php7版本的sphinx扩展资源有限,无法降低版本。
所以我使用的coreseek最大版本coreseek-4.1-beta

(也可能是我配置的问题,请读者按以下步骤尝试安装coreseek-3.2.14,以下以4.1为例,如果也出现了这个问题,那么请安装4.1版本或者降低sphinx扩展版本)

下载地址:http://files.opstool.com/man/coreseek-4.1-beta.tar.gz
filezilla工具等上传至/usr下
2,编译安装

##安装必要的编译工作支持(如已安装的也建议从新安装,避免出现错误)
yum install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-devel libxml2-devel expat-devel

##开始编译
安装过程中出现过make出现错误的,笔者记不清在何处出现了,执行下make clean即可解决。
$ tar xzvf coreseek-4.1-beta.tar.gz
$ cd coreseek-4.1-beta
在这里插入图片描述
csft-3.2.14即coreseek,另外还有mmseg-3.2.14为mmseg中文分词,分别安装之。

##安装mmseg中文分词
$ cd mmseg-3.2.14
$ ./bootstrap #输出的warning信息可以忽略,如果出现error则需要解决
$ ./configure --prefix=/usr/local/mmseg3
$ make && make install
如出现错误,大部分是因为第一步必要的编译支持没有安装。

##安装csft-4.1
$ cd csft-4.1
$sh buildconf.sh #生成configure文件
(如未生成,且提示automake: warnings are treated as errors,可以将configure.ac中的这行AM_INIT_AUTOMAKE([-Wall -Werror foreign])改为AM_INIT_AUTOMAKE([-Wall foreign])即删掉-Werror,然后重新运行sh buildconf.sh。如果configure的时候提示没有安装MySQL的头文件,安装libmysql+±dev包即可。如没其他错误,生成configure后可执行编译安装)
$ ./configure --prefix=/usr/local/coreseek --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/ --with-mysql --with-php-config=/www/server/php/72/bin/php-config
(with-mysql:当 mysql_config 在环境变量中时,可以直接使用 --with-mysql 参数,而无需指定任何路径信息, sphinx 会自动从 mysql_config 命令的结果中提取 includes 和 libs 的路径, 当 mysql_config 不在环境变量中时,使用 --with-mysql=/path/to/mysql 指定 mysql_config 的路径, sphinx 会自动在指定路径下的 bin 目录下寻找 mysql_config 文件,所以目录结构应为: /path/to/mysql/bin/mysql_config。
2、通过参数 --with-mysql-includes 和 --with-mysql-libs 直接指定 includes 和 libs 的路径。 如:–with-mysql-includes=/path/to/mysql/include --with-mysql-libs=/path/to/mysql/lib。
注意:是否使用 --with-mysql-includes 和 --with-mysql-libs 参数,只跟 sphinx 是否可以找到 mysql_config 文件有关, 二者选其一即可。)

成功会提示 :Thank you for choosing Sphinx!

$ make && make install
但是make时报错:
sphinxexpr.cpp:1746:43: 错误:‘ExprEval’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
这是因为gcc版本在4.7以上,编译的时候出现的sphinx的一个bug报错
可以如下解决:修改csft-4.1/src/sphixexpr.cpp文件的1746, 1777和1823行,将三行中的ExprEval改为this->ExprEval。
修改完毕如无其他错误则执行make && make install

#编译libsphinxclient 为即将安装sphinx扩展做准备
libsphinxclient在csft-4.1/api目录下
在这里插入图片描述
$ cd libsphinxclient/
$ ./configure --prefix=/usr/local/libsphinxclient --with-php-config=/www/server/php/72/bin/php-config
报如下错误;
config.status: creating Makefile
config.status: error: cannot find input file: Makefile.in
这是因为configure.in的文件格式可能不对(是dos格式),用编辑器打configure.in文件,将其重新保存为 unix格式。
查看当前格式 :set ff ,如果为unix则格式正确
不正确的情况:
> vi configure.in
vi > set fileformat=unix
vi > wq
> aclocal
> autoconf
> automake -a
> ./configure
如果格式正确:
aclocal
libtoolize --force
automake --add-missing
autoconf
autoheader
make clean
执行以上命令再次进行安装
make && make install

阶段总结1

通过安装mmseg中文分词、安装csft-4.1 、编译libsphinxclient,在/usr/local下
在这里插入图片描述

安装php的sphinx扩展

Coreseek官方教程中建议php使用直接include一个php文件进行操作,但是在laravel中引入php文件来使用,显然不理想,事实上php有独立的sphinx模块可以直接操作coreseek(coreseek就是sphinx!)已经进入了php的官方函数库,而且效率的提升不是一点点!但php模块依赖于libsphinxclient包,即刚才编译完成的/usr/local/libsphinxclient.

##下载sphinx扩展
php7版本地址:
git.php.net/?p=pecl/search_engine/sphinx.git;a=shortlog;h=refs/heads/php7
在这里插入图片描述
如不是php7版本请看下方原文地址.
#编译sphinx
./configure --with-php-config=/www/server/php/72/bin/php-config --with-sphinx=/usr/local/libsphinxclient
make && make install
安装完成后,下方会出现sphinx.so的目录地址,即php扩展的模块默认目录
然后找到php.ini
在里面添加 extension=spninx.so
php-m 或者看下phpinfo();能看到sphinx即可。

阶段总结2

至此安装部分已全部完毕,下面是启动服务,然后laravel的使用

编辑coreseek配置文件 启动searchd

##编辑配置文件
$ cd /usr/local/coreseek/etc //配置目录
$ vim csft.conf
以下是我精简出来的conf文件,请删除注释,作为参考使用

source place{
	#source源
        type = mysql
        sql_host = 127.0.0.1
        sql_user = root
        sql_pass = root
        sql_db = ws
        sql_port = 3306
        sql_query_pre = SET NAMES utf8
        sql_query = SELECT id,name FROM places #要建立索引的表、字段
        sql_query_info_pre = SET NAMES utf8
        #在执行完之后执行语句 记录最大id 用于增量索引
        sql_query_post = replace into a select 1,max(id) from places; #这里需建立a表
}

index place{
        source = place #对应的source名称
        #存储索引的位置,路径必须真实存在,
        #如这里data下的place,需手动建立place文件夹
        #linux路径必须以/结尾
        path = /usr/local/coreseek/var/data/place/ 
        docinfo = extern #默认
        mlock =0  #默认
        morphology = none #默认
        min_word_len =1 #最小分词大小
        html_strip =0 #是否过滤html标签

        charset_type = zh_cn.utf-8 #编码
        charset_dictpath = /usr/local/mmseg3/etc/  #中文分词配置
        #charset-table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
        #ngram_len = 1
        #ngram_chars = U+3000..U+2FA1F
}

#定义增量源 会继承上面的写法
source add_place:place
{
        #取出还没有创建索引的语句,进行索引创建
        sql_query = select id,name from places where id > (select max_id from a);
}
#定义增量索引
index add_place:place
{
        source = add_place
        path = /usr/local/coreseek/var/data/place/
}

indexer
{
        mem_limit               = 128M #生成索引分配的内存

        # max_iops              = 40

        # max_iosize            = 1048576

        # max_xmlpipe2_field    = 4M

        # write_buffer          = 1M
}

searchd{
        listen = 6001 #默认监听端口 可以修改 使用默认也可以 需在阿里云开放入网端口
        read_timeout =5 #连接超时时间
        max_children = 30
        max_matches = 1000 #最多返回的查询到的索引记录数 不可过少
        seamless_rotate = 0
        preopen_indexes = 0
        unlink_old = 1
        pid_file = /usr/local/coreseek/var/log/searchd_rtindex.pid
        log = /usr/local/coreseek/var/log/searchd_rindex.log
        query_log = /usr/local/coreseek/var/log/query_rtindex.log
}

保存
#启动searchd
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft.conf
#查看进程
ps -aux | grep searchd

建立索引
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft.conf --place //某索引名称
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft.conf --all //全部索引
#至于定时更新索引,和定时增量索引,这里暂不介绍,无非是crontab定期执行命令脚本

laravel应用

GitHug地址:https://github.com/sngrl/sphinxsearch
按照说明一步步来就可以,提示的几点是:
php artisan vendor:publish --provider=sngrl\SphinxSearch\SphinxSearchServiceProvider --force
运行以上命名后,config目录下并没有生成sphinx的配置文件
则可以到vendor下sngrl/sphinxsearch里找,放进config文件,但这个配置文件有错误,把mysql_server配置删掉。
或者在config下直接手动创建sphinxsearch.php
配置文件中的代码即

return array (
	'host'    => '127.0.0.1',
	'port'    => 9312,
	'indexes' => array (
		'my_index_name' => array ( 'table' => 'my_keywords_table', 'column' => 'id' ),
	)
);

即是对的,需要注意的是port是csft.conf中的端口,其他笔者也在探索。
这样laravel就可以使用啦!

sphinx中文指南
http://www.sphinxsearch.org/sphinx-tutorial

感谢文章:
借鉴最多的文章:
https://www.cnblogs.com/gaoxu387/archive/2012/12/05/2803183.html
这篇文章讲的比较细致,对错误介绍的比较详细,coreseek4.1
https://blog.csdn.net/youcijibi/article/details/76690286
解决 coreseek 因 gcc 版本大于 4.7导致安装失败
https://segmentfault.com/a/1190000005019591
安装coreseek过程
http://www.thinkphp.cn/topic/34548.html
linux下php支持sphinx的扩展安装
https://blog.csdn.net/youcijibi/article/details/76728382

扩展文章:
[coreseek/sphinx学习笔记5]–通用API
https://blog.csdn.net/slqgenius/article/details/52108458
coreseek增量索引
https://www.cnblogs.com/timothy-lai/p/6145928.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值