81_elasticsearch高手进阶_统计当前位置每个距离范
2020-03-04 本文已影响0人
小山居
81_elasticsearch高手进阶_统计当前位置每个距离范围内有多少家酒店
最后一个知识点,基于地理位置进行聚合分析
我的需求就是,统计一下,举例我当前坐标的几个范围内的酒店的数量,比如说举例我0100m有几个酒店,100m300m有几个酒店,300m以上有几个酒店
一般来说,做酒店app,一般来说,我们是不是会有一个地图,用户可以在地图上直接查看和搜索酒店,此时就可以显示出来举例你当前的位置,几个举例范围内,有多少家酒店,让用户知道,心里清楚,用户体验就比较好
GET /hotel_app/hotels/_search
{
"size": 0,
"aggs": {
"agg_by_distance_range": {
"geo_distance": {
"field": "pin.location",
"origin": {
"lat": 40,
"lon": -70
},
"unit": "mi",
"ranges": [
{
"to": 100
},
{
"from": 100,
"to": 300
},
{
"from": 300
}
]
}
}
}
}
搜素结果:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0,
"hits": []
},
"aggregations": {
"agg_by_distance_range": {
"buckets": [
{
"key": "-100.0",
"from": 0,
"to": 100,
"doc_count": 1
},
{
"key": "100.0-300.0",
"from": 100,
"to": 300,
"doc_count": 0
},
{
"key": "300.0-",
"from": 300,
"doc_count": 0
}
]
}
}
}
m (metres) but it can also accept: m (miles), km (kilometers)
sloppy_arc (the default), arc (most accurate) and plane (fastest)