如何在太阳黑子中使用Solr空间搜索引用距离?

时间:2012-10-16 12:34:42

标签: ruby-on-rails solr sunspot websolr

我使用标准语法在Rails上通过Sunspot使用Apache Solr:

class Post < ActiveRecord::Base
  searchable do
    # ...
    latlon(:location) { Sunspot::Util::Coordinates.new(lat, lon) }
  end
end

搜索:

Post.search do
  order_by_geodist(:location, 32, -68)
end

如何从点击中的搜索位置获取距离?我已经尝试了一百万种不同的语法选项来返回点击中的geodist,但似乎没有任何效果。

1 个答案:

答案 0 :(得分:2)

在Sunspot 2.0.0.pre.120925中,您必须在http://wiki.apache.org/solr/SpatialSearch#Returning_the_distance处加入hack。这会将距离放入分数,这看起来像是:

results = Post.search do
  fulltext "{!func}geodist(location_ll, 32, -68)"
  order_by(:score, :asc)
end

post = results.hits.first.result
distance = results.hits.first.score