zoukankan      html  css  js  c++  java
  • PHP读取sphinx 搜索返回结果完整实战实例

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

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

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

  • 相关阅读:
    一个关于Delphi XML处理单元的BUG
    弹出一个非阻塞对话框
    更新Delphi中SVN客户端版本的方法
    程序只允许运行一个+重复运行程序提前
    Reverse Words in a String
    How to define Servlet filter order of execution using annotations
    Understanding and Using Servlet Filters
    Observer Pattern
    javascript 比较
    SOAP vs REST
  • 原文地址:https://www.cnblogs.com/zdz8207/p/sphinx-php-result.html
Copyright © 2011-2022 走看看