elasticsearch - 从Elastic Search中的结果获取每个坐标的距离

标签 elasticsearch

我需要过滤最接近给定坐标的记录。从下面的查询中删除 script_fields 将起作用(给出结果)。
需要获取每个匹配结果的距离。

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

最佳答案

正如@Sharath指出的那样,不建议使用distanceInKm。这些天,您可以使用arcDistance并通过除以1000将值转换为km。

GET my-index-000001/_search
{
  ...
  "script_fields": {
    "distance": {
      "script": "doc['location'].arcDistance(57.3079700, 123.4977090) / 1000"
    }
  }
}
这是currently supported geo methods的列表,这是 arcDstance source

关于elasticsearch - 从Elastic Search中的结果获取每个坐标的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64737926/

相关文章:

elasticsearch - Fluentd 尾源不将日志移动到 ElasticSearch

elasticsearch - 将JSON文件中的大容量索引文档导入ElasticSearch

spring - Spring数据 Elasticsearch 映射

elasticsearch - 使用logstash的csv文件输入处理在2/3天后停止工作

elasticsearch - Elasticsearch最佳实践(地址)

elasticsearch - spring data elasticsearch中,聚合查询不能放在repository实现吗?

elasticsearch - _ttl更新不适用于doc选项

elasticsearch - 是否可以在ElasticSearch中为内部对象定义默认映射?

networking - 在Elasticsearch集群中 “IN THE SAME NETWORK”是什么意思?

node.js - 如何将Logstash集成到Node.js应用程序中?