sphinx配置和使用
sphinx安装配置
安装sphinx
sphinx下载
sphinx如果要用mysql作为数据源的话需要依赖安装msyql-devel
1 2 3 4 5 6 7 |
wget http://sphinxsearch.com/files/sphinx-2.2.10-release.tar.gz tar zxvf sphinx-2.2.10-release.tar.gz cd sphinx-2.2.10-release ./configure --prefix=/usr/local/webserver/sphinx --with-mysql=/usr/local/webserver/mysql/ #--prefix : 指定Sphinx安装到何处,我的安装目录是"/usr/local/webserver/sphinx" #--with-mysql : mysql的安装目录,我的安装目录是"/usr/local/webserver/mysql/",如果安装在系统默认则不需要指定 make && make install |
若有以下报错:
1 2 3 4 5 6 7 |
../config/depcomp: line 669: exec: g++: not found make[2]: *** [sphinx.o] 错误 127 make[2]: Leaving directory '/home/wuwei/sphinx-2.2.10-release/src' make[1]: *** [all] 错误 2 make[1]: Leaving directory '/home/wuwei/sphinx-2.2.10-release/src' make: *** [all-recursive] 错误 1 g++没有找到需要安装g++,在不同的linux中g++的名称可能不同 |
配置sphinx
BBC提供的sphinx默认配置文件说明
BBC提供的sphinx默认配置目录:script/sphinx/
需要PHP支持的sphinx配置文件:script/sphinx/sphinx.conf
注意:sphinx服务器需要有PHP支持
该配置文件的好处:可以单独配置索引 索引配置存放在conf.d文件夹中
sphinx原生支持的配置文件:script/sphinx/sphinx_all.conf
注意:不需要PHP支持,所有索引配置在同一个文件中
选择配置文件script/sphinx/sphinx.conf + conf.d/*或script/sphinx/sphinxall.conf 拷贝到sphinx服务器/usr/local/webserver/sphinx/etc/(cp sphinxall.conf sphinx.conf)
配置文件注意事项
需要PHP支持的配置:script/sphinx/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 |
#!/usr/bin/php <?php //MYSQL参数和WEB服务器中的config/production/database.php MYSQL参数一致 //如果有主从配置,请选择一个从服务器的配置 $sourceConfig = array( "type = mysql", "sql_host = 127.0.0.1", "sql_user = root", "sql_pass = root", "sql_db = b2b2c", "sql_port = 3306", ); $sourceConfig = implode(" \n", $sourceConfig); $files = scandir(dirname(__FILE__) . "/conf.d/"); foreach($files as $key => $file) { if($file != "." && $file != ".." && preg_match('/.conf$/iU', $file) ) { $tmpFile = dirname(__FILE__) . "/conf.d/$file"; $fileContents = file_get_contents($tmpFile); echo str_replace('{sourcesConfig}', $sourceConfig, $fileContents); } } ?> indexer { mem_limit = 80M } searchd { listen = 9312 listen = 9306:mysql41 log = /usr/local/var/log/searchd.log //日志文件需要读写权限 query_log = /usr/local/var/log/query.log //日志文件需要读写权限 read_timeout = 5 client_timeout = 300 max_children = 30 pid_file = /usr/local/var/log/searchd.pid //守护进程文件需要读写权限 seamless_rotate = 1 preopen_indexes = 0 unlink_old = 1 mva_updates_pool= 1M max_packet_size = 8M max_filters = 256 max_filter_values= 4096 dist_threads = 2 } |
conf.d/*中具体索引配置说明,不可直接使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
source sysitem_item { {sourcesConfig} ...... ...... } index sysitem_item { source = sysitem_item path = /usr/local/var/data/sysitem_item #商品搜索索引文件需要读写权限 docinfo = extern ...... ...... } |
sphinx原生支持的配置文件:script/sphinx/sphinx_all.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 |
source sysitem_item { #MYSQL参数和WEB服务器中的config/production/database.php MYSQL参数一致 #如果有主从配置,请选择一个从服务器的配置 type = mysql sql_host = 127.0.0.1 sql_user = root sql_pass = root sql_db = b2b2c sql_port = 3306 ...... ...... #这里如果需要定制,我们只能简单修改select语句,其他是和代码向关联,一般无法修改,必须修改,需要自己开发或者找我哟 } index sysitem_item { source = sysitem_item path = /usr/local/var/data/sysitem_item #商品搜索索引文件需要读写权限 docinfo = extern ...... ...... } indexer { mem_limit = 80M } searchd { listen = 9312 listen = 9306:mysql41 log = /usr/local/var/log/searchd.log //日志文件需要读写权限 query_log = /usr/local/var/log/query.log //日志文件需要读写权限 read_timeout = 5 client_timeout = 300 max_children = 30 pid_file = /usr/local/var/log/searchd.pid //守护进程文件需要读写权限 seamless_rotate = 1 preopen_indexes = 0 unlink_old = 1 mva_updates_pool= 1M max_packet_size = 8M max_filters = 256 max_filter_values= 4096 dist_threads = 2 } |
启动sphinx
环境变量配置
1 2 |
echo "export PATH=$PATH:/usr/local/webserver/sphinx/bin" >> /etc/profile source /etc/profile |
建立索引indexer --all --rotate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Sphinx 2.2.11-id64-release (95ae9a6) Copyright (c) 2001-2016, Andrew Aksyonoff Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com) using config file '/usr/local/webserver/sphinx/etc/sphinx.conf'... indexing index 'sysitem_item'... collected 395 docs, 0.1 MB collected 3177 attr values sorted 0.0 Mvalues, 100.0% done sorted 0.0 Mhits, 100.0% done total 395 docs, 87472 bytes total 0.179 sec, 487168 bytes/sec, 2199.91 docs/sec total 7133 reads, 0.004 sec, 0.0 kb/call avg, 0.0 msec/call avg total 15 writes, 0.000 sec, 35.3 kb/call avg, 0.0 msec/call avg |
执行searchd开启sphinx, 如果要停止sphinx执行命令searchd --stop
1 2 3 4 5 6 7 8 9 |
Sphinx 2.2.11-id64-release (95ae9a6) Copyright (c) 2001-2016, Andrew Aksyonoff Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com) using config file '/usr/local/webserver/sphinx/etc/sphinx.conf'... listening on all interfaces, port=9312 listening on all interfaces, port=9306 precaching index 'sysitem_item' precached 1 indexes in 0.002 sec |
使用MYSQL协议查看sphinx索引表
1 2 3 4 5 6 7 8 9 |
mysql -P9306 -h127.0.0.1 mysql> show tables; +--------------+-------+ | Index | Type | +--------------+-------+ | sysitem_item | local | +--------------+-------+ 1 row in set (0.00 sec) |
定时更新sphinx索引
1 |
* * * * * /usr/local/webserver/sphinx/crontab.sh |
crontab.sh脚本
1 2 3 4 5 6 7 8 9 |
#!/bin/bash step=60 #间隔的秒数,不能大于60 for (( i = 0; i < 60; i=(i+step) )); do /usr/local/webserver/sphinx/bin/indexer --all --rotate sleep $step done exit #60秒全部更新一次索引,可以手动执行更新索引查看全部更新索引的时间是否在可以接受, 如果在不可接受的情况下,可进行配置增量索引。并且可配置MYSQL从服务器,用于sphinx更新索引使用 |
WEB服务器:安装scws
如果是多台web服务器则,每台web服务器都需要安装scws
scws下载
安装scws
1 2 3 4 5 |
wget http://www.xunsearch.com/scws/down/scws-1.2.2.tar.bz2 tar xvf scws-1.2.2.tar.bz2 cd scws-1.2.2 ./configure --prefix=/usr/local/scws make && make install |
PHP安装scws扩展(回到scws源目录中scws-1.2.2)
1 2 3 4 |
cd phpext/ /usr/local/webserver/php/bin/phpize ./configure --with-scws=/usr/local/scws/ --with-php-config=/usr/local/php/bin/php-config make && make install |
配置php.ini,在php.ini中添加如下代码
1 2 3 4 5 |
[scws] extension = scws.so scws.default.charset = utf8 scws.default.fpath = /usr/local/scws/ ;该路径是编译时的--with-scws的值 |
BBC中配置sphinx
BBC中使用sphinx的配置放在config/search.php中
将config/search.php复制到config/production/search.php
XDB 词典文件下载
简体中文(utf8)词典下载
简体中文(GBK)词典下载
繁体中文(UTF-8)词典下载
一般我们bbc使用简体中文(utf8)词典就可以了,下载词典放置到/usr/local/scws/etc/
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 |
<?php /* |-------------------------------------------------------------------------- | sphinx 配置 |-------------------------------------------------------------------------- | | 对应原系统: SPHINX_SERVER_HOST SPHINX_PCONNECT | */ 'sphinx' => array ( 'host' => '127.0.0.1:9306',//指定sphinx服务器的ip和端口 'pconnect' => true, // 是否启用sphinx持续连接 ), /* |-------------------------------------------------------------------------- | scws 配置 |-------------------------------------------------------------------------- | | 对应原系统: SCWS_DICT SCWS_RULE | 如果是集群部署|词典路径需一致|或者词典放在同步目录里面调用 | */ 'scws' => array( 'dict' => '/usr/local/scws/etc/dict.utf8.xdb', 'rule' => '/usr/local/scws/etc/rules.utf8.ini', ), |
配置后运营平台中开启sphinx搜索
系统-》搜索配置-》搜索引擎管理
查看状态,如果状态为连接异常
- config/production/search.php中的sphinx服务器ip和端口是否正确
- 到sphinx服务器中查看是否开启,到sphinx执行下 mysql -P9306 -h127.0.0.1
- 查看sphinx服务器iptables是否屏蔽了web服务器
如果显示正常则可以点击 启用 则表示正常使用了sphinx
系统-》搜索配置-》索引管理
启用了sphinx 则可以在索引管理中看到当前sphinx中有哪些索引表和对应的索引字段,对应的索引字段类型
附:Sphinx 单词+数字索引
导读
Sphinx 是一个非常强大的全文搜索引擎,能处理各种复杂的索引请求.但有时候需要一些特殊的设置.
问题
比如文档中有一种词,该词包含了字母和数字,比如(onepiece123) 如果一般情况下直接搜索onepiece或者123的时候,你是得不到这个文档的.
原因
sphinx在处理单词时,把整个词看作一个整体,用关键词搜索的时候也是,比如,搜索词如果是onepiece,那么得到的文档只能是包含了onepiece这个词段(如onepiece 123)的才能被索引到,否则除非直接搜索onepiece123,你是得不到文档onepiece123的.
解决方式
这种时候有两种方式可以解决:
中缀索引
设置中缀索引长度,中缀索引可以让我们进行通配符搜索文档的操作,比如word, word*, *word
mininfixlen
中缀索引长度默认为0,即不做中缀索引,假如设置成一个合适的长度,那么就能根据单词的某一部分查找到对应的文档,比如把mininfixlen 设置为2, 那么文档中包含test的词汇就会被分成 te, es, st tes, est,test这几种(最小长度2),当我们用关键字es 去搜索的时候,就能命中这个包含test的文档,当然,这也导致了我们的索引文件大小大幅度增加,索引速度降低的问题
为了解决开始的时候我们提到的这种单词带数字的查找,我们可以设置一个比较合适的中缀索引长度,比如8,那么包含onepiece123这个词的文档会被分成onepiece, onepiece1, onepiece12, onepiece123, nepiece12,...等等(按顺序包含至少8个原有字符,不能跳过任意的字母),这样,当我们搜索oinepiece的时候, 因为原有的oinepiece123文档被分成了包含了onepiece等多个的文档,我们就能命中这个oinepiece123所在的文档了.
设置regexp_filter
该设置允许我们通过正则的方式映射最终的文档格式.那样的话我们就能统一一些常见的单词或者短语的格式了.
比如
'iphone 3gs' , 'iphone 3 gs' (或者甚是 'iphone3 gs')
当我们在搜索这些关键词的时候,我们都希望能命中包含iphone3gs这个文档,我们可以像这样设置索引:
regexp_filter = [(iphone 3gs)| (iphone 3 gs) | (iphone3 gs)] => iphone3gs
这样在创建索引的时候, 所有包含上面三种的iphone 3gs的格式都能映射成iphone3gs, 在搜索iphone3gs的时候, 就能命中所有包含上面三种词的文档了
回归我们的初始问题,这种方式处理单词+数字的索引设置可以这么设置
regexp_filter = ([a-z|A-Z]+)(\d+) =>\1 \2
"\1"和"\2"之间有空格,"\1"对应单词的正则匹配, "\2"对应数字的正则匹配,中间加了一个空格,这就表示在创建索引的时候单词+数字的文档会被分开.
不过要说明的是,如果想使用这个配置,需要安装一个名叫re2的正则引擎替代系统内置的正则引擎,安装好后重新编译安装sphinx的源码,在./configure的时候带上参数
--with-re2,然后regexp_filter才会游泳
这样也能解决单词+数字的搜索问题。
总结
两种方式都能起作用,中缀索引比较简单,但会大幅度增加索引文件的大小和索引的速度.regexp_filter安装正则引擎比较烦,而且要重新编译安装sphinx, 但对索引文件的大小和索引速度的影响较小。