Elasticsearch-rails: Using Kaminari with Elasticsearch::Persistence::Model

Created on 2 Mar 2015  路  12Comments  路  Source: elastic/elasticsearch-rails

Hi, I tried to use Kaminari with Elasticsearch::Persistence::Model, but it does not work yet it seems. Is there a way to paginate a Elasticsearch::Persistence::Model? Or is it somewhere on the roadmap?

Thank you!

feature

Most helpful comment

I was able to paginate the elasticsearch results with the below example.

# User Controller
def index
  @records = User.match_all_query(params[:page])
  total_count = @records.response.hits.total
  @records = Kaminari.paginate_array(@records, total_count: total_count)
                                    .page(params[:page])
                                    .per(100)
end


# User Model
def match_all_query(page = 1, per_page = 100)
  page ||= 1
  search({
    query: {
      match_all: {}
     },
     size: per_page,
     from: (page.to_i - 1) * per_page
   })
end


We can then use @records in the view page as <%= paginate @records %>

All 12 comments

We have a similar issue with will_paginate...
undefined method `paginate' for #

There is no automatic support for Kaminari/WillPaginate yet, but it's of course planned.

+1

+1

+1

I was able to paginate the elasticsearch results with the below example.

# User Controller
def index
  @records = User.match_all_query(params[:page])
  total_count = @records.response.hits.total
  @records = Kaminari.paginate_array(@records, total_count: total_count)
                                    .page(params[:page])
                                    .per(100)
end


# User Model
def match_all_query(page = 1, per_page = 100)
  page ||= 1
  search({
    query: {
      match_all: {}
     },
     size: per_page,
     from: (page.to_i - 1) * per_page
   })
end


We can then use @records in the view page as <%= paginate @records %>

Thanks for the tip!

+1

+1

I'd be interested to help with this feature. I'd appreciate any advice on how to implement it without making breaking changes. As far as I understood when calling method search on a repository or a model, the request is done right away and isn't waiting until method results is called. We'd have to change that behaviour right ?

@karmi if you could put me in the right path, that would be great

thanks and sorry for my poor english

+1 Hi, I have the same problem(elasticsearch-persistence + kaminari), I will try with the solution of @amal007 馃

I'm going to close this as there is currently support for Kaminari and WillPaginate (and Pagy in progress).

Was this page helpful?
0 / 5 - 0 ratings