Sphinx 入门

sphinx说是性能不错

高速索引
在现代CPU上可达10 MB/秒(英文),在启用了中文分词后,建立索引的速度可达300K/s;
高速搜索
在2-4 GB的文本建立的索引上搜索,平均0.1秒内获得结果;
可处理大数据量
在单一CPU上,实测最高可对100GB的文本建立索引,单一索引可包括100M文件
支持分布式搜索
支持主从式的分布式搜索,支持单一节点失效不影响整个搜索系统

而且支持php,java,python
就是本地需要启个服务,然后其他的程序通过连这个服务搜索。默认端口9312

首先本地windows环境测试,
直接下载官方最新版本sphinx2.2.6
http://sphinxsearch.com/downloads/release/
配置方式网上有很多,我试的是mysql索引
比如这篇:
http://www.cnblogs.com/ainiaa/archive/2010/12/21/1912459.html

1,建立索引
下载好目录中有个sphinx.conf.in
改名成sphinx.conf 去掉id,配置下里面的mysql参数
然后运行bin\indexer.exe 就可以了

2,启动服务
运行bin\searchd.exe [可以写个索引名]

3,搜索
网上有很多search 关键词 搜索的例子
这个版本已经没有这个exe了。
提供了一个php的api
官方两个例子比较复杂
找了个简单的例子:
SetServer(‘localhost’,9312);
$result = $s->Query(‘key’);
//var_dump($result);
echo json_encode($result);
?>

其中
Query ( $query, $index=”*”, $comment=”” )
支持3个参数,
$query=查询关键词
$index=索引名,有多个表都有索引的话可以通过这里过滤下
$comment=应该是要查询的字段

不过问题来了。。介个不支持中文
http://www.coreseek.cn
搜索中文分词出现了coreseek,一开始以为是个分词工具,
但其实是sphinx中文分词版,里面带完整的sphinx.而且呢,里面的例子更方便,直接带cmd测试工具。。
配置文件也全中文说明,太方便了。

就是版本老的可以,最后一次更新是2013-11-26 年,Sphinx 2.2.1-beta 与 2.1.3-release 发布。

接下来用这个版本测试了下中文分词搜索,好用了!给个好评。

基本命令:
http://blog.sina.com.cn/s/blog_705e4fdc0101ajog.html

6、创建更新所有索引
$sudo /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/dict/csft_mysql.conf –all –rotate
如果配置正确的话,现在辅助表sph_counter中已经添加了一条数据

7、更新增量索引
$sudo /usr/local/coreseek/bin/indexer delta -c /usr/local/coreseek/dict/csft_mysql.conf –rotate

8、合并增量索引到主索引
$sudo /usr/local/coreseek/bin/indexer –merge -c /usr/local/coreseek/dict/csft_mysql.conf –rotate

9、启动sphinx的守护进程searchd
$sudo /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/dict/csft_mysql.conf

centos 安装参考
http://www.cnblogs.com/huailian/archive/2013/06/26/2987231.html

安装mmseg的时候,./configure出现错误:config.status: error: cannot find input file: src/Makefile.in
网上的文章是:
aclocal
libtoolize –force (libtoolize –f)
automake –add-missing (automake –a)
autoconf
autoheader
make clean

括号中是我的版本有些属性差异
运行aclocal
warning: macro `AM_PROG_LIBTOOL’ not found in library centos
需要安装
yum install libtool

安装coreseek的时候报错:

checking for CFLAGS needed for pthreads... none
checking for LIBS needed for pthreads... -lpthread
checking for pthreads... found
checking for pthread_mutex_timedlock... yes
checking whether to compile with MySQL support... yes
checking for mysql_config... mysql_config
checking for mysql_real_connect... no
checking for mysql_real_connect... no
checking MySQL include files... configure: error: missing include files.

******************************************************************************
ERROR: cannot find MySQL include files.

Check that you do have MySQL include files installed.
The package name is typically ‘mysql-devel’.

If include files are installed on your system, but you are still getting
this message, you should do one of the following:

1) either specify includes location explicitly, using –with-mysql-includes;
2) or specify MySQL installation root location explicitly, using –with-mysql;
3) or make sure that the path to ‘mysql_config’ program is listed in
your PATH environment variable.

To disable MySQL support, use –without-mysql option.
******************************************************************************

然后尝试安装mysql-devel报错:

warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID 4520afa9: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY.art.txt

rpm -Uhv http://dag.wieers.com/packages/rpmforge-release/rpmforge-release-0.3.4-1.el4.rf.i386.rpm
rpm –import /etc/pki/rpm-gpg/RPM-GPG-KEY-6

尼玛,疯了, 数据源问题,先不整这个了,去掉mysql支持安装下,最后终于装上了,但不支持mysql
换另一个服务器试下。。。
换了个服务器,安装成功。
查询乱码需要修改字符集
charset_type = utf-8

 

 

排序和分页:

java代码排序

public static void main(String[] args){
List l;
Object a;
SphinxClient clint=new SphinxClient();
try {
clint.SetSortMode(4, "id desc");
SphinxResult result= clint.Query("a");
System.out.println(result.matches);
for(int i=0;i<result.matches.length;i++){
SphinxMatch match= result.matches[i];
l=match.attrValues;
for(int j=0;j<l.size();j++){
a=l.get(j);
System.out.print(a+"|");

}
System.out.println("");
}
} catch (SphinxException e) {
e.printStackTrace();
}
}

 

php代码排序

require 'sphinxapi.php';
$s = new SphinxClient();
$s->SetServer('localhost',9312);
$s->SetSortMode( SPH_SORT_EXTENDED, "group_id DESC,@id DESC" );
$result = $s->Query('a');
echo json_encode($result);

java-ok php排序无效,为毛啊,api有问题么
换新api提示
searchd error: client version is higher than daemon version (client is v.1.30, daemon is v.1.23)
不匹配

卧槽,发现firefox json插件自动帮我排序,谢谢。我错怪api了。

然后测试下分页
$s->SetLimits(1,5,10000);
这样

基本使用上就没有啥问题了。