2019独角兽企业重金招聘Python工程师标准>>>
提要:本来之前一直都是使用whoosh作为全文搜索,后面在使用的过程愈发的发现这个东西的搜索并不特别的完美。并且生成的索引量又十分巨大。所以考虑换一个,最后选了sphinx来作为新的搜索。而且网上的安装例子都并不是很全。因此做下记录。
介绍
Sphinx是一个基于SQL的全文检索引擎,可以结合MySQL,PostgreSQL做全文搜索,它可以提供比数据库本身更专业的搜索功能,使得应用程序更容易实现专业化的全文检索。Sphinx特别为一些脚本语言设计搜索API接口,如PHP,Python,Perl,Ruby等,同时为MySQL也设计了一个存储引擎插件
Sphinx 单一索引最大可包含1亿条记录,在1千万条记录情况下的查询速度为0.x秒(毫秒级)。Sphinx创建索引的速度为:创建100万条记录的索引只需 3~4分钟,创建1000万条记录的索引可以在50分钟内完成,而只包含最新10万条记录的增量索引,重建一次只需几十秒
Sphinx的主要特性包括:
高速索引 (在新款CPU上,近10 MB/秒); 高速搜索 (2-4G的文本量中平均查询速度不到0.1秒); 高可用性 (单CPU上最大可支持100 GB的文本,100M文档); 提供良好的相关性排名 支持分布式搜索; 提供文档摘要生成; 提供从MySQL内部的插件式存储引擎上搜索 支持布尔,短语, 和近义词查询; 支持每个文档多个全文检索域(默认最大32个); 支持每个文档多属性; 支持断词; 支持单字节编码与UTF-8编码
-
1、安装包的准备
sphinx:https://github.com/eric1688/sphinx (我采用的是sphin-for-chinases支持中文搜索)
xdict :http://pan.baidu.com/s/1czkGO2 密码:kouv (下载这个分词工具可能需要梯子,因此我直接常上传百度云)
shpinxapi: https://github.com/feeen/sphinxapi/blob/master/sphinxapi/init.py (这个是python调用shpinx的API。直接拿过来导入就可以使用)
-
2、安装sphinx
-
首先安装mysql
apt-get -y install mysql-server libmysqlclient-devupdatedb
-
-
安装sphinx
git clone https://github.com/eric1688/sphinx cd sphinx ./configure --prefix=/usr/local/sphinxmake -j2 && make install
-
3、配置中文支持 tar xf xdict_1.1.tar.gz /usr/local/sphinx/bin/mkdict xdict_1.1.txt xdict mv xdict /usr/local/sphinx/etc/
-
4、修改配置文件 cp sphinx.conf.dist sphinx.conf vim sphinx.conf source posts { type = mysql sql_host = 127.0.0.1 sql_user = test sql_pass = test sql_db = test sql_port = 3306 # optional, default is 3306 sql_sock = /var/run/mysqld/mysqld.sock sql_query = SELECT id, post_title FROM test sql_query_pre = SET NAMES utf8 # sql_attr_uint = id sql_field_string = post_title sql_ranged_throttle = 0 } index test1 { source = posts # 索引文件的存放位置
path = /usr/local/sphinx/var/data/posts 文件存储模式(默认为extern)
docinfo = extern
#缓存数据内存锁定
mlock = 0
# 马氏形态学(对中文无效)
morphology = none
# 索引词最小长度
min_word_len = 1
# 数据编码(设置成utf8才能索引中文,最新版本的sphinx不支持这个参数)
# charset_type = utf-8
# 中文分词词典
chinese_dictionary = /usr/local/sphinx/etc/xdict
# 最小索引前缀长度
min_prefix_len = 0
# 最小索引中缀长度
min_infix_len = 1
# 对于非字母型数据的长度切割 ngram_len = 1 # 对否对去除用户输入查询内容html_strip = 0 }# 索引器设置 indexer { # 内存大小限制 默认是 32M, 最大 2047M, 推荐为 256M 到 1024M之间 mem_limit = 256M }# sphinx服务进程search的相关配置 searchd { # 监测端口及形式,一下几种均可,默认为本机9312端口 # listen = 127.0.0.1 # listen = 192.168.0.1:9312 # listen = 9312 # listen = /var/run/searchd.sock # search进程的日志路径log = /usr/local/sphinx/var/log/searchd.log # 查询日志地址 query_log = /usr/local/sphinx/var/log/query.log # 读取超时时间 read_timeout = 5 # 请求超时市时间 client_timeout = 300 # searche进程的最大运行数 max_children = 30 # 进程ID文件 pid_file = /usr/local/sphinx/var/log/searchd.pid # 最大的查询结果返回数 (这个参数新版本不支持) #max_matches = 1000 # 是否支持无缝切换(做增量索引时需要) seamless_rotate = 1 # 在启动运行时是否提前加载所有索引文件 preopen_indexes = 0 # 是否释放旧的索引文件 unlink_old = 1 # MVA跟新池大小(默认为1M) mva_updates_pool = 1M # 最大允许的网络包大小(默认8M) max_packet_size = 8M # 每个查询最大允许的过滤器数量(默认256) max_filters = 256 #每个过滤器最大允许的值的个数(默认4096) max_filter_values = 4096 # 每个组的最大查询数(默认为32) max_batch_queries = 32 } # Sphinx配置文件结束
-
5、增加索引 /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf test1 如果有多个索引就使用参数--all /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf --all
-
6、开启守护进程 /usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/sphinx.conf 如果重建索引时守护进程正在运行,需要运行下面的指令 会重建索引并且重开守护进程 /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf --all --rotate
##这样一来就基本配置完成了,然后下面这个简单的demo就是用python来调用sphinx达到查询 import sphinxapi spc = sphinxapi.SphinxClient() spc.SetServer(str('127.0.0.1'),9312) spc.SetMatchMode(sphinxapi.SPH_MATCH_ANY) spc.SetLimits(0,5) res = spc.Query('星巴克') print res