从elastic search的结果中获取每个坐标的距离

yrdbyhpb  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(649)

我需要过滤最接近给定坐标的记录。从下面的查询中删除脚本\字段将起作用(给出结果)。
需要得到每个匹配结果的距离。

GET story/_search
{
  "_source": [
    "title.english", "location"
  ],
  "query": {
    "bool": {
      "filter": [
        {
          "geo_distance": {
            "distance": "1000km",
            "location": {
              "lat": 57.3079700,
              "lon": 123.4977090
            }
          }
        }
      ]
    }
  },
  "script_fields": {
    "distance": {
      "script": "doc['location'].distanceInKm(57.3079700, 123.4977090)"
    }
  }
}

下面是错误

"failures" : [
      {
        "shard" : 1,
        "index" : "story",
        "node" : "asdf-asdf",
        "reason" : {
          "type" : "script_exception",
          "reason" : "runtime error",
          "script_stack" : [
            "doc['location'].distanceInKm(57.3079700, 123.4977090)",
            "               ^---- HERE"
          ],
          "script" : "doc['location'].distanceInKm(57.3079700, 123.4977090)",
          "lang" : "painless",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "dynamic method [org.elasticsearch.index.fielddata.ScriptDocValues.GeoPoints, distanceInKm/2] not found"
          }
        }
      }
    ]
  },
clj7thdc

clj7thdc1#

正如@sharath所指出的, distanceInKm 已弃用。现在你可以用 arcDistance 再除以1000将值转换成km。

GET my-index-000001/_search
{
  ...
  "script_fields": {
    "distance": {
      "script": "doc['location'].arcDistance(57.3079700, 123.4977090) / 1000"
    }
  }
}

以下是当前支持的geo方法列表 arcDstance 来源。

相关问题