Replies: 6 comments 3 replies
-
If you have an ElasticsearchRails object available, I guess you could just create a pagy object out of it by using the pagy extra in passive mode. And I guess that you can use that pagy object to paginate the AR objects coming out of chewy, because the number of results should match 1 to 1 the AR records. So it might be a lot simpler than what it looks. |
Beta Was this translation helpful? Give feedback.
-
Why this is marked as closed but it's unanswered? Reopening. |
Beta Was this translation helpful? Give feedback.
-
@dlegr250 any feedback about my answer? |
Beta Was this translation helpful? Give feedback.
-
@ddnexus I built a working version built on top of Chewy without touching the ES-rails object, but I am also going to try getting the ES-rails client from Chewy directly as you mentioned and try that out. I'll try that tomorrow at my work project and post my results to this thread. |
Beta Was this translation helpful? Give feedback.
-
The version that works on my project using Chewy and Pagy is:
The I didn't get a good version working connecting to the Elasticsearch-rails object internally held by Chewy, because Chewy adds additional functionality that I needed, so trying to retrieve records from ES object itself wouldn't work in my situation. But the above code seems to work just fine without having to make any further additions to Pagy or Chewy! |
Beta Was this translation helpful? Give feedback.
-
@dlegr250 thank you for the feedback
Indeed I wasn't suggesting to use the raw ES objects, but the AR objects provided by chewy exactly as you currently do. The only difference would be extracting the pagy object instead of manually creating it. chewy_query = SomeIndex.query(...)
# you can even put the next line directly in the view instead of pre-building the object, if you want to delay it
@pagy = Pagy.new_from_elasticsearch_rails(chewy_query.whatever_is_the_metod_to_get_the_elasticsearch_response) That would be the less app-invasive way to use pagy: i.e. leveraging the native/built-in ES pagination. Pagy would merely adapt to whatever pagination has already been performed by chewy or ES or whatever does it. |
Beta Was this translation helpful? Give feedback.
-
I see Pagy has an implementation for
elasticsearch-rails
gem itself, where it's getting the response from Elasticsearch and generating the pagination for it.However, a project I'm on uses
chewy
, which is built on top ofelasticsearch-rails
and provides useful features not found in the base gem itself. Thechewy
gem autoloads either thekaminari
orwill_paginate
gems for pagination, if they are present, which does cause me some frustration.It looks like the only way I can get Pagy to work with
chewy
is to ensure that neitherkaminari
orwill_paginate
are included in the project, and then I have to use the raw Pagy object itself to get the results.In short,
chewy
wraps theelasticsearch-rails
results and the#objects
method returns theActiveRecord
objects themselves, andchewy
implements a#limit
and#offset
method that is used for pagination. Reading the Pagy docs, it looks like Pagy uses those methods automatically for limit/offset pagination.I did some basic benchmarking using
benchmark-memory
for this setup and Pagy does indeed have about a 40-50% memory reduction, but the difficulty was I had to add/remove gems and run the benchmarks separately because I could not have both Pagy andwill_paginate
running together in the project becausechewy
would automatically load and usewill_paginate
and Pagy was then doubling the objects used because the pagination was being done twice.I'm going to reach out to the
chewy
repo separately and ask about how they handle optional pagination or if there's a better way to turn off automatic loading of pagination, but I also wanted to see if anyone on the Pagy discussions could let me know if the approach I'm using above is an appropriate use of Pagy for this?I'm used to using Pagy with the standard
ActiveRecord
setup, so I've never really had to dive deep into the inner workings of it.Beta Was this translation helpful? Give feedback.
All reactions