4

I'm having issues getting pagination of search results to work with Elasticsearch, Tire, and Kaminari.

I am searching on all models in my application (news, paintings, books) as a general site search and therefore, need a block for the tire search, in my site controller for more fine grained control, such as showing more than the default of 10 entries:

class SiteController < ApplicationController
  def search
    # @results = Painting.search(params[:query])
    query = params[:query]
    @results = Tire.search ['news','paintings', 'books'], :page => (params[:page]||1), :per_page=> (params[:per_page] || 3) do
      query { string query }
      size 100
    end
  end
end

In my search results page, I have the following code:

- if @results
  - @results.results.each do |result|
    = result.title
#pagination
  = paginate @results

and in all my models, I have the proper mapping and includes from tire:

  # - - - - - - - - - - - - - - - - 
  # Elasticsearch
  # - - - - - - - - - - - - - - - - 
  include Tire::Model::Search
  include Tire::Model::Callbacks

  mapping do
    indexes :id, index: :not_analyzed
    indexes :title, boost: 100
    indexes :slug, boost: 100, as: 'slug'
    indexes :content
    indexes :image, as: 'image.thumb.url'
    indexes :tags, as: 'tags'
    indexes :gallery_name, as: 'gallery.name'
    indexes :created_at, :type => 'date'
  end

I ensured all my entries are indexed properly in Elasticsearch.

The issue I'm having is I can't get it to work, the latest error is:

undefined method `current_page'

Any thoughts would be greatly appreciated. Thank you.

anthony
  • 391
  • 1
  • 3
  • 15

2 Answers2

3

Can you try this??? When I used the per_page option, I had similar issue. So, I shifted to the from size options provided by Tire. I'm not sure what went wrong. But, explicitly setting and using from and size did the trick for me...

class SiteController < ApplicationController
  def search
    # @results = Painting.search(params[:query])
    options = { :page => (params[:page] || 1), :size => 100 }
    query = params[:query]
    @results = Tire.search ['news','paintings', 'books'], options do
      query { string query }
      from options[:size].to_i * (options[:page].to_i-1)
    end
  end
end
Vamsi Krishna
  • 3,742
  • 4
  • 20
  • 45
  • What this code snippet really helped me was where I should put from and size options. They need to be in side of the code block for the search, not as part of the method parameters. Thank you for this, Vamsi Krishna – Yosep Kim May 09 '14 at 10:48
  • Glad that this piece of code helped you... @YosepKim – Vamsi Krishna May 15 '14 at 08:15
0

Tire has method for Will-paginate gem included in its lib so I would prefer that rather than Kaminari gem. If in any case you still want to be with Kaminari, collect the results of tire search.

@results = @results.all.to_hash

paginate @results
usmanali
  • 2,028
  • 2
  • 27
  • 38