用于获取从任意点到 Geopoint 的距离的弹性搜索查询的单位

Units of an elasticsearch query to get distance from arbitrary point to Geopoint

我有一个 django 项目,它使用 elasticsearch 6.5.3 将商店中的产品索引为 GeoPoints。我正在尝试查询此索引并计算任意点之间的距离,比如用户的位置到每个这些结果。

我正在使用 elasticsearch_dsl,我的代码如下所示:

search_query = search_query.script_fields(distance={
'script':{
    'inline':"doc['location'].arcDistance(params.lat, params.lon)", 
    'params': {
        'lat':user_loc.lat, 
        'lon':user_loc.lon
        }
    }
})
for result in search_query.execute():
    print(result.distance)

这给我的值如下所示:

[123456.456879123]

但我不确定它的单位。 通过使用 https://www.nhc.noaa.gov/gccalc.shtml 中的在线距离计算器, 这给了我约 123 公里的距离, 值似乎以米为单位。

所以:

1.我在哪里可以找到有关其单位的明确答案?

请指出这些方法的相关文档。 我也很想知道是否有一种方法可以指定方法调用中结果的预期单位。

2。在 python?

中有更好的方法吗?

我不确定确切的 python API,但是 elasticsearch 有地理距离查询: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html

在:https://github.com/elastic/elasticsearch-dsl-py/issues/398 中有一个 python ES 用法的示例 API:

MyDocType.search().filter(
'geo_distance', distance='1000m', location={"lat": "40", "lon": "-74"}
)

'geo_distance' 查询是获取索引到 elasticsearch 的两个地理点之间距离的最简单方法。我认为您不需要使用脚本来实现它。

关于距离单位,如你所料,默认是米。从: https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#distance-units

Wherever distances need to be specified, such as the distance parameter in the Geo Distance Query), the default unit if none is specified is the meter.

单位是 arcDistance 方法返回的单位,在您的脚本中提供值。

The arc distance (in meters) of this geo point field from the provided lat/lon

无痛文档还有很多不足之处(6.5 中似乎没有关于此方法的文档)。上面的引用是从这里获得的:https://www.elastic.co/guide/en/elasticsearch/reference/2.3/modules-scripting.html

此外,他们在这里提到 arcDistance 计算米:https://www.elastic.co/guide/en/elasticsearch/reference/5.5/breaking_50_scripting.html