登录  /  注册

linux下编译安装Sphinx、中文分词coreseek及PHP的sphinx扩展

伊谢尔伦
发布: 2016-11-25 15:57:22
原创
1884人浏览过

linux环境为:centos5.5、ubuntu12.04

1、软件下载

sphinx-2.1.3

coreseek-4.1

2、安装 sphinx

tar zxvf sphinx-2.1.3.tar.gz //解压sphinx包
cd sphinx-2.1.3
./configure --prefix=/usr/local/sphinx --with-mysql=/usr/local/mysql/
登录后复制

--prefix:指定 sphinx 的安装路径
--with-mysql:指定 mysql 安装路径

sudo make && make install
登录后复制

编译并安装

安装成功之后,sphinx 会形成三个命令:

indexer 创建索引命令
searchd 启动进程命令
search 命令行搜索命令
登录后复制

注:上述命令默认在/usr/local/sphinx/bin目录下

3、配置 sphinx及使用

cd /usr/local/sphinx/etc
登录后复制

进入 sphinx 的 etc 目录下

sudo cp sphinx.conf.dist sphinx.conf
登录后复制

拷贝一份配置文件,并且命名为 sphinx.conf。

将该目录下的example.sql文件导入本地数据库名为test的数据库中。

修改配置信息,将其中数据库连接信息修改为你的本地数据库信息。

然后使用如下命令导入数据并生成索引:

$ cd /usr/local/sphinx/etc
$ sudo /usr/local/sphinx/bin/indexer --all
登录后复制

如果执行indexer命令报错:

ERROR: index 'test1stemmed': sql_connect: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
登录后复制

则可能是因为mysql.sock的位置问题,在本机中,改位置是/tmp/mysql.sock(与安装mysql时设置有关),在sphinx.conf中取消这一行的注释即可(去掉前面的#号):

#sql_sock                = /tmp/mysql.sock
登录后复制

再次执行上述indexer命令,一切正常。

执行查询的话可以使用如下指令:

$ cd /usr/local/sphinx/etc
$ sudo /usr/local/sphinx/bin/search test
登录后复制

在执行search搜索过程中可能报错:

index 'test1': search error: query too complex, not enough stack (thread_stack=-2665032K or higher required).
登录后复制

我试着在sphinx.conf中的searchd模块中修改thread_stack的值,但没有解决问题,后来注释掉source src1中的如下这行

#sql_query_info         = SELECT * FROM documents WHERE id=$id
登录后复制

再执行search命令,就OK了。

后台启动sphinx使用如下指令:

$ cd /usr/local/sphinx/etc
$ sudo /usr/local/sphinx/bin/searchd
登录后复制

使用php脚本执行搜素命令,可使用如下指令:

$ cd sphinx/api
$ php test.php test
登录后复制

输入结果如下:

Query 'test ' retrieved 3 of 3 matches in 0.022 sec.
Query stats:
'test' found 10 times in 6 documents
Matches:
1. doc_id=1, weight=101, group_id=1, date_added=2014-01-20 10:07:37
2. doc_id=2, weight=101, group_id=1, date_added=2014-01-20 10:07:37
3. doc_id=4, weight=1, group_id=2, date_added=2014-01-20 10:07:37
登录后复制

4、安装 coreseek

tar zxvf coreseek-4.1.tar.gz
登录后复制

解压会出现两个目录:csft-4.1、mmseg-3.2.14

先安装 mmseg

cd mmseg-3.2.14/
./configure --prefix=/usr/local/mmseg
登录后复制

编译如果出现错误:“config.status: error: cannot find input file: src/Makefile.in”

解决方案:

sudo apt-get install automake
aclocal
libtoolize --force 我运行后有一个错误,没管它。
automake --add-missing
autoconf
autoheader
make clean
./configure --prefix=/usr/local/mmseg
make
sudo make install
登录后复制

安装 csft(coreseek)

cd csft-4.1/
sh buildconf.sh
./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/ 
sudo make && make install
登录后复制

安装完成之后和sphinx安装后一样,需要导入coreseek/etc/example.sql文件到本地数据库,然后拷贝sphinx.conf.dist到csft.conf,修改其中数据库用户名及密码,测试出错和sphinx中出错解决办法一样。

使用 sphinx 需要做以下几件事

1.有数据;

2.建立 sphinx 配置文件;

3.生成索引;

4.启动 searchd 服务进程,默认是9312

5.用 PHP 去连接 sphinx 服务

启动 sphinx

cd /usr/local/coreseek/bin/
./searchd
登录后复制

启动命令

searchd 命令参数介绍:

-c 指定配置文件

--stop 停止服务

--pidfile 用来显式指定一个 PID 文件

-p 指定端口

5、php 安装 sphinx 扩展

sudo pecl install sphinx
登录后复制

如果出现错误:“configure: error: Cannot find libsphinxclient headers”

解决方法:

cd coreseek-4.1/csft-4.1/api/libsphinxclient/
./configure --prefix=/usr/local/libsphinxclient
sudo make && make install
登录后复制

解决完毕!

回去接着执行

./configure --with-php-config=/usr/local/php/bin/php-config --with-sphinx=/usr/local/libsphinxclient
sudo make && make install
登录后复制

出现类似“Installing shared extensions: /usr/lib/php5/20090626/sphinx.so”,表示成功。

可以进入该目录下会发现生成了一个 sphinx.so 文件

在 php.ini 中加载该 so 文件

extension=/usr/lib/php5/20090626/sphinx.so

重启 apache ,phpinfo() 中出现这个表明成功。

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号