通过协会进行太阳黑子地理搜索
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
通过这种设置,我如何使用 Rails 上的 SunSpot(在 Solr 上)在给定的纬度/经度内搜索用户?例如,我希望能够做到这一点:
@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 类中,完成此操作的最佳方法是什么?
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
With this kind of a setup, how can I search using SunSpot (on Solr) on Rails for a user within a given lat/long? For example, I want to be able to do this:
@search = User.search() do
fulltext(params[:q])
with(:coordinates).near(@lat, @long, :precision => 5)
end
The following works just fine:
@search = Office.search() do
fulltext(params[:q])
with(:coordinates).near(@lat, @long, :precision => 5)
end
What is the best way to accomplish this given that the lat/long for each User really lives in the Office class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
office
关联应位于用户的可搜索块内。鉴于此,这就是我要开始的内容(未经测试,超出我的想象,等等):
在像这样的块中获取关联对象的值实际上是使用太阳黑子处理非规范化的一种非常常见的模式。
The
office
association should be in scope inside your User's searchable block.Given that, here's what I would start with (untested, off the top of my head, etc):
Fetching values for associated objects in a block like this is actually a pretty common pattern for handling denormalization with Sunspot.