1. 配置索引
cd /usr/local/sphinx/etc/
cp sphinx.conf.dist sphinx.conf //备份配置文件,防止改错
vim sphinx.conf
配置文件结构:
# 主数据源,(main名字可更改)
source main{
type = mysql #数据库类型
sql_host = localhost #MySQL主机IP
sql_user = test #MySQL用户名
sql_pass = #MySQL密码
sql_db = test #MySQL数据库
sql_port = 3306 #MySQL端口
sql_sock = /tmp/mysql.sock #Linux下需要开启,指定sock文件
sql_query_pre = SET NAMES utf8 #MySQL检索编码
sql_query_pre = SET SESSION query_cache_type=OFF #关闭缓存
sql_query = #获取数据的SQL语句
SELECT id, title, content FROM post
# 以下是用来过滤或条件查询的属性,这里列出的字段将可以进行条件查询,同时不参与全文检索
#sql_attr_uint = group_id
#sql_attr_timestamp = date_added
}
# 增量数据源(inherited source), 继承主数据源
source src1throttled : main{
}
# 主索引(local index),(main名字可更改)
index main{
source = main # 指定主数据源
path = /usr/local/sphinx/var/data/main # 索引路径
}
# 增量索引(inherited index)
index test1stemmed : test1{
}
# 分布式索引(distributed index)
index dist1{
}
# 实时索引(realtime index)
index rt{
}
# 索引器设置,(调整最小内存到最佳)
indexer{
mem_limit = 256M #内存大小限制,默认128M,推荐256M
#其它用默认即可
}
# 服务进程设置,(监听端口号)
searched{
#全部默认即可,默认端口号就是9312
}
# 公共配置
common{
}
2. 创建索引
创建索引命令:indexer
-c 指定配置文件
–all 对所有索引重新编制索引
–rotate 用于轮换索引,在不停止服务的时候(searchd运行时)增加索引;searchd运行时不加会报错。
–merge 合并索引,增量索引合并到主索引的时候用
生成全部索引: /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf –all
或指定索引(例如main): /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf main
(1)如果这里出现报错:
【ERROR: index ‘main’: sql_connect: Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’】
没找到/tmp/mysql.sock, 通过find / -name mysql.sock -print查找到位置,在配置sphinx.conf里更改正确。
如:mysql_sock = /var/lib/mysql/mysql.sock 保存退出。
(2)继续创建索引,警告:
【WARNING: Attribute count is 0: switching to none docinfo】
改sphinx.conf里的docinfo = none就没有警告了。(http://sphinxsearch.com/docs/current.html#conf-docinfo)
创建索引出现如下提示,表示生成成功:
3. 启动Sphinx
重建索引:./searchd -c /usr/local/sphinx/etc/sphinx.conf
轮换索引: ./searchd -c /usr/local/sphinx/etc/sphinx.conf goods_list –rotate
./searchd -c /usr/local/sphinx/etc/sphinx.conf store_list –rotate
停止服务:./searchd -c /usr/local/sphinx/etc/sphinx.conf –stop
4. 使用sphinx
在web根目录下建立一个search目录(当然不在根目录也行,同样目录名也可以随取),复制E:coreseekapi sphinxapi.php文件到search目录(sphinxapi.php这个是sphinx官方提供的api),开始php程序的编写。
在search目录建立一个文件,名字叫啥都行,我管它叫index.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);
打印结果
Array
(
………省略………
[matches] => Array
(
[2] => Array
(
[weight] => 2
[attrs] => Array
(
[addtime] => 1282622004
)
)
[4] => Array
(
[weight] => 2
[attrs] => Array
(
[addtime] => 1282622079
)
)
)
………省略………
)
Matches中就是查询的结果了,但是仿佛不是我们想要的数据,比如titile,content字段的内容就没有查询出来,根据官方的说明是Sphinx并没有连接到MySQL去取数据,只是根据它自己的索引内容进行计算,因此如果想用Sphinx提供的API去取得我们想要的数据,还必须以查询的结果为依据,再次查询MySQL从而得到我们想要的数据。
查询结果中键值分别表示
2唯一主键
weight权重
attrs sql_attr_*中配置
至此,搜索引擎算是完成一大半了,剩下的大家可以自行完成。
比如:
<?php
$ids = array_keys($res[‘matches’]); // 获取主键
$ids = join(‘,’, $ids);
$query = mysql_query(“SELECT * FROM post WHERE id IN ({$ids})”);
while($row = mysql_fetch_assoc($query)) {
…..
}



