Calculate Average of an Array's indexes in ElasticSearch

末鹿安然 提交于 2019-12-12 05:58:14

问题


I am trying to calculate average of the result set that is returning me locations from Elastic Search. Here is what i am trying.

'aggs' => [
            "avg_location" => [
                'avg' => [
                    'field' => 'location'
                ]
            ]
        ]

This returns error as location itself is an object/array that returns me [lat,long] of the point. I need to calculate average of lats and longs of all the points returned.

How can i do that? I tried quite a few things but none of them worked.

Here is the whole code.

$json = [
        'query' => [
            'bool' => [
                'must_not' => [
                    'terms' => ['rarity' => []],
                ],
                'must' => [
                    'range' => [
                        'disappearTime' => [
                            'gte' => 'now',
                            'lte' => 'now+1d'
                        ]
                    ]
                ],
                'filter' => [
                    [
                        'geo_bounding_box' => [
                            'location' => [
                                'top_left' => [
                                    'lat' => $northWestLat,
                                    'lon' => $northWestLng
                                ],
                                'bottom_right' => [
                                    'lat' => $southEastLat,
                                    'lon' => $southEastLng
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ],
        'aggs' => [
            "avg_location" => [
                'avg' => [
                    'field' => 'location'
                ]
            ]
        ]
    ];

Thanks

来源:https://stackoverflow.com/questions/43195348/calculate-average-of-an-arrays-indexes-in-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!