gpt4 book ai didi

sorting - Elasticsearch地理距离排序不完全/顺序错误

转载 作者:行者123 更新时间:2023-12-02 23:33:33 25 4
gpt4 key购买 nike

我正在使用Elasticsearch为具有数千个位置的Web项目获得更快的地理距离搜索功能。结果应按距离排序。我的搜索查询有效,但是结果的顺序不正确。结果应按距离升序排列,但结果首先是“9 km”,然后是“100 km”,然后是“90 km”。

我的搜索查询JSON是以下内容(使用php创建):

{
"index": "profiles",
"type": "profile",
"size": 30,
"from": 0,
"body": {
"query": {
"bool": {
"filter": {
"geo_distance": {
"distance": "100km",
"location": {
"lat": 49.449919468911,
"lon": 11.073560787681
}
}
}
}
},
"script_fields": {
"distance": {
"lang": "groovy",
"params": {
"lat": 49.449919468911,
"lon": 11.073560787681
},
"script": "doc['location'].distanceInKm(lat,lon)"
}
},
"sort": [
{
"upgrade_sort": {
"order": "desc"
}
},
{
"has_siegel": {
"order": "desc"
}
},
{
"_geo_distance": {
"location": {
"lat": 49.449919468911,
"lon": 11.073560787681
},
"order": "asc",
"unit": "km"
}
}
]
},
"fields": [
"_source",
"distance"
]
}

不幸的是,我没有错,所以我希望有人能帮助我。谢谢!

最佳答案

您必须将_geo_distance排序放在第一个位置,而不是第三个位置。就目前的情况而言,_geo_distance排序只会针对upgrade_sorthas_siegel具有相同值的那些文档按升序对文档进行排序。尝试以下方法:

{
"size": 30,
"from": 0,
"body": {
"query": {
"bool": {
"filter": {
"geo_distance": {
"distance": "100km",
"location": {
"lat": 49.449919468911,
"lon": 11.073560787681
}
}
}
}
},
"script_fields": {
"distance": {
"lang": "groovy",
"params": {
"lat": 49.449919468911,
"lon": 11.073560787681
},
"script": "doc['location'].distanceInKm(lat,lon)"
}
},
"sort": [
{
"_geo_distance": {
"location": {
"lat": 49.449919468911,
"lon": 11.073560787681
},
"order": "asc",
"unit": "km"
}
},
{
"upgrade_sort": {
"order": "desc"
}
},
{
"has_siegel": {
"order": "desc"
}
}
]
},
"fields": [
"_source",
"distance"
]
}

关于sorting - Elasticsearch地理距离排序不完全/顺序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33985792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com