douanrang4728 2016-07-08 09:39
浏览 53

PHP的json_decode使用elasticsearch查询结果返回NULL

I've seen similar question here, but no one with an aggregation which might have something to do with the problem at hand.

This is the code I'm trying to run:

require '../vendor/autoload.php';

$client = Elasticsearch\ClientBuilder::create()->build();

$params = [
    'index' => 'search_log',
    'type' => 'search_log',
    'body' => [
        'size' => 0,
        'aggs' => [
            'popular_terms' => [
                'terms' => [
                    'field' => 'filteredSearch'
                ]
            ]
        ]
    ]
];

// Execute query
$response = $client->search($params);

// Decode response
$myData = json_decode($response);
var_dump($myData);

The output I get:

NULL

I have the problem narrowed down to the decoding part. If I execute a print_r of the $response, this is what I get (actually all in one line, I've pretty-printed it manually so it's easier to read):

Array (
    [took] => 245
    [timed_out] =>
    [_shards] => Array (
        [total] => 5
        [successful] => 5
        [failed] => 0
    )
    [hits] => Array (
        [total] => 5124004
        [max_score] => 0
        [hits] => Array ( )
    )
    [aggregations] => Array (
        [popular_terms] => Array (
            [doc_count_error_upper_bound] => 37750
            [sum_other_doc_count] => 11388032
            [buckets] => Array (
                [0] => Array (
                    [key] => term1
                    [doc_count] => 385107
                )
                [1] => Array (
                    [key] => term2
                    [doc_count] => 169381
                )
                [2] => Array (
                    [key] => term3
                    [doc_count] => 155258
                )
                [3] => Array (
                    [key] => term4
                    [doc_count] => 150382
                )
                [4] => Array (
                    [key] => term5
                    [doc_count] => 124759
                )
                [5] => Array (
                    [key] => term6
                    [doc_count] => 102589
                )
                [6] => Array (
                    [key] => term7
                    [doc_count] => 98791
                )
                [7] => Array (
                    [key] => term8
                    [doc_count] => 98772
                )
                [8] => Array (
                    [key] => term9
                    [doc_count] => 98091
                )
                [9] => Array (
                    [key] => term10
                    [doc_count] => 94559
                )
            )
        )
    )
)

My desired result would be to print just term1, term2, etc, without the rest of stuff that comes with Elasticsearch responses.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看