sphinx是一个高效的搜索引擎,分词搜索的速度比较快,索引建立存储在硬盘文件,不会干扰数据库,有自己内置的一套数据库.希望能帮助到大家。
php中使用sphinx搜索引擎
sphinx是一个高效的搜索引擎,分词搜索的速度比较快,索引建立存储在硬盘文件,不会干扰数据库,有自己内置的一套数据库
1.ubuntu安装sphinx
如果没有安装aptitude ,需要先安装 aptitude 因为因为用apt get install 安装下面命令会出现问题.sudo apt-get install aptitudesudo aptitude install sphinx3 sphinx3-doc sphinxsearch sphinx-common -y
2.配置
| @@######@@ 1 2 |
@@######@@ cd /etc/sphinxsearch/ cp sphinx.conf.sample sphinx.conf |
修改配置文件如下
| @@######@@ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
@@######@@ source src1 { type = mysql sql_host = localhost sql_user = root sql_pass = magicmoma sql_db = coupon_20160901 sql_port = 3306 # optional, default is 3306 sql_query = SELECT couponid,title,description FROM app_coupon_api ##### 需要包含主键,分词索引字段 ######## } index test1 { source = src1 path = /var/lib/sphinxsearch/data/test1 #索引存放目录 docinfo = extern mlock = 0 morphology = none min_word_len = 1 charset_type = utf-8 min_prefix_len = 0 min_infix_len = 0 ngram_len = 1 html_strip = 0 } indexer { mem_limit = 2048M } searchd { listen = 9312 listen = 9306:mysql41 log = /var/log/sphinxsearch/searchd.log query_log = /var/log/sphinxsearch/query.log read_timeout = 5 client_timeout = 300 max_children = 30 pid_file = /var/run/sphinxsearch/searchd.pid max_matches = 1000 seamless_rotate = 1 preopen_indexes = 1 unlink_old = 1 mva_updates_pool = 1M max_packet_size = 8M max_filters = 256 max_filter_values = 4096 max_batch_queries = 32 workers = threads # for RT to work } |
-
执行命令分词,会在/var/lib/sphinxsearch/data/test1 目录下生成一堆索引文件
sudo indexer -c /etc/sphinxsearch/sphinx.conf test1
BJXSHOP网上开店专家下载BJXShop网上购物系统是一个高效、稳定、安全的电子商店销售平台,经过近三年市场的考验,在中国网购系统中属领先水平;完善的订单管理、销售统计系统;网站模版可DIY、亦可导入导出;会员、商品种类和价格均实现无限等级;管理员权限可细分;整合了多种在线支付接口;强有力搜索引擎支持... 程序更新:此版本是伴江行官方商业版程序,已经终止销售,现于免费给大家使用。比其以前的免费版功能增加了:1,整合了论坛
test1为上述配置文件的index名字
4.命令行测试搜索
sudo search -c /etc/sphinxsearch/sphinx.conf google
二.在php中使用
1.安装php sphinx的依赖库
1.安装 aptitude
apt-get install aptitudesudo 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中调用搜索
| @@######@@ 1 2 3 4 5 6 7 8 9 10 11 12 13 |
@@######@@ 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,该搜索引擎支持增量索引,多种模式的搜索,网上的资料也非常多










