Elasticsearch地理距离排序不完全/顺序错误 [英] Elasticsearch geo distance sorting not exactly / wrong order

查看:176
本文介绍了Elasticsearch地理距离排序不完全/顺序错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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".

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

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!

推荐答案

您必须将_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"
  ]
}

这篇关于Elasticsearch地理距离排序不完全/顺序错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆