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

标签 sorting elasticsearch geolocation

我正在使用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/

相关文章:

search - 弹性(搜索): Get docs with max and min timestamp values

python - 具有日期和时间范围的综合数据

arrays - 使用 jq 根据对象中的键值从数组返回整个对象

c# - 对 DataTable 字符串列进行排序,但底部为空/空

sorting - 在 GoLang 中排序对

php,从mysql排序一个数组

elasticsearch - Elasticsearch Lucene查询语法来添加和减去N分钟与时间字段?

javascript - 如何从 Elasticsearch 查询/过滤器返回部分文档?

javascript - 获取地理位置 KCLError 域错误

javascript - 地理位置 API - 不同的结果