hel*_*llo 2 ruby ruby-on-rails sunspot ruby-on-rails-3 ruby-on-rails-3.2
如何搜索关联和太阳黑子?
class StaticController < ApplicationController
def search
@search = Sunspot.search Business, Service do
fulltext params[:q]
paginate :per_page => 10
order_by_geodist(:location, *Geocoder.coordinates(params[:loc]))
end
@biz = @search.results
end
Run Code Online (Sandbox Code Playgroud)
class Business < ActiveRecord::Base
attr_accessible :name
has_many :services, :through => :professionals
searchable do
text :name #name in business column
# how to do I get the services?
end
end
Run Code Online (Sandbox Code Playgroud)
class Service < ActiveRecord::Base
attr_accessible :service
belongs_to :professional
end
Run Code Online (Sandbox Code Playgroud)
class Professional < ActiveRecord::Base
belongs_to :business
has_many :services, as: :servicable
end
Run Code Online (Sandbox Code Playgroud)
在视图中,我有这个(很多循环)
<%= @biz.each do |b| %>
<%= b.name %>
<!-- looping through professionals model -->
<% b.professionals.each do |prof| %>
<!-- looping through services model -->
<% prof.services.each do |s| %>
<%= s.service %>
<% end %>
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
如果我搜索商业模型中的名称,这是有效的,但如果我在搜索Service模型中的术语,该怎么办?它无法正确显示,因为我的观点仅来自业务方面.如何搜索Service模型,如何创建商业名称?
谢谢
您需要为调用模型中的关联模型创建其他索引才能实现此目的.例如:
class Business < ActiveRecord::Base
attr_accessible :name
has_many :services, :through => :professionals
searchable do
text :name #name in business column
text :services do # this one for full text search
services.map(&:service).compact.join(" ")
end
string :services , :multiple => true do #this one for exact searches
services.map(&:service).compact
end
end
end
Run Code Online (Sandbox Code Playgroud)
之后,您可以执行以下查询:
Bussines.search do
with(:services, "some_service")
end.execute.results
Run Code Online (Sandbox Code Playgroud)
现在,您不再需要连接mysql表来获取数据.您可以从solr中获取数据.这是solr的最大优势之一.
我希望这说清楚.如果您需要更多详细信息,请随意删除评论.
| 归档时间: |
|
| 查看次数: |
1314 次 |
| 最近记录: |