一。下载与安装:
sphinx的下载地址:http://sphinxsearch.com/downloads/release/ 不支持中文搜索
(我使用的是coreseek 3.2.14 win32版,在win7 64位的环境下测试的)
coreseek的下载地址:http://down.51cto.com/data/643304 这个应该是可以用的,支持中文搜索
二。配置文件:
把sphinx/sphinx.conf.in文件复制到bin目录下,改名为什么sphinx.conf
coreseek的配置文件,默认文件名是csft.conf,放在bin目录下
当然你也可以用其它文件名,在建议索引和启动searchd服务的时候要指定配置文件。
打开文件后修改一些配置:
#MySQL数据源配置,详情请查看:http://www.coreseek.cn/products-install/mysql/
#请先将var/test/documents.sql导入数据库,并配置好以下的MySQL用户密码数据库
#很多地方引用的资料都是来自www.coreseek.cn或者是否www.coreseek.com的,但现在这两个网站都打不开了。
#源定义
source mysql
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = root
sql_db = test
sql_port = 3306
sql_query_pre = SET NAMES utf8
sql_query_pre =SET SESSION query_cache_type=OFF //关闭缓存
#插入文章表的最大id
sql_query_pre = REPLACE INTO acticle_new SELECT 1, MAX(id) FROM acticle
sql_query = SELECT id, title,content FROM acticle
#sql_query第一列id需为整数
#title、content作为字符串/文本字段,被全文索引
#sql_attr_uint = group_id #从SQL读取到的值必须为整数
#sql_attr_timestamp = date_added #从SQL读取到的值必须为整数,作为时间属性
sql_query_info_pre = SET NAMES utf8 #命令行查询时,设置正确的字符集
sql_query_info = SELECT * FROM acticle WHERE id=$id #命令行查询时,从数据库读取原始数据信息
}
#增量数据源
source new:mysql
{
sql_query_pre = SET NAMES utf8
sql_query_pre =SET SESSION query_cache_type=OFF
sql_query=select id,title,content from acticle where id>(select newid from acticle_new where id=1);
#sql_query_post = REPLACE INTO acticle_new SELECT 1, MAX(id) FROM acticle
#sql_query_post_index = REPLACE INTO acticle_new SELECT 1, MAX(id) FROM acticle
}
#index 主索引定义
index mysql
{
source = mysql #对应的source名称
path = E:/coreseek/var/data/mysql #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
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 = E:/coreseek/etc/ #Windows环境下设置,/符号结尾,最好给出绝对路径,例如:C:/usr/local/coreseek/etc/...
charset_type = zh_cn.utf-8
}
#增量索引
index new:mysql
{
type=rt
source = new #对应的source名称
path = E:/coreseek/var/data/new #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
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 = E:/coreseek/etc/ #Windows环境下设置,/符号结尾,最好给出绝对路径,例如:C:/usr/local/coreseek/etc/...
charset_type = zh_cn.utf-8
}
#全局index定义
indexer
{
mem_limit = 128M
}
#searchd服务定义
searchd
{
listen = 9312
read_timeout = 5
max_children = 30
max_matches = 1000
seamless_rotate = 0
preopen_indexes = 0
unlink_old = 1
pid_file = E:/coreseek/var/log/searchd_mysql.pid #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
log = E:/coreseek/var/log/searchd_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
query_log = E:/coreseek/var/log/query_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
}
数据源:我这里用的是test.acticle的表,字段包括id(必须,主键索引,自增), tittle, content, 其它字段
(因为其它字段不在搜索的的范围,所以建立索引的时候不添加进来)
用了一个纪录主索引最大id的表:test.acticle_new ,字段有id 和 newid
三。启动服务
建立索引:indexer -c sphinx.conf (加--all,建立所有索引,也可以指定索引 indexer mysql)
报错:FATAL: failed to open @CONFDIR@/data/test1.spl: No such file or directory, will not index. Try --rotate option.
(不存在 pid文件,或者是不存在sph文件)
解决方法:
1)把sphinx.conf文件中的所有@CONFDIR@替换成data文件夹所在的绝对路径
2)重建索引文件: indexer -c csft.conf --all --rotate
生成索引文件:searchd --config csft.conf --pidfile
参考:http://blog.csdn.net/lgm252008/article/details/7637673
启动服务:searchd -c sphinx.conf --console (如果不加--console,会报错,各种文件找不到)
[都是默认的配置,则可以不加参数,直接searchd可以开启服务]
cmd中进行搜索:--测试未通过(没有search命令->补充:这个要看版本,如果bin目录下存在search.exe文件的,则是可以在cmd中进行搜索的)
四。php调用
require "sphinxapi.php";
$cl = new SphinxClient();
$cl->SetServer('localhost', 9312); //注意这里的主机
$cl->SetArrayResult ( true );//添加这个后,返回的结果可读性更强
$index='test1';
$keyword="test";
$res = $cl->Query($keyword, $index);
$err = $cl->GetLastError();
var_dump($res);
//数组形式的返回结果:
["matches"]=>
array(3) {
[0]=>
array(3) {
["id"]=>
int(1)
["weight"]=>
string(4) "2421"
["attrs"]=>
array(2) {
["group_id"]=>
string(1) "1"
["date_added"]=>
string(10) "1472115411"
}
}
[1]=>
array(3) {
["id"]=>
int(2)
["weight"]=>
string(4) "2421"
["attrs"]=>
array(2) {
["group_id"]=>
string(1) "1"
["date_added"]=>
string(10) "1472115411"
}
}
[2]=>
array(3) {
["id"]=>
int(4)
["weight"]=>
string(4) "1442"
["attrs"]=>
array(2) {
["group_id"]=>
string(1) "2"
["date_added"]=>
string(10) "1472115411"
}
}
}
一般两种调用方式:
一。include 类文件
二。把coreseek(或者是sphinx添加到php拓展)
**三。直接把sphinx编译到mysql中
这里用的是最简单的第一种方式
五。返回结果:
未添加$cl->SetArrayResult ( true );时的返回结果:
array(10) {
["error"]=>
string(0) ""
["warning"]=>
string(0) ""
["status"]=>
int(0)
["fields"]=>
array(2) {
[0]=>
string(5) "title"
[1]=>
string(7) "content"
}
["attrs"]=>//配置文件中设置的属性
array(2) {
["group_id"]=>
int(1)
["date_added"]=>
int(2)
}
["matches"]=>//这里是返回的结果
array(3) {
[1]=> //对应的id
/**
matches中的ID就是指配置文件中sql_query SELECT语句中的第一个字段,我们配置文件中是这样的
sql_query = SELECT emailid,fromid,toid,subject,content,sendtime,attachement FROM email
所以matches中的ID是指emailid
**/
array(2) {
["weight"]=>
string(4) "2421"//权重
["attrs"]=>
array(2) {
["group_id"]=>
string(1) "1"
["date_added"]=>
string(10) "1472115411"
}
}
[2]=>
array(2) {
["weight"]=>
string(4) "2421"
["attrs"]=>
array(2) {
["group_id"]=>
string(1) "1"
["date_added"]=>
string(10) "1472115411"
}
}
[4]=>
array(2) {
["weight"]=>
string(4) "1442"
["attrs"]=>
array(2) {
["group_id"]=>
string(1) "2"
["date_added"]=>
string(10) "1472115411"
}
}
}
["total"]=>//结果总数
string(1) "3"
["total_found"]=>
string(1) "3"
["time"]=>
string(5) "0.055"
["words"]=>
array(1) {
["test"]=>//搜索的关键词
array(2) {
["docs"]=>
string(1) "3"
["hits"]=>
string(1) "5"
}
php手册中关于返回值的说明:
"matches" | 存储文档ID以及其对应的另一个包含文档权重和属性值的hash表 |
"total" | 此查询在服务器检索所得的匹配文档总数(即服务器端结果集的大小,且与相关设置有关)//实际返回的总数 |
"total_found" | (服务器上找到和处理了的)索引中匹配文档的总数//索引中的总数 |
"words" | 将查询关键字(关键字已经过大小写转换,取词干和其他处理)映射到一个包含关于关键字的统计数据(“docs”——在多少文档中出现,“hits”——共出现了多少次)的小hash表上。 |
"error" | searchd报告的错误信息 |
"warning" | searchd报告的警告信息 |
注:返回结果中,需要用到的可以有三个:
1)total返回的结果数,如果为0就不用看其它参数了
2)如果total大于1,可能需要搜索出结果来,所在要在matches中把结果id拿到,再去mysql中搜索
3)关键词高亮显示的话,还需要words
六。仍然存在的问题:
1)用searchd.exe --install --config命令把searchd加入到系统服务中后,服务并没有启动,手动启动仍然会报错?
参考:searchd的命令参考
七。Windows下启动计划任务:
脚本:new.cmd
@CLS
@e:
@cd E:\coreseek\bin
@indexer new --rotate
@%ComSpec% /Q /K ECHO. #这一行只在测试的时候加,会有一个弹窗,去掉则会自动关闭cmd窗口
计划任务中:周期选择“每天”,设置好后再在“触发器”的“编辑”中选择“重复任务时间间隔”为5分钟
按照以上方式,再建一个每天晚上启动一次的“重复主索引"的计划任务
八。参考:
http://www.php100.com/html/itnews/it/2013/0505/13435.html 这文章写得不错
http://cn.php.net/manual/zh/book.sphinx.php php手册
http://www.zzbaike.com/wiki/Sphinx 不错的资源
http://www.cnblogs.com/yjf512/p/3598332.html spxinx配置文件全解析
http://www.cnblogs.com/GaZeon/p/5327578.html 有关Linux下使用coreseek不错的文章