#
Elasticsearch Rails Extra
Paginate ElasticsearchRails
response objects.
#
Setup
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
extend Pagy::ElasticsearchRails
# 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, items: 10)
You search and paginate
Pagy creates its object out of your result.
#
Usage
# 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 :items
, :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.
pagy_elasticsearch_rails_get_vars(array)
This sub-method is similar to the pagy_get_vars
sub-method, but it is called only by the pagy_elasticsearch_rails
method. (see
the pagy_get_vars doc).