且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-10 11:21:58

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

You have to put the _geo_distance sort in the first position instead of the third one. As it stands, the _geo_distance sort will only sort the docs in ascending distance order for those documents that have the same value for upgrade_sort and has_siegel. Try this instead:

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