网上看到的教程写的内容不全,导致出现一些问题,于是自己写了一个完整的方法,也提及各部分可能出现的问题。我用的版本是2.0.6.
1. 解压缩到c:\sphinx后,在c:\sphinx\创建data目录和log目录,用mysql数据库导入example.sql。
2. 把sphinx.conf.in拷贝到c:\sphinx\bin里面文件名为sphinx.conf,修改该文件内容数据库的用户名和密码为mysql的用户名密码,把文件中全部@CONFDIR@改为c:/sphinx/。(有人说windows要注释掉pid_file=C:/sphinx/log/searchd.pid)更多修改说明见附1
(coreseek是修改./etc/csft.conf等)
3. 用管理员身份打开命令行:进入c:\sphinx\bin执行searchd --
install
--config
c:\sphinx\bin\sphinx.conf --servicename SphinxSearch
(注意:conf文件的地址必须为绝对地址,否则启动服务会出现错误1067,删除服务方法:sc delete SphinxSearch)
4. Sphinx服务在没有创建任何索引时是不能启动的,所以我们需要先建立索引,执行indexer -c c:\sphinx\bin\sphinx.conf --all来创建全部索引,如果在输出中没有任何警告或错误提示就表示创建成功了
5. net start SphinxSearch启动服务, net stop SphinxSearch停止服务。停止服务时能进行上面一步的建立索引,否则在建立索引的时候,在编制索引时加上–-rotate参数,才可以不停止sphinx服务器来编制索引。(服务启动失败可以查看是否端口被占用netstat -aon|findstr"9312")
6. 测试:search test
7. 用php测试,首先要把xampp的php.exe加入环境变量。Php的api在c:\sphinx\api\sphinxapi.php里面,所以自己写的test.php或用提供的范例php文件要与sphinxapi.php在同一个目录。一般要有以下几个内容<?php
include 'sphinxapi.php'; // 加载Sphinx API
$sc = new SphinxClient(); // 实例化Api
$sc->setServer('localhost', 9312); // 设置服务端,第一个参数sphinx服务器地址,第二个sphinx监听端口
$res = $sc->query('sphinx', 'mysql'); // 执行查询,第一个参数查询的关键字,第二个查询的索引名称,mysql索引名称(这个也是在配置文件中定义的),多个索引名称以,分开,也可以用*表示所有索引。
print_r($res);
?>
如果出现Query failed:connection to localhost:9312 failed (errno=10060, msg=由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。).
1、如果本地使用IPV4、IPV6双栈网络(windows 7),默认localhost 指向IPV6 如下图
修改test.php 将localhost 修改为127.0.0.1, 问题得到解决。
有关更好支持中文检索的coreseek内容可从http://www.coreseek.cn/products/products-install/install_on_windows/和http://www.coreseek.cn/docs/coreseek_3.2-sphinx_0.9.9.html#installing-windows寻找。
附1:
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
}
转载请注明出处。
参考文献:
http://hi.baidu.com/tianhuimin/item/6201b76d1984f409a0cf0f66
http://blog.csdn.net/21aspnet/article/details/6740510