建立索引
PUT /my_locations
{
"mappings": {
"properties": {
"pin": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
}
}
添加記錄
PUT my_locations/_doc/1
{
"pin" : {
"location" : {
"lat" : 40.12,
"lon" : -71.34
}
}
}
PUT my_locations/_doc/2
{
"pin" : {
"location" : {
"lat" : 40.12,
"lon" : -72.34
}
}
}
根據距離排序查詢(按距離近遠排序)
GET /my_locations/_search
{
"sort" : [
{
"_geo_distance" : {
"pin.location" : [-70, 40],
"order" : "asc",
"unit" : "km",
"mode" : "min",
"distance_type" : "arc",
"ignore_unmapped": true
}
}
],
"query": {
"bool": {
"must": {
"match_all": {}
}
}
}
}
結果
{
"took": 16,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "my_locations",
"_type": "_doc",
"_id": "2",
"_score": null,
"_source": {
"pin": {
"location": {
"lat": 40.12,
"lon": -71.34
}
}
},
"sort": [
114.8181793764576
]
},
{
"_index": "my_locations",
"_type": "_doc",
"_id": "1",
"_score": null,
"_source": {
"pin": {
"location": {
"lat": 40.12,
"lon": -72.34
}
}
},
"sort": [
199.58751303963078
]
}
]
}
}