如何在ElasticSearch中求出两地理点之间的距离?

sr4lhrrt  于 2022-11-28  发布在  ElasticSearch
关注(0)|答案(1)|浏览(243)

ElasticSearch中的以下查询将给予位于指定距离内的结果。结果不包括实际距离。有什么方法可以在ElasticSearch中获得此信息吗?

"query": {
    "bool" : {
        "must" : {
            "match_all" : {}
        },
        "filter" : {
            "geo_distance" : {
                "distance" : "12km",
                "pin.location" : [-70, 40]
            }
        }
    }
}
e4eetjau

e4eetjau1#

使用与geo_distance筛选器中具有相同参考点的脚本字段。完整查询:

{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": "5000km",
          "pin.location": [
            -70,
            40
          ]
        }
      }
    }
  },
  "script_fields": {
    "distance_in_m": {
      "script": "doc['pin.location'].arcDistance(40, -70)"
    }
  }
}


geo-sort,您将获得排序响应中包含的距离信息。

相关问题