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

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

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

可以查看本人百度经验:php CI 实战教程:[1]完整解析sphinx返回结果_百度经验
http://jingyan.baidu.com/article/6f2f55a18436a2b5b83e6c46.html

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 对应代码

 <?php foreach($sites as $row){ $site = $row["attrs"]; ?>
<div class="listitem">
<a title="<?=$site["sname"]?>" href="<?=$site["url"]?>" target="_blank"><?=$site["sname"]?></a>
<span>更新时间:<?=date("Y-m-d H:i",$site["update_time"])?></span>
</div>
<?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 ) ) )

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

PHP读取sphinx 搜索返回结果完整实战实例的更多相关文章

  1. BI之SSAS完整实战教程7 -- 设计维度、细化维度中 :浏览维度,细化维度

    上篇文章我们已经将Dim Geography维度设计好. 若要查看维度的成员, AS需要接收该维度的详细信息(包括已创建的特性.成员属性以及多级层次结构), 通过XMLA与AS的实例进行通信. 今天我 ...

  2. BI之SSAS完整实战教程4 -- 部署至SSAS进行简单分析

    上一篇已经创建了多维数据集的结构. 接下来我们将多维数据集的架构定义发送到Analysis Services实例,部署到AS上去. 文章提纲 部署和浏览多维数据集 SSMS使用简介 总结 一.部署和浏 ...

  3. Selenium2学习-009-WebUI自动化实战实例-007-Selenium 8种元素定位实战实例源代码(百度首页搜索录入框及登录链接)

    此 文主要讲述用 Java 编写 Selenium 自动化测试脚本编写过程中,通过 ID.name.xpath.cssSelector.linkText.className.partialLinkTe ...

  4. XML解析之sax解析案例(一)读取contact.xml文件,完整输出文档内容

    一.新建Demo2类: import java.io.File; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXPar ...

  5. Java生鲜电商平台-电商中海量搜索ElasticSearch架构设计实战与源码解析

    Java生鲜电商平台-电商中海量搜索ElasticSearch架构设计实战与源码解析 生鲜电商搜索引擎的特点 众所周知,标准的搜索引擎主要分成三个大的部分,第一步是爬虫系统,第二步是数据分析,第三步才 ...

  6. BI之SSAS完整实战教程6 -- 设计维度、细化维度上:创建维度定义特性关系

    前面我们使用过数据源向导.数据源视图向导.Cube向导来创建相应的对象. 本篇我们将学习使用维度向导来创建维度. 通过前面几个向导的学习,我们归纳一下共同点,主要分成两步 1. 使用某种对象类型的向导 ...

  7. BI之SSAS完整实战教程5 -- 详解多维数据集结构

    之前简单介绍过多维数据集(Cube)的结构. 原来计划将Cube结构这部分内容打散,在实验中穿插讲解, 考虑到结构之间不同的部分都有联系,如果打散了将反而不好理解,还是直接一次性全部讲完. 本篇我们将 ...

  8. BI之SSAS完整实战教程2 -- 开发环境介绍及多维数据集数据源准备

    上一篇我们已经完成所有的准备工作,现在我们就开始动手,通过接下来的三篇文章创建第一个多维数据集. 传统的维度和多维数据集设计方法主要是基于现有的单源数据集. 在现实世界中,当开发商业智能应用程序时,很 ...

  9. BI之SSAS完整实战教程1 -- 开篇, BI简介 & SSAS简介

    文章提纲 商业智能(BI, Business Intelligence)基本概念 SSAS(SQL Server Analysis Services)相关工具(开发.管理和客户端) 总结 一.商业智能 ...

随机推荐

  1. Django---项目如何创建

    首先是安装好Django,找到 Scripts 目录配置环境变量: 只要添加到环境变量,在任何目录执行 django-admin startproject mysite 就可以创建 Django 程序 ...

  2. 8.31前端 jQuery

    2018-8-31 19:52:09 周末吧这几天课看完 结束前端!然后进行Django!!! 越努力,越幸运! day56 2018-03-16 1. 内容回顾 1. 样式操作 1. 操作class ...

  3. [实战]MVC5+EF6+MySql企业网盘实战(2)——验证码

    写在前面 断断续续,今天算是把验证码的东东弄出来了. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网盘实战(1) [实战]MVC5 ...

  4. poj1850Code

    Code Transmitting and memorizing information is a task that requires different coding systems for th ...

  5. python2和python3的不同

    1.性能 Py3.0运行 pystone benchmark的速度比Py2.5慢30%.Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可 以取得很好的优化结果. Py3.1性能比Py2 ...

  6. [No00004F]史上最全Vim快捷键键位图(入门到进阶)vim常用命令总结

    在命令状态下对当前行用== (连按=两次), 或对多行用n==(n是自然数)表示自动缩进从当前行起的下面n行.你可以试试把代码缩进任意打乱再用n==排版,相当于一般IDE里的code format.使 ...

  7. SQL Fundamentals: 分组统计查询(FROM-WHERE-GROUPBY-HAVING-SELECT-ORDER BY)

    SQL Fundamentals || Oracle SQL语言 统计函数 单字段分组统计(GROUP BY) 多字段分组统计 HAVING子句 控制操作的显示列:基本的SELECT语句 控制行:限定 ...

  8. 优云软件应邀出席 ITSS 数据中心运营管理工作组 2017 年春季研讨会

    2017 年 4 月 15 日,中国电子工业标准化技术协会信息技术服务分会(以下称 ITSS 分会)数据中心运营管理工作组(以下简称 DCMG)在江苏省启东市召开春季研讨会. DCMG 工作组组长肖建 ...

  9. eclipse的new server里tomcat7.0根本选不上解决方法

    创建Tomcat v7.0 Server 不能进行下一步. 解决方法: 1.退出 eclipse 2.到[工程目录下]/.metadata/.plugins/org.eclipse.core.runt ...

  10. crawlspider爬虫:定义url规则

    spider爬虫,适合meta传参的爬虫(列表页,详情页都有数据要爬取的时候) crawlspider爬虫,适合不用meta传参的爬虫 scrapy genspider -t crawl it it. ...