sphinx搜索结果按权重排序php,PHP读取sphinx 搜索返回结果完整实战实例

PHP读取sphinx 搜索返回结果完整实战实例

网上搜索N久都没有一个正在读取返回sphinx结果的实例,都是到了matches那里就直接var_dump或者print_r了,没有读取到字段的例子,困扰了很久

结果分析测试最终搞出来了,这里分享下,其他的网上有的就不多说了,直接上代码吧(CI框架的)

Model类方法:

function getResultByTag($keyword = "") {

$query = array ();

try {

$cl = new SphinxClient ();

$cl->SetServer ( 'localhost', 9312 ); // 注意这里的主机

$cl->SetMatchMode ( SPH_MATCH_EXTENDED ); // 使用多字段模式

$index = "site";

$query = $cl->Query ( $keyword, $index );

$cl->close ();

// $query = $result ['matches'];

// if ($result ['total'] > 0) {

// // var_dump($result ['matches']);

// // 根据打印出的结果进行相应的读取

// }

} catch ( Exception $e ) {

}

return $query;

}

Control类方法

$result = $this->tagsmodel->getsitesByTag($name);

if ($result['total'] > 0) {

$sites = $result['matches'];

}else{

$sites = array ();

}

$data['total'] = $result['total'];

$data['sites'] = $sites;

View 对应代码

1 <?php foreach($sites as $row){ $site = $row["attrs"]; ?>

2

3 " href="=$site["url"]?>" target="_blank">=$site["sname"]?>

4 更新时间:=date("Y-m-d H:i",$site["update_time"])?>

5

6 <?php }?>

为了更直观也把dump 的结果贴出来(去掉了部分结果内容)

array(10) {

["error"]=>

string(0) ""

["warning"]=>

string(0) ""

["status"]=>

int(0)

["fields"]=>

array(3) {

[0]=>

string(7) "orderid"

[1]=>

string(11) "description"

[2]=>

string(7) "content"

}

["attrs"]=>

array(16) {

["id2"]=>

int(1)

["sname"]=>

int(7)

["url"]=>

int(7)

["sitecateg"]=>

int(7)

["title"]=>

int(7)

["keywords"]=>

int(7)

["description"]=>

int(7)

["content"]=>

int(7)

["thumb"]=>

int(7)

["snapshoot"]=>

int(7)

["tags"]=>

int(7)

["area"]=>

int(7)

["ishot"]=>

int(1)

["show_index"]=>

int(1)

["update_time"]=>

int(2)

["create_time"]=>

int(2)

}

["matches"]=>

array(20) {

[60]=>

array(2) {

["weight"]=>

string(4) "1672"

["attrs"]=>

array(16) {

["id2"]=>

int(60)

["sname"]=>

string(10) "21CN新闻"

["url"]=>

string(20) "http://news.21cn.com"

["sitecateg"]=>

string(18) "网上新闻媒体"

["title"]=>

string(17) "新闻频道-21CN"

["keywords"]=>

string(36) "新闻,新闻频道,21CN新闻频道"

["description"]=>

string(177) "21CN新闻,关注最有价值的新闻;新闻频道,包含有国内新闻,国际新闻,社会新闻,新闻评论,新闻图片,新闻专题,的新闻资讯聚合门户网站。"

["content"]=>

string(0) ""

["thumb"]=>

string(0) ""

["snapshoot"]=>

string(0) ""

["tags"]=>

string(0) ""

["area"]=>

string(0) ""

["ishot"]=>

int(0)

["show_index"]=>

int(1)

["update_time"]=>

int(1382241483)

["create_time"]=>

int(1382192160)

}

}

[45]=>

array(2) {

["weight"]=>

string(4) "1670"

["attrs"]=>

array(16) {

["id2"]=>

int(45)

["sname"]=>

string(12) "腾讯新闻"

["url"]=>

string(18) "http://news.qq.com"

["sitecateg"]=>

string(18) "网上新闻媒体"

["title"]=>

string(22) "新闻中心_腾讯网"

["keywords"]=>

string(55) "新闻 新闻中心 事实派 新闻频道,时事报道"

["description"]=>

string(220) "腾讯新闻,事实派。新闻中心,包含有时政新闻、国内新闻、国际新闻、社会新闻、时事评论、新闻图片、新闻专题、新闻论坛、军事、历史、的专业时事报道门户网站"

["content"]=>

string(0) ""

["thumb"]=>

string(0) ""

["snapshoot"]=>

string(0) ""

["tags"]=>

string(0) ""

["area"]=>

string(0) ""

["ishot"]=>

int(0)

["show_index"]=>

int(1)

["update_time"]=>

int(1382241430)

["create_time"]=>

int(1382192109)

}

}

}

["total"]=>

string(3) "153"

["total_found"]=>

string(3) "153"

["time"]=>

string(5) "0.000"

["words"]=>

array(1) {

["新闻"]=>

array(2) {

["docs"]=>

string(3) "153"

["hits"]=>

string(3) "285"

}

}

}

把直接print_r的部分结果也贴出来:

Array ( [error] => [warning] => [status] => 0 [fields] => Array ( [0] => orderid [1] => description [2] => content ) [attrs] => Array ( [id2] => 1 [sname] => 7 [url] => 7 [sitecateg] => 7 [title] => 7 [keywords] => 7 [description] => 7 [content] => 7 [thumb] => 7 [snapshoot] => 7 [tags] => 7 [area] => 7 [ishot] => 1 [show_index] => 1 [update_time] => 2 [create_time] => 2 ) [matches] => Array ( [60] => Array ( [weight] => 1672 [attrs] => Array ( [id2] => 60 [sname] => 21CN新闻 [url] => http://news.21cn.com [sitecateg] => 网上新闻媒体 [title] => 新闻频道-21CN [keywords] => 新闻,新闻频道,21CN新闻频道 [description] => 21CN新闻,关注最有价值的新闻;新闻频道,包含有国内新闻,国际新闻,社会新闻,新闻评论,新闻图片,新闻专题,的新闻资讯聚合门户网站。 [content] => [thumb] => [snapshoot] => [tags] => [area] => [ishot] => 0 [show_index] => 1 [update_time] => 1382241483 [create_time] => 1382192160 ) ) [2033] => Array ( [weight] => 1648 [attrs] => Array ( [id2] => 2033 [sname] => 百度财经 [url] => http://finance.baidu.com [sitecateg] => 财经综合 [title] => 百度新闻搜索──财经新闻 [keywords] => [description] => 百度新闻是包含海量资讯的新闻服务平台,真实反映每时每刻的新闻热点。您可以搜索新闻事件、热点话题、人物动态、产品资讯等,快速了解它们的最新进展。 [content] => [thumb] => [snapshoot] => [tags] => [area] => [ishot] => 0 [show_index] => 0 [update_time] => 1382780728 [create_time] => 1382199044 ) ) ) [total] => 153 [total_found] => 153 [time] => 0.000 [words] => Array ( [新闻] => Array ( [docs] => 153 [hits] => 285 ) ) )

这下大家应该就知道怎么读取自己的搜索结果了,其他网上大把的资料有的这里就不写了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值