Pagination with Elasticsearch, Tire, and Kaminari

I'm having trouble paginating search results for jobs with Elasticsearch, Tire, and Kaminari.

I am looking for all the models in my application (news, pictures, books) as a general search on the site and, therefore, you need a block for searching tires, in my site controller for finer-grained control, for example, showing more than 10 entries by default :

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 

On 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 correct display and includes tires:

  # - - - - - - - - - - - - - - - - # 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 that all my records are indexed properly in Elasticsearch.

The problem I am facing is that I cannot get it to work, the last error is:

undefined method `current_page '

Any thoughts would be greatly appreciated. Thanks.

+4
source share
2 answers

Can you try this? When I used the per_page parameter, I had a similar problem. So, I moved on to the size options provided by Tire. I'm not sure what went wrong. But explicit setup and use 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 
+3
source

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

 @results = @results.all.to_hash paginate @results 
0
source

Source: https://habr.com/ru/post/1468759/


All Articles