Elasticsearch地理距离排序不准确/顺序错误



我正在使用弹性搜索为一个拥有数千个位置的网络项目获得更快的地理距离搜索功能。结果应按距离排序。我的搜索查询有效,但结果的顺序不正确。结果应该按距离升序排列,但最初有一个距离为"9公里"、"100公里"one_answers"90公里"的结果。

我的搜索查询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排序放在第一个位置,而不是第三个位置。目前,对于upgrade_sorthas_siegel具有相同值的文档,_geo_distance排序将仅按升序对文档进行排序。试试这个:

{
  "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"
  ]
}

最新更新