Eager Pagination with Elasticsearch-rails
12 Nov 2014So you’re searching millions of records with Elasticsearch-model
but want to eagerly load all associations when rendering your page for performance reasons. But wait, you’re using Kaminari for pagination,
and when you includes(:association)
, you lose the pagination support.
Below, you’ll see how a simple delegator will relieve your woes and eagerly load associations with support for pagination.
The Problem
Controller
View
Error!
The Solution
Controller
Model
Delegator
What’s going on here? EagerPagination
overrides the def each
method to use the eager loading scope,
but delegates all other calls to the underlying records object that still has Kaminari pagination support.
My initial solution started to dive into the Elasticsearch-model implementation, but in the end I felt that the ES gem shouldn’t burden itself with the responsibilities of handing AR scopes.
I’m happy with how light this final outcome is. Simplicity is king.
Full example can be found in the gist below.