最近负责一个项目,需要用到全文检索,我的环境大体如下:
## Minimal Sphinx configuration sample (clean, simple, functional)##数据源,src1为名字,后面会引用这个名字source src1{type = mysqlsql_host = localhostsql_user = testsql_pass =sql_db = testsql_port = 3306 # optional, default is 3306sql_query = \SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \FROM documentssql_attr_uint = group_idsql_attr_timestamp = date_added}#test1为索引名称,sphinx检索时需要这个名字,相当于关系数据库中的tableindex test1{source = src1 #引用的数据源名称path = @CONFDIR@/data/test1}index testrt{type = rtrt_mem_limit = 128Mpath = @CONFDIR@/data/testrtrt_field = titlert_field = contentrt_attr_uint = gid}indexer{mem_limit = 128M}searchd{listen = 9312listen = 9306:mysql41log = @CONFDIR@/log/searchd.logquery_log = @CONFDIR@/log/query.logread_timeout = 5max_children = 30pid_file = @CONFDIR@/log/searchd.pidseamless_rotate = 1preopen_indexes = 1unlink_old = 1workers = threads # for RT to workbinlog_path = @CONFDIR@/data}
D:\blue\sphinx-2.2.8-release-win64-full\bin>indexer -c ..\sphinx-min.conf.in --allSphinx 2.2.8-id64-release (r4942)Copyright (c) 2001-2015, Andrew AksyonoffCopyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)using config file '..\sphinx-min.conf.in'...indexing index 'test1'...collected 4 docs, 0.0 MBsorted 0.0 Mhits, 100.0% donetotal 4 docs, 33882 bytestotal 0.121 sec, 278900 bytes/sec, 32.92 docs/secskipping non-plain index 'testrt'...total 3 reads, 0.000 sec, 12.0 kb/call avg, 0.0 msec/call avgtotal 12 writes, 0.001 sec, 5.7 kb/call avg, 0.1 msec/call avg需要注意的是,如果需要建立的索引已经被使用,即已经启动了searchd服务,就需要增加--rotate参数,类似于indexer -c ..\sphinx-min.conf.in --all --rotate
然后在同一目录下运行 searchd -c ..\sphinx-min.conf.in,如下
D:\blue\sphinx-2.2.8-release-win64-full\bin>searchd -c ..\sphinx-min.conf.inSphinx 2.2.8-id64-release (r4942)Copyright (c) 2001-2015, Andrew AksyonoffCopyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)using config file '..\sphinx-min.conf.in'...listening on all interfaces, port=9312listening on all interfaces, port=9306precaching index 'test1'rotating index 'test1': successprecaching index 'testrt'precached 2 indexes in 0.045 sec
没有什么错误,需要注意的是,需要先创建索引,才能启动服务,否则可能会出错,searchd命令也可以安装为服务,以后使用起来会更加方便,这里这么做也是为了看到底是否配置成功,否则系统服务出错,我们看不到错误原因。
Sphinx 2.2.8-id64-release (r4942)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file '..\sphinx-min.conf.in'...
indexing index 'test1'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 303 bytes
total 0.086 sec, 3518 bytes/sec, 46.44 docs/sec
skipping non-plain index 'testrt'...
total 3 reads, 0.000 sec, 0.4 kb/call avg, 0.0 msec/call avg
total 12 writes, 0.001 sec, 0.2 kb/call avg, 0.0 msec/call avg
rotating indices: successfully sent SIGHUP to searchd (pid=4556).
"id" "group_id" "date_added""3" "2" "1427446411""4" "2" "1427446411"
这里面有一个问题,可以看出id 4实际上并没有“重构”这个词,只是包含“重”“构”这两个字而已,所以可能无法满足某些需求,但是好在Sphinx的默认匹配方式是短语相似度,所以理论上来说,包含“重构”这个词的会排序在前面,简单测试也是如此,是否一直如此就不知道了。可以参考这篇文章:http://rainkid.blog.163.com/blog/static/165140840201010277223611/
var SphinxClient = require ("sphinxapi"),util = require('util'),assert = require('assert');var cl = new SphinxClient();cl.SetServer('localhost', 9312);cl.Query('重构','test1', function(err, result) {assert.ifError(err);console.log(util.inspect(result, false, null, true));});运行程序,node sphinx2.js,如下{ error: '',warning: '',status: [ 0 ],fields: [ 'title', 'content' ],attrs:[ [ 'group_id', 1 ],[ 'date_added', 2 ] ],matches:[ { id: 3,weight: 2,attrs: { group_id: 2, date_added: 1427446411 } },{ id: 4,weight: 1,attrs: { group_id: 2, date_added: 1427446411 } } ],total: 2,total_found: 2,time: 0.004,words:[ { word: '重', docs: 2, hits: 2 },{ word: '构', docs: 2, hits: 2 } ] }可以看出和SphinxQL运行的效果一样,只不过返回的信息更多而已。
2)SphinxQL
#sphinx.js
var mysql = require('mysql');var connection = mysql.createConnection({host : 'localhost',port : '9306'});connection.connect();var queryString = "SELECT * FROM test1 WHERE MATCH('重构')";connection.query(queryString, function(err, rows, fields) {if (err) throw err;for (var i in rows) {console.log(JSON.stringify(rows[i]));}});connection.end();运行程序,node sphinx.js,如下{"id":3,"group_id":2,"date_added":1427446411}{"id":4,"group_id":2,"date_added":1427446411}乍看起来,似乎sphinxapi提供的信息更多,我没有具体比较过,不过sphinxQL也包含了一些函数,如weight(),可以返回权重,如执行SELECT *, weight() FROM test1 WHERE MATCH('重构'); 结果如下"id" "group_id" "date_added" "weight()""3" "2" "1427446411" "2557""4" "2" "1427446411" "1557"可知sphinxap提供的权重,似乎是sphinxQL提供的值除以1000之后的值
3、CentOS的安装和使用
$ yum install postgresql-libs unixODBC
$ rpm -Uhv sphinx-2.2.8.rhel6.x86_64.rpm
$ service searchd start
具体的使用和Windows是一样的,没有什么区别。
4、其他
indexer --merge DSTINDEX SRCINDEX [--rotate]
indexer --merge main delta --merge-dst-range deleted 0 0
2013.11.09 sphinx-for-chinese-2.2.1-dev-r4311-win32.zip2013.11.09 sphinx-for-chinese-2.2.1-dev-r4311.tar.gz
index test1{source = src1path = D:/blue/sphinx_data/data/test1docinfo = externcharset_type = utf-8chinese_dictionary = D:\blue\sphinx-for-chinese-2.2.1-dev-r4311-win32\xdict}其中charset_type = utf-8在最新的版本中已经废弃,因为默认已经是utf-8,xdict是一个字典文件