php中使用sphinx搜索引擎
来源:互联网 发布:淘宝网韩都衣舍 编辑:程序博客网 时间:2024/06/06 07:43
sphinx是一个高效的搜索引擎,分词搜索的速度比较快,索引建立存储在硬盘文件,不会干扰数据库,有自己内置的一套数据库.
php中使用sphinx搜索引擎
sphinx是一个高效的搜索引擎,分词搜索的速度比较快,索引建立存储在硬盘文件,不会干扰数据库,有自己内置的一套数据库
1.ubuntu安装sphinx
如果没有安装aptitude ,需要先安装 aptitude 因为因为用apt get install 安装下面命令会出现问题.sudo apt-get install aptitude
sudo aptitude install sphinx3 sphinx3-doc sphinxsearch sphinx-common -y
2.配置
12
cd /etc/sphinxsearch/cp sphinx.conf.sample sphinx.conf
修改配置文件如下
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
source src1{type = mysqlsql_host = localhostsql_user = rootsql_pass = magicmomasql_db = coupon_20160901sql_port = 3306 # optional, default is 3306sql_query = SELECT couponid,title,description FROM app_coupon_api##### 需要包含主键,分词索引字段 ########}index test1{source = src1path = /var/lib/sphinxsearch/data/test1 #索引存放目录docinfo = externmlock = 0morphology = nonemin_word_len = 1charset_type = utf-8min_prefix_len = 0min_infix_len = 0ngram_len = 1html_strip = 0}indexer{mem_limit = 2048M}searchd{listen = 9312listen = 9306:mysql41log = /var/log/sphinxsearch/searchd.logquery_log = /var/log/sphinxsearch/query.logread_timeout = 5client_timeout = 300max_children = 30pid_file = /var/run/sphinxsearch/searchd.pidmax_matches = 1000seamless_rotate = 1preopen_indexes = 1unlink_old = 1mva_updates_pool = 1Mmax_packet_size = 8Mmax_filters = 256max_filter_values = 4096max_batch_queries = 32workers = threads # for RT to work}
- 执行命令分词,会在/var/lib/sphinxsearch/data/test1 目录下生成一堆索引文件
sudo indexer -c /etc/sphinxsearch/sphinx.conf test1
test1为上述配置文件的index名字
4.命令行测试搜索
sudo search -c /etc/sphinxsearch/sphinx.conf google
二.在php中使用
1.安装php sphinx的依赖库
1.安装 aptitude
apt-get install aptitude
sudo aptitude install libsphinxclient-dev libsphinxclient-0.0.1 -y
2.安装php sphinx的扩展
安装 peclsudo apt-get install php-pear php5-dev
在安装sphinxsudo pecl install sphinx
3.在配置文件php.ini中添加sphinx的扩展,
我的php.ini文件为sudo vim /etc/php5/fpm/php.ini
获取自己的php.ini文件位置使用php5-fpm -i|grep ini
添加:extension=sphinx.so
4.重启php5-fpm,查看php是否加载sphinx模块sudo /etc/init.d/php5-fpm restart
5.将search程序运行在后台sudo searchd -c /etc/sphinxsearch/sphinx.conf
默认监听配置文件中的端口:9312
6.在thinkphp中调用搜索
12345678910111213
public function testSphinx(){$s = new \SphinxClient;$s->setServer("localhost", 9312);$s->SetArrayResult (true );$s->setMatchMode(SPH_MATCH_ANY);$s->setMaxQueryTime(3);$result = $s->query("test");$result = $result['matches'];$result = array_column($result,'id');$list = M('CouponApi')->field('couponid,title,description')->where(array('couponid'=>array('in',$result)))->select();dump($list);}
搜索完毕,返回结果(默认返回20条,修改返回条数用添加 $s->SetLimits(0, 1000, 1000);
),搜索速度相当快,索引80w条数据的title和description两个字段耗时不到10s,该搜索引擎支持增量索引,多种模式的搜索,网上的资料也非常多
转载自:http://xueqy.top/
- php中使用sphinx搜索引擎
- PHP中使用Sphinx
- 在java环境中使用sphinx搜索引擎
- 在php中使用Sphinx
- 使用sphinx创建搜索引擎
- PHP之SPHINX的使用(全文搜索引擎)
- php sphinx 高效率搜索引擎搭建
- PHP+Sphinx+Mysql做搜索引擎
- Linux 全文搜索引擎 Sphinx 使用
- 用PHP构建自定义搜索引擎Sphinx
- php sphinx 高效率搜索引擎搭建(一)
- php sphinx 高效率搜索引擎搭建(一)
- php sphinx 高效率搜索引擎搭建(一)
- php sphinx 高效率搜索引擎搭建(二)
- php sphinx 高效率搜索引擎…
- php sphinx 高效率搜索引擎…
- php sphinx 高效率搜索引擎搭建(一)
- centso5下使用coreseek(sphinx)建立搜索引擎
- usb各个描述符的解释说明
- POI2014Salad Bar
- PHP 超级全局变量
- HDU5950Recursive sequence(矩阵快速幂)
- 回调函数的好处——有关查找值的例子分析
- php中使用sphinx搜索引擎
- 项目经验总结
- 接受现在的自己
- 58Q游戏(4)73(5)85(6)98(7)
- 如何直观理解交叉熵及其优势?
- USB一些解释说明
- CentOS 7.2 部署Node.js开发环境
- 串--快速转置
- jq应用总结(三)