php解析Elasticsearch的返回值

本文介绍如何使用PHP解析Elasticsearch的搜索返回值。示例代码展示了具体的解析过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是通过Es搜索查找出来的值$data

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 495,
        "max_score": null,
        "hits": [
        {
            "_index": "post",
            "_type": "full",
            "_id": "869480",
            "_score": null,
            "_source": {
                "hit": 201,
                "create_time": "1476942098",
                "reply": 4,
                "content": "有续期是个好服务,为了无缝对接,需要提前取消也",
                "title": "人人 未经同意自动续期产品的流氓行为",
                "tid": "869480",
                "type": 1,
                "username": "breathxue",
                "lastpost": "1476953784",
                "uid": "540553"
            },
            "highlight": {
                "title": [
                "<em class="c_color">人人</em><em class="c_color"></em> 未经同意自动续期产品的流氓行为"
                ]
           },
            "sort": [
                "1476953784"
            ]
        }]
   }
 }

解析的代码:

public function EsData($data){
    $datas=json_decode($data);
    $total = $data->hits->total;
    $data=$datas->hits->hits;
    $res=array();
    foreach($data as $item){
        $res['index'] = $item->_index;
        $res['id'] = $item->_id;
        $res['content'] = $item->_source->content;
    }
    return $res;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值