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

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

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

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

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

Model类方法:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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类方法

1
2
3
4
5
6
7
8
$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 的结果贴出来(去掉了部分结果内容)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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 ) ) )

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

 

posted @   大自然的流风  阅读(1464)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示