我在Elasticsearch中有此查询。它可以正常工作,但不能按到搜索点的距离对结果进行排序。
GET /place_names/_search
{
"query": {
"bool" : {
"must" : {
"match_all" : {}
},
"filter" : {
"geo_distance" : {
"distance" : "10km",
"location" : {
"lat" : 64,
"lon" : -22
}
}
}
}
},
"script_fields" : {
"id": {"script": "doc['id']"},
"name": {"script": "doc['name']"},
"type": {"script": "doc['type']"},
"location": {"script": "doc['location']"},
"dist_to_point" : {
"script" : "doc['location'].arcDistance(64, -22)"
}
}
}
我想按“ dist_to_point”对结果进行排序。可能吗?
答案 0 :(得分:1)
您可以使用内置的_geo_distance
排序:
GET /place_names/_search
{
"sort": [
{
"_geo_distance": {
"location": {
"lat": 64,
"lon": -22
},
"distance_type" : "arc",
"order": "asc"
}
}
]
}
或应用_script
排序-两者等效:
GET places/_search
{
"sort": [
{
"_geo_distance": {
"location": {
"lat": 64,
"lon": -22
},
"distance_type": "arc",
"order": "asc"
}
},
{
"_script": {
"script": "doc['location'].arcDistance(64, -22)",
"type": "number",
"order": "asc"
}
}
]
}
但是您不能通过脚本字段进行排序,因为它们仅存在于与排序API分开的搜索API中(=他们彼此不了解)。