1. 程式人生 > 其它 >DAY 122 ES--位置座標

DAY 122 ES--位置座標

一 建立mapping

PUT test
{
"mappings": {
"test":{
"properties": {
"location":{
"type": "geo_point"
}
}
}
}
}

二 匯入資料

POST test/test
{
"location":{
"lat":12,
"lon":24
}
}

三 查詢

3.1根據給定兩個點組成的矩形,查詢矩形內的點

GET test/test/_search
{
"query": {
"geo_bounding_box": {
"location": {
"top_left": {
"lat": 28,
"lon": 10
},
"bottom_right": {
"lat": 10,
"lon": 30
}
}
}
}
}

3.2根據給定的多個點組成的多邊形,查詢範圍內的點

GET test/test/_search
{
"query": {
"geo_polygon": {
"location": {
"points": [
{
"lat": 11,
"lon": 25
},
{
"lat": 13,
"lon": 25
},
{
"lat": 13,
"lon": 23
},
{
"lat": 11,
"lon": 23
}
]
}
}
}
}

3.3查詢給定1000KM距離範圍內的點

GET test/test/_search
{
"query": {
"geo_distance": {
"distance": "1000km",
"location": {
"lat": 12,
"lon": 23
}
}
}
}

3.4查詢距離範圍區間內的點的數量

GET test/test/_search
{
"size": 0,
"aggs": {
"myaggs": {
"geo_distance": {
"field": "location",
"origin": {
"lat": 52.376,
"lon": 4.894
},
"unit": "km",
"ranges": [
{
"from": 50,
"to": 30000
}
]
}
}
}
}