Elasticsearch geo distance sorting not exactly / wrong order

≡放荡痞女 提交于 2019-12-13 18:29:17

问题


I am using elasticsearch to get a faster geo distance search functionality for a web project with thousands of locations. The results should be sorted by distance. My search query works, but the order of the results is not correct. The results should be sorted ascending by distance, but there is a result at first with a distance of "9 km", then "100 km" and then "90 km".

My search query JSON is the following (created with 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"
    ]
}

Unfortunately, I can find no fault, so I hope that someone can help me. Thanks!


回答1:


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


来源:https://stackoverflow.com/questions/33985792/elasticsearch-geo-distance-sorting-not-exactly-wrong-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!