安装和使用coreseek
一般网站需要搜索功能。 对于php mysql站点,建议选择coreseek。 对于java站点,建议使用lucene。 coreseek是一款优秀的中文全文搜索/检索软件,支持快速索引、强大的可扩展性和分布式搜索,支持不同的搜索模式“‘完全匹配’、‘短语匹配’”
标志1
如果以下步骤遇到问题,请参阅安装coreseek时出现的错误和问题的解决方案。 这里总结了安装和配置中出现的常见问题和解决方案。
一. coreseek安装
安装mmseg分词典
wget 3358 www.coreseek.cn/uploads/csft/4.0/coreseek-3.2.14-beta.tar.gz
tar-zxvf coreseek-3.2.14-beta.tar.gz
CDCoreseek-3.2.14-贝塔
cd mmseg-3.2.14
./bootstrap
./configure-- prefix=/usr/local/MMSE G3 #配置安装目录
化妆
制造安装
cd .
安装coreseek
cd csft-3.2.14
sh buildconf.sh
./configure---- prefix=/usr/local/coreseek---- without-unixodbc---- with-mmseg-- with-mmseg-incinch
化妆
制造安装
cd .
3 )测试mmseg分词和coreseek检索
CD/usr/local/src/coreseek-3.2.14
CD测试包
cat var/test/test.xml #现实的是中文内容
显示/usr/local/MMSE G3/wxdjz/mmseg-d/usr/local/MMSE G3/etc var/test/test.XML # XML文件的内容
/usr/local/coreseek/wxdjz/indexer-cetc/csft.conf--创建all #索引
在/usr/local/coreseek/wxdjz/search-cetc/csft.conf #中搜索
二. coreseek的构成和使用
配置example.conf文件
cd /usr/local/coreseek/etc
cpsphinx-min.conf.dist example.conf
改造example.conf的配置,使其成为我们需要的配置文件。
source example
{
type=mysql
sql_host=localhost
sql_user=***
sql_pass=***
sql_db=***
sql_port=3306
sql_sock=/tmp/mysql.sock
sql_query_pre=SET NAMES utf8
无论表的主键被称为什么,sql_query=SELECT id,content FROM table_name #的检查结果都是id
对于SQL _ query _ info _ pre=setnamesutf8#命令行查询,请设置正确的字符集
对于SQL _ query _ info=select * fromtablewhereid=$ id limit 100 #命令行查询,从数据库中读取原始数据信息
}
#索引定义
索引xample
{
与source=example #对应的source名称
path=/usr/local/coreseek/var/data/example/#请修改为实际使用的绝对路径
docinfo=extern
mlock = 0
morphology = none
min_word_len = 1
html_strip = 0
#中文分词配置,详情请查看:http://www.coreseek.cn/products-install/coreseek_mmseg/
charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux环境下设置,/符号结尾
#charset_dictpath = etc/ #Windows环境下设置,/符号结尾,最好给出绝对路径,
charset_type = zh_cn.utf-8
}
#全局index定义
indexer
{
mem_limit = 128M
}
#searchd服务定义
searchd
{
listen = 9312
read_timeout = 5
max_children = 60
max_matches = 1000
seamless_rotate = 0
preopen_indexes = 0
unlink_old = 1
pid_file = /usr/local/coreseek/var/log/searchd_example.pid #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
log = /usr/local/coreseek/var/log/searchd_example.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
query_log = /usr/local/coreseek/var/log/query_example.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
}
2:开启searchd服务 (这点很重要)
coreseek刚安装后searchd服务是没开启的,需要先开启,不然创建索引的时候会出错。
先在 /usr/local/src/coreseek/var/log下创建一个searchd_example.pid文件,然后执行下面的代码:
/usr/local/coreseek/wxdjz/searchd --config /usr/local/coreseek/etc/example.conf
3:创建索引
/usr/local/coreseek/wxdjz/indexer -c /usr/local/coreseek/etc/example.conf --all --rotate
出现下面的提示:
Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
Copyright (c) 2007-2011,
Beijing Choice Software Technologies Inc (http://www.coreseek.com)
using config file '/usr/local/coreseek/etc/example.conf'...
indexing index 'example'...
collected 1 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 1 docs, 631 bytes
total 0.005 sec, 118854 bytes/sec, 188.35 docs/sec
total 1 reads, 0.000 sec, 0.8 kb/call avg, 0.0 msec/call avg
total 5 writes, 0.000 sec, 0.5 kb/call avg, 0.0 msec/call avg
rotating indices: succesfully sent SIGHUP to searchd (pid=14094).
说明coreseek已经可以使用了。
可以把创建索引的命令写入crontab里面进行定时,实时更新索引。
4:php 和 coreseek 结合进行查询
先下载sphinxapi,然后文件引入结合php进行查询,下面更多参数的详解和配置,可以参考 coreseek api php手册
require_once('sphinxapi');
$s = new SphinxClient();
$s->SetServer('127.0.0.1','9312'); //设置searchd的主机名和TCP端口
$s->SetConnectTimeout(1); // 设置连接超时
$s->SetMatchMode(SPH_MATCH_BOOLEAN); //设置全文查询的匹配模式
$page_size=20;//自己定义的页数
$s->SetLimits($start,$page_size); //设置返回结果集偏移量和数目
$s->SetSortMode( SPH_SORT_EXTENDED,"good_count DESC, @id DESC" ); // 设置排序
$s->SetArrayResult(true);//控制搜索结果集的返回格式
$res = $s->Query($keyword,'*');// 执行搜索查询
$res_list = $res['matches'];
安装遇到的错误和问题解决方案
1、测试mmseg分词的时候
执行
/usr/local/coreseek/wxdjz/indexer -c etc/csft.conf --all
提示下面的错误:
/usr/local/coreseek/wxdjz/indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
原因:sphinx indexer的依赖库ibmysqlclient.so.18找不到。
解决办法:
vi /etc/ld.so.conf
加入 /usr/local/mysql/lib
然后运行 ldconfig
问题解决
2、执行索引的时候
/usr/local/coreseek/wxdjz/indexer -c /usr/local/coreseek/etc/csft_ttd_search.conf --all --rotate
提示下面的错误:
FATAL: failed to open /usr/local/coreseek/var/data/ttd_article/.tmp.spl: No such file or directory, will not index. Try --rotate option.
原因:source源找不到mysql.sock
解决办法:在配置文件csft_ttd_search.conf(自己创建的文件)的 source源 加入下面的代码
sql_sock = /tmp/mysql.sock
3、执行索引的时候,出现的警告,导致索引没创建成功
WARNING: failed to open pid_file '/usr/local/coreseek/var/log/searchd_ttd_search.pid'.
WARNING: indices NOT rotated.
原因:找不到searchd_ttd_search.pid文件
解决办法:在’/usr/local/coreseek/var/log 下创建searchd_ttd_search.pid文件
再执行/usr/local/coreseek/wxdjz/indexer -c /usr/local/coreseek/etc/csft_ttd_search.conf –all –rotate
出现了另外一个警告:
WARNING: failed to scanf pid from pid_file '/usr/local/coreseek/var/log/searchd_ttd_search.pid'.
WARNING: indices NOT rotated.
原因:虽然创建了searchd_ttd_search.pid文件,但是里面没有写入进程id
解决办法(根本原因):在执行索引之前没有启动searchd服务,因此执行下面的命令
/usr/local/coreseek/wxdjz/searchd --config /usr/local/coreseek/etc/ttd_search.conf
出现了期待已久的成功提示:
Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
Copyright (c) 2007-2011,
Beijing Choice Software Technologies Inc (http://www.coreseek.com)
using config file '/usr/local/coreseek/etc/ttd_search.conf'...
listening on all interfaces, port=9312
rotating index 'mysql': success