计算elasticsearch中的地理距离

我在我的查询中使用带轮胎的geo_distancefilter ,它工作正常:

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

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

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

==更新==

我在谷歌小组中找到了答案 :

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

_geo_distance排序将产生原始结果中的距离。

_geo_distance排序将返回结果中的距离: http : _geo_distance

对于那些可能想知道的人,在弹性搜索结果的sort属性中返回距离。 要访问它并在视图中显示它,请使用each_with_hit方法。 例如:

 class Venue attr_accessor :distance end def index @search = Venue.search(params) @search.each_with_hit do |result, hit| result.distance = hit['sort'][0] # distance from the ES results end render json: @search.results end 

“each_with_hit”修改生成的对象并呈现正确的json。 到目前为止,要实现此function,您必须在搜索时将load设置为true:

 :load => true