compute geo distance in elasticsearch

心已入冬 提交于 2019-12-03 08:08:55

Sorting by _geo_distance will return the distance in the results: http://www.elasticsearch.org/guide/reference/api/search/sort.html

For those that may be wondering, the distance is returned in the sort property of the elastic search results. In order to access it and display it in the view, use the each_with_hit method. For instance:

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" modifies the resulting object and renders the proper json. So far for this to work, you must set load to true when searching:

:load => true
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!