Sunspot pagination with kaminari
Asked Answered
S

1

7

I recently decided to port my indexing engine from sphinx to solr. Having used kaminari with thinking_sphinx I decided to try making use of generic pagination in sunspot https://github.com/sunspot/sunspot/pull/64 / https://github.com/sunspot/sunspot/pull/67, in order to use avoid moving to will_paginate.

My search is handled as follows:

@search = Address.search do
  fulltext params[:search]
  with(:updated_at).greater_than(1.week.ago)
  order_by :updated_at, :desc
  paginate :page => params[:page], :per_page => 7
end

My view is unchanged from what I had when I was using thinking_sphinx:

<%= render :partial => 'address' %>
<%= paginate @addresses %>

My problem is that after the change I continually get the following error when trying to perform a search:

undefined method `current_page' for []:Array

I am using the latest version of sunspot, which to my knowledge should enable me to use kaminari:

Using sunspot (1.3.0.rc3) from git://github.com/sunspot/sunspot.git (at master) 
Using sunspot_rails (1.3.0.rc3) from git://github.com/sunspot/sunspot.git (at master) 

This worked perfectly with my old thinking_sphinx setup, so what am I doing wrong?

Sastruga answered 28/10, 2011 at 12:37 Comment(4)
Well I got tired of trying to make it work and switched to will_paginate, works great now.Sastruga
there is a sunspot kaminari gem which makes kaminari and sunspot play nicely together https://github.com/richardiux/sunspot_with_kaminari works absolutely fine for us.Wanda
I had seen that gem but overlooked at the time as there did not seem to be a lot of activity there. Perhaps I judged it somewhat harshly, I will take another look at it. Cheers for the recommendation.Sastruga
if you look at the code, you can see, that it really is just a few lines of code, very simple, and if breaks with a future kaminari or sunspot version, it should be dead easy to fork and fix.Wanda
R
13

This is how I have used and it works great

@search = Sunspot.search(Listing) do
      if params[:category].present?
        with :category_id, params[:category]
      end
      if params[:subcategory].present?
        with :subcategory_id, params[:subcategory]
      end
      if params[:q].present?
        keywords params[:q]  do 
          fields :title, :description
        end
      end
      paginate :page => params[:page], :per_page => SEARCH_RESULT_PER_PAGE
    end

And in views I have this

<%= paginate @search.hits %>
Rorke answered 17/1, 2012 at 14:9 Comment(1)
Nowadays it's paginate @search.results.Shorthand

© 2022 - 2024 — McMap. All rights reserved.