Search code examples
ruby-on-railsruby-on-rails-4sunspotsunspot-railssunspot-solr

Rails sunspot results disappear after adding grouping


So my search worked fine, before I added grouping option to filter out duplicate results, even though i did everything as sunspot api instructed. Now I keep getting blank search page, even if I search for existing records.

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

And here is what I get in SOLR request in console after submitting search

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"]} ]

------------------------------------------------------------------------------------------------------------------------------------EDIT

I found out that "group :diy_id_str" line in controller causes this problem, when I remove it everything works fine, but as soon as I add it back, i don't get any results. But in sunspots console it shows that I'm getting hits, as seen here

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

Solution

  • For over a week of researching I finally figured it out!

    Had to add these lines in

    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  
    

    And go one more "results" deeper in my view, for some reason I could not make it in controller.

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