添加分组后,Rails太阳黑子结果消失

时间:2016-03-08 20:45:52

标签: ruby-on-rails ruby-on-rails-4 sunspot sunspot-rails sunspot-solr

因此,在我添加分组选项以过滤掉重复结果之前,我的搜索工作正常,即使我按照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"]} ]

-------------------------------------------- -------------------------------------------------- --------------------------------------修改

我发现&#34; group:diy_id_str&#34;控制器中的行导致了这个问题,当我删除它时一切正常,但是一旦我将其添加回去,我就不会得到任何结果。但是在太阳黑子游戏机中,它显示我正在点击,如此处所示

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

1 个答案:

答案 0 :(得分:0)

经过一周多的研究,我终于明白了!

必须在

中添加这些行

<强> 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  

在我看来更深入一些“结果”,由于某些原因我无法在控制器中实现。

<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>