原文地址: https://www.elastic.co/guide/cn/elasticsearch/guide/current/geo-distance-agg.html, 版权归 www.elastic.co 所有
英文版地址: https://www.elastic.co/guide/en/elasticsearch/guide/current/geo-distance-agg.html
英文版地址: https://www.elastic.co/guide/en/elasticsearch/guide/current/geo-distance-agg.html
请注意:
本书基于 Elasticsearch 2.x 版本,有些内容可能已经过时。
本书基于 Elasticsearch 2.x 版本,有些内容可能已经过时。
地理距离聚合edit
geo_distance 聚合 对一些搜索非常有用,例如找到所有距离我 1km 以内的披萨店。搜索结果应该也的确被限制在用户指定 1km 范围内,但是我们可以添加在 2km 范围内找到的其他结果:
GET /attractions/restaurant/_search
{
"query": {
"bool": {
"must": {
"match": {
"name": "pizza"
}
},
"filter": {
"geo_bounding_box": {
"location": {
"top_left": {
"lat": 40.8,
"lon": -74.1
},
"bottom_right": {
"lat": 40.4,
"lon": -73.7
}
}
}
}
}
},
"aggs": {
"per_ring": {
"geo_distance": {
"field": "location",
"unit": "km",
"origin": {
"lat": 40.712,
"lon": -73.988
},
"ranges": [
{ "from": 0, "to": 1 },
{ "from": 1, "to": 2 }
]
}
}
},
"post_filter": {
"geo_distance": {
"distance": "1km",
"location": {
"lat": 40.712,
"lon": -73.988
}
}
}
}
|
主查询查找名称中含有 |
|
|
|
|
|
|
|
|
最后,后置过滤器 |
前面的请求 响应如下:
"hits": {
"total": 1,
"max_score": 0.15342641,
"hits": [
{
"_index": "attractions",
"_type": "restaurant",
"_id": "3",
"_score": 0.15342641,
"_source": {
"name": "Mini Munchies Pizza",
"location": [
-73.983,
40.719
]
}
}
]
},
"aggregations": {
"per_ring": {
"buckets": [
{
"key": "*-1.0",
"from": 0,
"to": 1,
"doc_count": 1
},
{
"key": "1.0-2.0",
"from": 1,
"to": 2,
"doc_count": 1
}
]
}
}
在这个例子中,我们计算了落在每个同心环内的饭店数量。当然,我们可以在 per_ring 聚合下 嵌套子聚合来计算每个环的平均价格、最受欢迎程度等。
>译者注: 本来只需要查询1km以内的数据, 但是在外面又套了一层1km~2km的数据, 所以就组成了同心环。