【问题标题】:compute geo distance in elasticsearch在弹性搜索中计算地理距离
【发布时间】:2012-03-06 14:21:39
【问题描述】:

我在查询中使用geo_distance filtertire,它工作正常:

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

我希望结果会以某种方式包含计算出的到我用于过滤器的地理位置的距离。

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

== 更新 ==

found the answer 在一个 google 群组中:

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

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

【问题讨论】:

  • 感谢您按_geo_distance排序!
  • @karmi 对于如何将实际地理距离而不是 sort 字段放入结果中,您有一个好的解决方案吗?
  • @phoet 完全不确定,必须研究一下...
  • 请记住,sortfilter 是两个不同的东西。排序将为您提供按到中心点的距离组织的结果(如果您这样做的话),而过滤器可以为您提供一个边界框,其中所有结果都在您的中心点 X 英里范围内,然后您可以排序结果。

标签: ruby geolocation elasticsearch


【解决方案1】:

_geo_distance 排序将返回结果中的距离: http://www.elasticsearch.org/guide/reference/api/search/sort.html

【讨论】:

    【解决方案2】:

    对于那些可能想知道的人,距离在弹性搜索结果的排序属性中返回。为了访问它并将其显示在视图中,请使用 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。到目前为止,要使其正常工作,您必须在搜索时将 load 设置为 true:

    :load => true
    

    【讨论】:

    • 有人知道如何在 ElasticSearch Nest 中做到这一点吗?
    猜你喜欢
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 2011-08-26
    • 2012-07-18
    相关资源
    最近更新 更多