elasticsearch_dsl 按地理位置距离搜索并获得结果和距离

elasticsearch_dsl search by geo location distance and get results and distance

我有一个索引,用于保存商店的位置数据。

当使用 python 的 elasticsearch_dsl 时,我想查找距离给定位置最近的商店,按距离排序。由于ES已经计算了距离,是否可以将其作为搜索结果的一部分?

是否可以在单个查询中使用elasticsearch_dsl来实现?还是我必须对位置经纬度进行 post 处理并手动计算距离?

谢谢。

您可以 sort by distance 并在结果命中中计算出距离。

例如:

s = Search().sort(
    {
        "_geo_distance" : {
            "location" : {"lat": 40, "lon": -70},
            "order" : "asc",
            "unit" : "km",
            "distance_type" : "arc"
        }
    }
)