要使用php连接Sphinx进行全文搜索,有两种方式
- 将Sphinx作为php扩展安装,这种方式比较麻烦,此处不介绍
- 在Sphinx源码目录下的有个api/sphinxapi.php,将此文件reqire到你的php中,即可调用api方法进行全文搜索
php调用示例
<?php
require('sphinxapi.php');
$sphinx = new SphinxClient();
$sphinx->SetServer('localhost',9312);
$sphinx->SetMatchMode(SPH_MATCH_ANY);
$sphinx->SetArrayResult ( true );
$res = $sphinx->Query($_GET['key'],'*');
print_r($res);
数据
id | content | createtime |
---|---|---|
1 | hello world | 2016-11-11 00:36:23 |
2 | php is greater than others | 2016-11-10 00:36:59 |
3 | sphinx search php | 2016-11-11 10:01:50 |
在以上数据中中查询hello php
,结果如下:
查询结果
Array
(
[error] =>
[warning] =>
[status] => 0
[fields] => Array
(
[0] => content
[1] => createtime
)
[attrs] => Array
(
)
[matches] => Array
(
[0] => Array
(
[id] => 1 //查询到的文档ID
[weight] => 1
[attrs] => Array
(
)
)
[1] => Array
(
[id] => 2 //查询到的文档ID
[weight] => 1
[attrs] => Array
(
)
)
[2] => Array
(
[id] => 3 //查询到的文档ID
[weight] => 1
[attrs] => Array
(
)
)
)
[total] => 3
[total_found] => 3
[time] => 0.000
[words] => Array
(
[hello] => Array
(
[docs] => 1 //命中文档数
[hits] => 1 //命中次数
)
[php] => Array
(
[docs] => 2 //命中文档数
[hits] => 2 //命中次数
)
)
)
php:Sphinx文档
php+Sphinx更多API使用方法,请参考官方文档: