小编典典

在Elasticsearch中计算地理位置距离

elasticsearch

我在查询中使用带轮胎geo_distance过滤器,效果很好:

search.filter :geo_distance, :distance => "#{request.distance}km", :location => "#{request.lat},#{request.lng}"

我希望结果将以某种方式包括我用于过滤器的地理位置的计算距离。

有没有办法告诉Elasticsearch在响应中包括它,这样我就不必为每个结果都用ruby计算它?

==更新==

我在一个Google网上论坛中找到了答案

search.sort do
  by "_geo_distance", "location" => "#{request.lat},#{request.lng}", "unit" => "km" if request.with_location?
end

按排序_geo_distance将得出原始结果中的距离。


阅读 1545

收藏
2020-06-22

共1个答案

小编典典

排序依据_geo_distance将返回结果中的距离:http
:
//www.elasticsearch.org/guide/reference/api/search/sort.html

2020-06-22