首页 > 解决方案 > ElasticSearch中的地理距离排序?

问题描述

参考ES 文档,如果 lat 和 lon 都在同一个 geotype 字段中,我们可以根据地理位置进行排序。

{
    "sort" : [
        {
            "_geo_distance" : {
                "pin.location" : [-70, 40],
                "order" : "asc",
                "unit" : "km",
                "mode" : "min",
                "distance_type" : "arc",
                "ignore_unmapped": true
            }
        }
    ],
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

就我而言,我在纬度和经度的单独浮动字段中都有位置。如果我无法更改架构,如何按地理距离排序?

标签: python-3.xelasticsearchsearch

解决方案


您将需要使用包含地理点字段而不是两个字段(纬度、经度)的新映射创建一个新索引。然后,您需要将数据索引到新创建的索引中。查看Reindex API - 将帮助您将数据移动到新索引中。


推荐阅读