在弹性搜索中计算地理距离 [英] compute geo distance in elasticsearch
问题描述
我正在使用 geo_distance
过滤器,我的查询中包含,并且可以正常工作:
i am using a geo_distance
filter with tire in my query and it works fine:
search.filter :geo_distance, :distance => "#{request.distance}km", :location => "#{request.lat},#{request.lng}"
我预计结果将以某种方式包括计算到我用于过滤器的地理位置的距离。
i expected that the result would somehow include the computed distance to the geo-location that i used for my filter.
有没有办法告诉弹性搜索将其包含在响应中,以便我每个结果都要用红宝石计算出来?
is there a way to tell elasticsearch to include that in the response, so that i don't have to calculate it in ruby for every result?
==更新==
我找到答案在Google群组中:
i found the answer in a google group:
search.sort do
by "_geo_distance", "location" => "#{request.lat},#{request.lng}", "unit" => "km" if request.with_location?
end
按 _geo_distance排序
将产生原始结果中的距离。
sorting by the _geo_distance
will yield the distance in the raw results.
推荐答案
排序 _geo_distance
将返回结果中的距离:
http:// www.elasticsearch.org/guide/reference/api/search/sort.html
Sorting by _geo_distance
will return the distance in the results:
http://www.elasticsearch.org/guide/reference/api/search/sort.html
这篇关于在弹性搜索中计算地理距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!