class Office < ActiveRecord::Base
has_many :users
searchable do
text :name
location :coordinates do
Sunspot::Util::Coordinates.new(latitude, longitude)
end
end
end
class User < ActiveRecord::Base
belongs_to :office
searchable do
text :name, :default_boost => 2
text :description
end
end使用这种设置,我如何在给定的经度/纬度范围内使用SunSpot (on Solr) on Rails搜索用户?例如,我希望能够这样做:
@search = User.search() do
fulltext(params[:q])
with(:coordinates).near(@lat, @long, :precision => 5)
end下面的代码运行得很好:
@search = Office.search() do
fulltext(params[:q])
with(:coordinates).near(@lat, @long, :precision => 5)
end假设每个用户的经度/经度都位于Office类中,那么实现这一目标的最佳方法是什么?
发布于 2011-05-20 14:59:20
office关联应该在用户的可搜索块的范围内。
鉴于此,我将从以下内容开始(未经测试,不假思索,等等):
class User < ActiveRecord::Base
belongs_to :office
searchable do
text :name, :default_boost => 2
text :description
location :coordinates do
Sunspot::Util::Coordinates.new(office.latitude, office.longitude)
end
end
end在这样的块中获取相关对象的值实际上是使用Sunspot处理反规范化的一种非常常见的模式。
https://stackoverflow.com/questions/6068186
复制相似问题