compute geo distance in elasticsearch compute geo distance in elasticsearch elasticsearch elasticsearch

compute geo distance in elasticsearch


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 :distanceenddef 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.resultsend

"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