Follow this guide to learn more about the two pagination types available Meilisearch.
offset
and limit
, and another using hitsPerPage
and page
.
offset
and limit
to create interfaces centered around “Previous” and “Next” buttons.
Other solutions, such as creating a page selector allowing users to jump to any search results page, make use of hitsPerPage
and page
to obtain the exhaustive total number of matched documents. These tend to be less efficient and may result in decreased performance.
Whatever UI pattern you choose, there is a limited maximum number of search results Meilisearch will return for any given query. You can use the maxTotalHits
index setting to configure this, but be aware that higher limits will negatively impact search performance.
maxTotalHits
to a value higher than the default will negatively impact search performance. Setting maxTotalHits
to values over 20000
may result in queries taking seconds to complete.limit
and offset
search parameters. Response bodies will include an estimatedTotalHits
field, containing a partial count of search results. This is Meilisearch’s default behavior:
limit
and offset
limit
and offset
search parameters.
limit
sets the size of a page. If you set limit
to 10
, Meilisearch’s response will contain a maximum of 10 search results. offset
skips a number of search results. If you set offset
to 20
, Meilisearch’s response will skip the first 20 search results.
For example, you can use Meilisearch’s JavaScript SDK to get the first ten films in a movies database:
offset
limit
to 20
and offset
to 0
, you get the first twenty search results. We can call this our first page.
limit
to 20
and offset
to 40
, you skip the first 40 search results and get documents ranked from 40 through 59. We can call this the third results page.
offset = limit * (target page number - 1)
. In the previous example, the calculation would look like this: offset = 20 * (3 - 1)
. This gives us 40
as the result: offset = 20 * 2 = 40
.
Once a query returns fewer hits
than your configured limit
, you have reached the last results page.
.pagination
, and updates it every time the user moves to a different search results page:
offset
is 0
, as this indicates your user is on the first results page.
To know when to disable the “Next” button, we recommend setting your query’s limit
to the number of results you wish to display per page plus one. That extra hit
should not be shown to the user. Its purpose is to indicate that there is at least one more document to display on the next page.
The following JavaScript snippet runs checks whether we should disable a button every time the user navigates to another search results page:
maxTotalHits
above its default value.
estimatedTotalHits
. This value is likely to change as a user navigates search results and should not be used to create calculate the number of search result pages.
When your query contains either hitsPerPage
, page
, or both these search parameters, Meilisearch returns totalHits
and totalPages
instead of estimatedTotalHits
. totalHits
contains the exhaustive number of results for that query, and totalPages
contains the exhaustive number of pages of search results for the same query:
hitsPerPage
and page
hitsPerPage
defines the maximum number of search results on a page.
Since hitsPerPage
defines the number of results on a page, it has a direct effect on the total number of pages for a query. For example, if a query returns 100 results, setting hitsPerPage
to 25
means you will have four pages of search results. Settings hitsPerPage
to 50
, instead, means you will have only two pages of search results.
The following example returns the first 25 search results for a query:
page
search parameter. If you set hitsPerPage
to 25
and your totalPages
is 4
, page
1
contains documents from 1 to 25. Setting page
to 2
instead returns documents from 26 to 50:
hitsPerPage
and page
take precedence over offset
and limit
. If a query contains either hitsPerPage
or page
, any values passed to offset
and limit
are ignored.totalPages
field included in the response contains the exhaustive count of search result pages based on your query’s hitsPerPage
. Use this to create a numbered list of pages.
For ease of use, queries with hitsPerPage
and page
always return the current page number. This means you do not need to manually keep track of which page you are displaying.
In the following example, we create a list of page buttons dynamically and highlight the current page: