© 2017-2022 Domizio DemichelisMIT License

➡ Chat Support on Gitter ➡

Meilisearch Extra

This extra deals with the pagination of Meilisearch results either by creating a Pagy object out of an (already paginated) Meilisearch results or by creating the Pagy and Meilisearch results from the backend params.


See extras for general usage info.

Require the extra in the pagy.rb initializer:

require 'pagy/extras/meilisearch'

Passive mode

If you have an already paginated Meilisearch results, you can get the Pagy object out of it:

@results = Model.ms_search(nil, offset: 10, limit: 10, ...)
@pagy    = Pagy.new_from_meilisearch(@results, ...)

Active Mode

If you want Pagy to control the pagination, getting the page from the params, and returning both the Pagy and the Meilisearch results automatically (from the backend params):

Extend your model:

extend Pagy::Meilisearch
ActiveRecord_Relation.include Pagy::Meilisearch  # <--- (optional) if you use `includes` makes it work as expected

In a controller use pagy_search in place of search:

results         = Article.pagy_search(params[:q])
@pagy, @results = pagy_meilisearch(results, items: 10)


Pasive mode


This constructor accepts a Meilisearch as the first argument, plus the usual optional variable hash. It sets the :items, :page and :count pagy variables extracted/calculated out of the Meilisearch object.

@results = Model.ms_search(nil, offset: 10, limit: 10, ...)
@pagy    = Pagy.new_from_meilisearch(@results, ...)

Notice: you have to take care of manually manage all the params for your search, however the method extracts the :items, :page and :count from the results object, so you don’t need to pass that again. If you prefer to manage the pagination automatically, see below.

Active Mode

Pagy::Meilisearch module

Extend your model with the Pagy::Meilisearch micro-module:

extend Pagy::Meilisearch

The Pagy::Meilisearch adds the pagy_search class method that you must use in place of the standard search method when you want to paginate the search response.

This method accepts the same arguments of the search method and you must use it in its place. This extra uses it in order to capture the arguments, automatically merging the calculated :offset and :limit options before passing them to the standard search method internally.


Variable Description Default
:meilisearch_pagy_search customizable name of the pagy search method :pagy_search
:meilisearch_search customizable name of the original search method :search


This extra adds the pagy_meilisearch method to the Pagy::Backend to be used when you have to paginate a Meilisearch object. It also adds a pagy_meilisearch_get_vars sub-method, used for easy customization of variables by overriding.

pagy_meilisearch(Model.pagy_search(…), vars={}})

This method is similar to the generic pagy method, but specialized for Meilisearch. (see the pagy doc)

It expects to receive a Model.pagy_search(...) result and returns a paginated response. You can use it in a couple of ways:

@pagy, @results = pagy_meilisearch(Model.pagy_search(params[:q]), ...)
@records = @results.results

# or directly with the collection you need (e.g. records)
@pagy, @records = pagy_meilisearch(Model.pagy_search(params[:q]).results, ...)


This sub-method is similar to the pagy_get_vars sub-method, but it is called only by the pagy_meilisearch method. (see the pagy_get_vars doc).