#
Elasticsearch Rails Extra
Paginate ElasticsearchRails
response objects.
#
Setup
pagy.rb (initializer)
require 'pagy/extras/elasticsearch_rails'
#
Modes
This extra offers two ways to paginate ElasticsearchRails
response objects.
Pagy searches and paginates
You use the pagy_search
method in place of the search
method.
#
Usage
Model
extend Pagy::ElasticsearchRails
Controller (pagy_search)
# get the collection in one of the following ways
collection = Article.pagy_search(params[:q])
collection = Article.pagy_search(params[:q]).records
collection = Article.pagy_search(params[:q]).results
# paginate it
@pagy, @response = pagy_elasticsearch_rails(collection, limit: 10)
You search and paginate
Pagy creates its object out of your result.
#
Usage
Controller (search)
# standard response (already paginated)
@response = Article.search('*', from: 0, size: 10, ...)
# get the pagy object out of it
@pagy = Pagy.new_from_elasticsearch_rails(@response, ...)
#
Variables
#
Methods
Pagy::ElasticsearchRails.pagy_search(...)
This method accepts the same arguments of the search
method and you must use it in its place in active mode.
Pagy.new_from_elasticsearch_rails(response, **vars)
This constructor accepts an Elasticsearch::Model::Response::Response
, plus the optional pagy variables. It automatically sets
the :limit
, :page
and :count
pagy variables extracted/calculated out of it.
pagy_elasticsearch_rails(pagy_search_args, **vars)
This method is similar to the generic pagy
method, but specialized for Elasticsearch Rails (see
the pagy doc).
It expects to receive YourModel.pagy_search(...)
result and returns the paginated response.