2.3. 在Linux、BSD上安装Sphinx/Coreseek
-
将你下载的tar包解压,并进入
coreseek
子目录:$ tar xzvf coreseek-3.2.14.tar.gz
$ cd coreseek
-
首先安装MMSeg:
$ cd mmseg
$ ./configure --prefix=/usr/local/mmseg
$ make
$ make install
$ cd ..
//如果出现错误 src/Makefile.in
yum install
libtool autoconf automake
aclocal
libtoolize --force
automake --add-missing
autoconf
autoheader
make clean
$ ./configure --prefix=/usr/local/mmseg
-
运行配置 程序:
$ ./configure
configure程序有很多运行选项。完整的列表可以通过使用
--help
开关得到。最重要的如下:--prefix
, 定义将Coreseek安装到何处;比如--prefix=/usr/local/coreseek
(以下全部示例都假定Coreseek安装在这个位置)--with-mysql
, 当自动检测失败时,指出在那里能找到MySQL 头文件和库文件;--with-pgsql
, 指出在那里能找到PostgreSQL头文件和库文件.--with-mmseg
, 启用基于MMSeg的中文分词法,并指出在那里能找到MMSeg头文件和库文件.--with-python
, 启用Python数据源支持. 需要预先安装Python2.6.
-
编译源代码生成二进制程序:
$ make
-
安装二进制程序到你设定的目录下: (类Unix操作系统下默认为
/usr/local/bin/
, 但是可以被configure --prefix
) 修改安装目录$ make install
如果出现
usr/local/sphinx/bin/indexer:error while loading shared libraries:libmysqlclient.so.18 cannot opent shared object file: No such file or directory
这主要是因为你安装库后,没有配置相应的环境变量.可以通过连接修正这个问题
C/C++ Code 复制内容到剪贴板- ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
对于64bit Linux,命令为:
C/C++ Code 复制内容到剪贴板- ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
然后再重试. ,即可正常启动sphinx的searchd进程了!
3.修改D:\sphinx\bin\sphinx.conf,我这里列出需要修改的几个:
type = mysql # 数据源,我这里是mysql sql_host = localhost # 数据库服务器 sql_user = root # 数据库用户名 sql_pass = '' # 数据库密码 sql_db = test # 数据库 sql_port = 3306 # 数据库端口
sql_query_pre = SET NAMES utf8 # 去掉此行前面的注释,如果你的数据库是uft8编码的
index test1 { # 放索引的目录 path = D:/sphinx/data/ # 编码 charset_type = utf-8 # 指定utf-8的编码表 charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F # 简单分词,只支持0和1,如果要搜索中文,请指定为1 ngram_len = 1 # 需要分词的字符,如果要搜索中文,去掉前面的注释 ngram_chars = U+3000..U+2FA1F }
# index test1stemmed : test1 # { # path = @CONFDIR@/data/test1stemmed # morphology = stem_en # }
# 如果没有分布式索引,注释掉下面的内容
# index dist1 # { # 'distributed' index type MUST be specified # type = distributed
# local index to be searched # there can be many local indexes configured # local = test1 # local = test1stemmed
# remote agent # multiple remote agents may be specified # syntax is 'hostname:port:index1,[index2[,...]] # agent = localhost:3313:remote1 # agent = localhost:3314:remote2,remote3
# remote agent connection timeout, milliseconds # optional, default is 1000 ms, ie. 1 sec # agent_connect_timeout = 1000
# remote agent query timeout, milliseconds # optional, default is 3000 ms, ie. 3 sec # agent_query_timeout = 3000 # }
# 搜索服务需要修改的部分 searchd { # 日志 log = D:/sphinx/log/searchd.log
# PID file, searchd process ID file name pid_file = D:/sphinx/log/searchd.pid
# windows下启动searchd服务一定要注释掉这个 # seamless_rotate = 1 }
4.导入测试数据
C:\Program Files\MySQL\MySQL Server 5.0\bin>mysql -uroot test<d:/sphinx/example.sql
5.建立索引
D:\sphinx\bin>indexer.exe –all
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoffusing config file ‘./sphinx.conf’…
indexing index ‘test1′…
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.101 sec, 1916.30 bytes/sec, 39.72 docs/secD:\sphinx\bin>
6.搜索’test’试试
D:\sphinx\bin>search.exe test
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoffusing config file ‘./sphinx.conf’…
index ‘test1′: query ‘test ‘: returned 3 matches of 3 total in 0.000 secdisplaying matches:
1. document=1, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
id=1
group_id=1
group_id2=5
date_added=2008-11-26 14:58:59
title=test one
content=this is my test document number one. also checking search within
phrases.
2. document=2, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
id=2
group_id=1
group_id2=6
date_added=2008-11-26 14:58:59
title=test two
content=this is my test document number two
3. document=4, weight=1, group_id=2, date_added=Wed Nov 26 14:58:59 2008
id=4
group_id=2
group_id2=8
date_added=2008-11-26 14:58:59
title=doc number four
content=this is to test groupswords:
1. ‘test’: 3 documents, 5 hits
D:\sphinx\bin>都所出来了吧。
6.测试中文搜索
修改test数据库中documents数据表,
UPDATE `test`.`documents` SET `title` = ‘测试中文’, `content` = ‘this is my test document number two,应该搜的到吧’ WHERE `documents`.`id` = 2;
重建索引:
D:\sphinx\bin>indexer.exe –all
搜索’中文’试试:
D:\sphinx\bin>search.exe 中文
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoffusing config file ‘./sphinx.conf’…
index ‘test1′: query ‘中文 ‘: returned 0 matches of 0 total in 0.000 secwords:
D:\sphinx\bin>貌似没有搜到,这是因为windows命令行中的编码是gbk,当然搜不出来。我们可以用程序试试,在D:\sphinx\api下新建一个foo.php的文件,注意utf-8编码
<?php
require ‘sphinxapi.php’;
$s = new SphinxClient();
$s->SetServer(‘localhost’,3312);
$result = $s->Query(‘中文’);
var_dump($result);
?>启动Sphinx searchd服务
D:\sphinx\bin>searchd.exe
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew AksyonoffWARNING: forcing –console mode on Windows
using config file ‘./sphinx.conf’…
creating server socket on 0.0.0.0:3312
accepting connections执行PHP查询:
php d:/sphinx/api/foo.php
结果是不是出来?剩下的工作就是去看手册,慢慢摸索高阶的配置