本文将讲述:sphinx在windows下的安装配置;sphinx与PHP的实时索引使用;sphinx服务加入windows服务。
参考手册:http://www.coreseek.cn/docs/coreseek_4.1-sphinx_2.0.1-beta.html
一、sphinx在windows下的安装配置
1.下载地址:
http://sphinxsearch.com/downloads/ 尽量下高一点的版本,我用的Sphinx 2.0.6-release,
下载后解压到,f:/sphinx(根据你的情况安装).
将sphinx.conf.in拷贝重名到/bin/sphinx.conf
新建:f:/sphinx/data 文件夹
新建:f:/sphinx/log 文件夹
2.修改配置文件(详细配置见http://blog.csdn.net/design321/article/details/8895608)
#
# Minimal Sphinx configuration sample (clean, simple, functional)
#
source src1
{
#数据库信息
type = mysql
sql_host = localhost
sql_user = root
sql_pass = 123456
sql_db = test
sql_port = 3306
sql_query_pre = SET NAMES utf8
sql_query = SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content FROM documents
sql_attr_uint = group_id
sql_attr_timestamp = date_added
sql_query_info = SELECT * FROM documents WHERE id=$id
}
index test2
{
source = src1
path = f:/sphinx/data/test1 //新建的data目录
docinfo = extern
charset_type = utf-8
min_prefix_len = 0
min_infix_len = 0
min_word_len = 1
ngram_len = 1
ngram_chars = U+3000..U+2FA1F
charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
}
indexer
{
mem_limit = 32M
}
searchd
{
port = 3312
log =f:/sphinx/log/searchd.log #新建的log目录
#query_log = F:/sphinx/log/query.log #新建的log目录
read_timeout = 5
max_children = 30
pid_file = f:/sphinx/log/searchd.pid #新建的log目录
max_matches = 1000
preopen_indexes = 0
unlink_old = 1
seamless_rotate = 0
}
# eof
配置完成
3.导入测试数据到mysql
将example.sql文件在test数据库执行。
注意,导入的数据非utf8格式,需要自已去数据库改一下字条集。
4.建立索引
F:\sphinx\bin>indexer.exe --config f:/sphinx/bin/sphinx.conf --all
Sphinx 2.0.6-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file 'f:/sphinx/bin/sphinx.conf'...
indexing index 'test2'...
collected 3 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 3 docs, 138 bytes
total 0.011 sec, 12402 bytes/sec, 269.61 docs/sec
total 2 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
total 9 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
5.查找
F:\sphinx\bin>search.exe test
得到以上内容,安装成功。
二、让PHP 能使用sphinx
1.启动sphinx服务
F:\sphinx\bin>searchd.exe
2,将F:\sphinx\api\sphinxapi.php拷贝到apach下的网站目录,以供调用.
3.利用现有索引的简单示例
SetServer('127.0.0.1',3312); //服务器名,sphinx端口号
$result = $s->Query('test','test2'); //关键词,索引名(与配置文件里一致,为*时表示全部)
echo '';
var_dump($result);
echo '';
?>
4.创建实时索引的示例
SetServer("localhost",3312);
$sphinx->SetMatchMode(SPH_MATCH_ANY);
//$sphinx->setLimits(0,0); //偏移量
$result=$sphinx->query("$keyword","test2");
//echo "";
//print_r($result);
//echo "
";
$ids=join(",",array_keys($result['matches']));
mysql_connect("localhost","root","123456");
mysql_select_db("test");
$sql="select * from documents where id in({$ids})";
mysql_query("set names utf8");
$rst=mysql_query($sql);
$opts=array(
"before_match"=>""
);
while($row=mysql_fetch_assoc($rst)){
$rst2=$sphinx->buildExcerpts($row,"main",$keyword,$opts);
echo '';
var_dump($row);
echo '';
/* echo "第{$rst2[0]}篇贴子
";
echo "标题: {$rst2[1]}
";
echo "内容: {$rst2[2]}
";
echo "
"; */
}
三、将sphinx服务添加到windows服务
F:\sphinx\bin>searchd.exe --install -c f:\sphinx\bin\sphinx.conf --servicename s
F:\sphinx\bin>net start sphinx
F:\sphinx\bin>net stop sphinx
sphinx 服务已成功停止。
F:\sphinx\bin>sc delete sphinx
end.