compute geo distance in elasticsearch

后端 未结 2 650
闹比i
闹比i 2021-02-09 07:49

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 =>         


        
相关标签:
2条回答
  • 2021-02-09 08:17

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

    0 讨论(0)
  • 2021-02-09 08:27

    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
    
    0 讨论(0)
提交回复
热议问题