地理坐标获取
PUT /address
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"location": {
"type": "geo_point"
}
}
}
}
注:如果geo_point为字符串或者对象时,先纬度,后经度,如果geo_point为数组时,先经度,后纬度
POST /address/_doc/1
{
"name": "广州公园前",
"location": "23.131479, 113.270719"
}
POST /address/_doc/2
{
"name": "广百百货",
"location": "23.129881, 113.274646"
}
PUT /address/_doc/3
{
"name": "广州市妇幼医院",
"location": "23.129785, 113.260777"
}
POST /address/_doc/4
{
"name": "广州市银河烈士陵园",
"location": "23.168615, 113.343607"
}
_geo_distance:用于计算两点间的距离
unit:指定单位为km,默认为m
GET /restaurant/_search
{
"query": {
"bool": {
"filter": {
"geo_distance": {
"distance": "1km",
"location": {
"lat": 23.131304,
"lon": 113.262402
}
}
}
}
},
"sort": [
{
"_geo_distance": {
"unit": "km",
"location": {
"lat": 23.131304,
"lon": 113.262402
},
"order": "asc"
}
}
]
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "restaurant",
"_type" : "_doc",
"_id" : "3",
"_score" : null,
"_source" : {
"name" : "广州市妇幼医院",
"location" : "23.129785, 113.260777"
},
"sort" : [
0.23694189365651083
]
},
{
"_index" : "restaurant",
"_type" : "_doc",
"_id" : "1",
"_score" : null,
"_source" : {
"name" : "公园前",
"location" : "23.131479, 113.270719"
},
"sort" : [
0.8506760921207346
]
}
]
}
}