最近负责一个项目,须要用到全文检索,个人环境大致以下:node
## 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,以下mysql
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命令也能够安装为服务,之后使用起来会更加方便,这里这么作也是为了看究竟是否配置成功,不然系统服务出错,咱们看不到错误缘由。linux
Sphinx 2.2.8-id64-release (r4942)git
Copyright (c) 2001-2015, Andrew Aksyonoffgithub
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)sql
using config file '..\sphinx-min.conf.in'...数据库
indexing index 'test1'...npm
collected 4 docs, 0.0 MBubuntu
sorted 0.0 Mhits, 100.0% donewindows
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以后的值
三、CentOS的安装和使用
$ yum install postgresql-libs unixODBC
$ rpm -Uhv sphinx-2.2.8.rhel6.x86_64.rpm
$ service searchd start
具体的使用和Windows是同样的,没有什么区别。
四、其余
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是一个字典文件