Py学习  »  Elasticsearch

如何使用Elasticsearch地理查询获取到给定地理点的最近位置(地理点)

Yasith Lokuge • 4 年前 • 2073 次点击  

我在Elasticsearch中索引了一组位置(地理点)。有人可以建议一种方法,使用elasticsearch geo queries获得距离给定地理点最近的地理点。我已经试过使用 Geo Distance 查询,但它要求我提供距离,以指定以指定位置为中心的圆的半径。我正在寻找一种替代解决方案,它可以返回最近的位置,而不管距离有多远。感谢您的建议。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/128132
文章 [ 1 ]  |  最新文章 4 年前
Val
Reply   •   1 楼
Val    4 年前

最好的方法是利用 distance sorting 并检索最近的地理位置:

GET my-index/_search
{
  "size": 1,                                <--- return top 1
  "sort" : [
    {
      "_geo_distance" : {
          "location-field" : [-70, 40],     <--- center location
          "order" : "asc",                  <--- sorted by closest distance
          "unit" : "km",
          "mode" : "min",
          "distance_type" : "arc",
          "ignore_unmapped": true
      }
    }
  ],
  "query" : {
    "match_all" : {}
  }
}