Rails 添加分组后太阳黑子结果消失
Rails sunspot results disappear after adding grouping
所以我的搜索工作正常,在我添加分组选项以过滤掉重复结果之前,即使我按照 sunspot api 的指示做了所有事情。现在我总是得到空白的搜索页面,即使我搜索现有记录也是如此。
attached_vehicle.rb
class AttachedVehicle < ActiveRecord::Base
belongs_to :diy
searchable do
text :make
text :model
text :attached_vehicles_year do
(self.start_year..self.end_year).to_a
end
string(:diy_id_str) { |p| p.diy_id.to_s }
end
end
attached_vehicles_controller.rb
def select_search
@select_search = AttachedVehicle.search do
keywords(params[:attached_vehicles_model])
keywords(params[:attached_vehicles_year])
keywords(Make.find(params[:attached_vehicles_make]).make_name)
group :diy_id_str
end
end
这是提交搜索后我在控制台的 SOLR 请求中得到的内容
SOLR Request (13.0ms) [ path=select parameters={fq: ["type:AttachedVehicle"], q: "(_query_:"{!edismax qf='make_text model_text attached_vehicles_year_text'}A6" AND _query_:"{!edismax qf='make_text model_text attached_vehicles_year_text'}2005" AND _query_:"{!edismax qf='make_text model_text attached_vehicles_year_text'}AUDI")", fl: "* score", start: 0, rows: 30, group: "true", group.ngroups: "true", group.field: ["diy_id_str_s"]} ]
---------------------------------------- ---------------------------------------------- --------------------------------------编辑
我发现控制器中的 "group :diy_id_str" 行导致了这个问题,当我删除它时一切正常,但是一旦我将它添加回来,我就没有得到任何结果。但在 sunsspots 控制台中,它显示我正在点击,如此处所示
2086072 INFO (qtp20557198-14) [ x:development] o.a.s.c.S.Request [development] webapp=/solr path=/select params={q=AUDI&defType=edismax&qf=make_text+model_text+attached_vehicles_year_text&fl=*+score&start=0&fq=type:AttachedVehicle&rows=30&group.ngroups=true&wt=ruby&group.field=diy_id_str_s&group=true} hits=2 status=0 QTime=3
经过一个多星期的研究,我终于弄明白了!
必须在
中添加这些行
attached_vehicles_controller.rb
def select_search
@select_search = Sunspot.search(AttachedVehicle) do
group :diy_id_s
keywords(params[:attached_vehicles_model])
keywords(params[:attached_vehicles_year])
keywords(Make.find(params[:attached_vehicles_make]).make_name)
end
@values = @select_search.group(:diy_id_s).groups.each do |group|
group.results
end
end
再深入 "results" 我的观点,出于某种原因我无法在控制器中做到这一点。
<div class="results">
<% @values.each do |v| %>
<% v.results.each do |attached_vehicle| %>
<% @diy = Diy.find(attached_vehicle.diy_id) %>
<div class="result">
<h2><%= link_to @diy.title, @diy %></h2>
<p><%= @diy.attached_vehicles.map { |av| "#{av.make} #{av.model} #{av.start_year}-#{av.end_year}"}.join(", ") %></p>
<p><%= truncate(@diy.summary, :length => 100) %></p>
</div>
<% end %>
<% end %>
</div>
所以我的搜索工作正常,在我添加分组选项以过滤掉重复结果之前,即使我按照 sunspot api 的指示做了所有事情。现在我总是得到空白的搜索页面,即使我搜索现有记录也是如此。
attached_vehicle.rb
class AttachedVehicle < ActiveRecord::Base
belongs_to :diy
searchable do
text :make
text :model
text :attached_vehicles_year do
(self.start_year..self.end_year).to_a
end
string(:diy_id_str) { |p| p.diy_id.to_s }
end
end
attached_vehicles_controller.rb
def select_search
@select_search = AttachedVehicle.search do
keywords(params[:attached_vehicles_model])
keywords(params[:attached_vehicles_year])
keywords(Make.find(params[:attached_vehicles_make]).make_name)
group :diy_id_str
end
end
这是提交搜索后我在控制台的 SOLR 请求中得到的内容
SOLR Request (13.0ms) [ path=select parameters={fq: ["type:AttachedVehicle"], q: "(_query_:"{!edismax qf='make_text model_text attached_vehicles_year_text'}A6" AND _query_:"{!edismax qf='make_text model_text attached_vehicles_year_text'}2005" AND _query_:"{!edismax qf='make_text model_text attached_vehicles_year_text'}AUDI")", fl: "* score", start: 0, rows: 30, group: "true", group.ngroups: "true", group.field: ["diy_id_str_s"]} ]
---------------------------------------- ---------------------------------------------- --------------------------------------编辑
我发现控制器中的 "group :diy_id_str" 行导致了这个问题,当我删除它时一切正常,但是一旦我将它添加回来,我就没有得到任何结果。但在 sunsspots 控制台中,它显示我正在点击,如此处所示
2086072 INFO (qtp20557198-14) [ x:development] o.a.s.c.S.Request [development] webapp=/solr path=/select params={q=AUDI&defType=edismax&qf=make_text+model_text+attached_vehicles_year_text&fl=*+score&start=0&fq=type:AttachedVehicle&rows=30&group.ngroups=true&wt=ruby&group.field=diy_id_str_s&group=true} hits=2 status=0 QTime=3
经过一个多星期的研究,我终于弄明白了!
必须在
中添加这些行attached_vehicles_controller.rb
def select_search
@select_search = Sunspot.search(AttachedVehicle) do
group :diy_id_s
keywords(params[:attached_vehicles_model])
keywords(params[:attached_vehicles_year])
keywords(Make.find(params[:attached_vehicles_make]).make_name)
end
@values = @select_search.group(:diy_id_s).groups.each do |group|
group.results
end
end
再深入 "results" 我的观点,出于某种原因我无法在控制器中做到这一点。
<div class="results">
<% @values.each do |v| %>
<% v.results.each do |attached_vehicle| %>
<% @diy = Diy.find(attached_vehicle.diy_id) %>
<div class="result">
<h2><%= link_to @diy.title, @diy %></h2>
<p><%= @diy.attached_vehicles.map { |av| "#{av.make} #{av.model} #{av.start_year}-#{av.end_year}"}.join(", ") %></p>
<p><%= truncate(@diy.summary, :length => 100) %></p>
</div>
<% end %>
<% end %>
</div>