-
Notifications
You must be signed in to change notification settings - Fork 247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Guide on pagination #561
Comments
If you want to create a finished pagination you indeed need to set a limit of results during your search. MeiliSearch is designed to fastly retrieve the best matching documents (the 20 firsts in a basic search request) but not to return the exact and exhaustif number of documents matching your request, for performance reasons. What you concretely have to do is to set the MeiliSearch will return a Example:
💡 Algolia sets the limit to 1000 for their pagination by default (see this page of docs), that's why we also recommend using 1000 as a limitation, but if you want to improve your performance, you can choose a lower limit. |
80: Fix getTotalCount() method r=curquiza a=curquiza Remove `nbHits` usage because this is not reliable information. The Meili team is aware of this. Here are the different issues and comments about it to explain why it's confusing, and why we should not use it: - meilisearch/meilisearch#1120 - meilisearch/documentation#561 - meilisearch/meilisearch-php#119 (comment) TLDR; `nbHits` is not reliable for pagination because can be exhaustive or not, depending on the value of `exhaustiveNbHits` that MeiliSearch returns which is always `false` for the moment. We are sorry for this. We all hope this confusion will be fixed asap in MeiliSearch.⚠️ The linter error in the CI will be fixed with #82 Co-authored-by: Clémentine Urquizar <[email protected]>
@bidoubiwa and @gmourier created a rough draft(private link) of the pagination guide that @guimachiavelli will edit for release alongside v0.28 docs. |
1753: v0.28: Pagination guide r=guimachiavelli a=guimachiavelli Closes #561 Co-authored-by: gui machiavelli <[email protected]> Co-authored-by: gui machiavelli <[email protected]>
1707: v0.28 r=guimachiavelli a=guimachiavelli This is a staging PR for all changes related to Meilisearch v0.28. Please avoid making changes directly to this PR; instead, create new child branches based off this one. Closes #1687, #1688, #1691, #1692, #1693, #1694, #1699, #1700, #1701, #1702, #1703, #1704, #1706, #1722, #1727, #561 Co-authored-by: gui machiavelli <[email protected]> Co-authored-by: gui machiavelli <[email protected]> Co-authored-by: Tommy Melvin <[email protected]> Co-authored-by: Maryam Sulemani <[email protected]> Co-authored-by: Maryam <[email protected]>
For those interested, the docs now have a dedicated guide explaining how to handle pagination in Meilisearch. |
Hello! |
Pagination best practice
MeiliSearch was not made for pagination. The idea is that you never have to "go to page 2".
In case you still want pagination, let us consider two pagination styles.
1. Infinity scroll, or arrow pagination: recommended
This pagination is recognizable by its absence of a numbered paginated page. It will either have no visual clues and will be done in the background while you scroll down infinitely (i.e.: dev.to) or using arrows that let you navigate to the next page of results.
To achieve this pagination you should make searches based on two pieces of information.
Showing a certain amount of hits per batch can be done using the limit. But since you don't want to give the possibility to your user to click on the right arrow if there are no more hits, you need to know where to stop. We suggest to always add
+ 1
on your limit count, if the number of results MeiliSearch returns is lower than this number, you should inform the user that he arrived at the end the results (by graying out the arrow for example).For this example, let's say that we want 100 hits per page. Pagination will work like this. We will be using two search parameter to achieve pagination: limit and offset
batman
with limit:101
and offset:0
.101
results we know that there are more results.batman
with limit:101
and offset:100
101
results we know that there are more results.batman
with limit:100
and offset:200
10
results we know that there are NO more possible results.As you may observe, the only parameter changing between each pagination is the offset.
1000
hits. Because the bigger the offset the longer it takes MeiliSearch to return the next results. This can be done by putting a limit on the offset value. This performance issue is due to the fact that MeiliSearch needs to sortlimit + offset
documents before returning the results. Thus instead of stoping the sort at 20 documents, it would have to stop the sort at 220 documents. Which takes more time.2. Numbered Pagination or Finished Pagination: NOT recommended
this is NOT recommended
Finished pagination is the most known pagination system:
While it has its uses, it is not a style that fits well with MeiliSearch.
No exact knowledge of the nb of hits
Meilisearch search response info
nbHits
is most of the time an upper bound approximation of the numbers of hits. It is not a reliable information unless the other search response infoexhaustiveNbHits
istrue
.Perfomances issues
Another problem comes from performance issues when paginating to lets say page 200. Since we are using a bucket sort, having the first 20 results is way faster than having the 2000th result.
So giving the possibility to a user to directly jump to page 200 is not a good experience.
Because of this behavior we do not recommend a finished pagination.
How to make a numbered pagination with limited damage
If you want to create finished pagination you need to set a limit of results during your search. MeiliSearch is designed to fastly retrieve the best matching documents (the 20 firsts in a basic search request) but not to return the exact and exhaustive number of documents matching your request, for performance reasons.
What you concretely have to do is to set the
limit
search parameter to1000
.MeiliSearch will return a
hits
array (less or equals to 1000) and the length of thishits
array is the number of results -> you can now know the number of pages.Depending on which page you want to display, you just have to take the piece of
hits
you need to display the expected results.Example:
I have a front end application with finished pagination and I want to display 10 results per page.
I search for
prince
withlimit
set to 1000.I get 32 results in
hits
.0
to9
for page 1,10
to19
for page 2, and20
to22
for the last page.💡As this technique lowers the performance of the search **we recommend setting the limit of results to a maximum of 1000 **. If you want to improve your performance, you can choose a lower limit. Algolia also sets the limit to 1000 for their pagination by default (see this page of docs).
Todo
Creation of a guide
A (small) guide should be made explaining how to create a pagination system with MeiliSearch.
This guide will be added in the
search
main concept guide as an example of what can be done using the search route.The guide should only use
curl
and MeiliSearch routes. No other external library. Once it is written withcURL
our other clients (ruby, js, php,..) will be able to add code-samples to match thecURL
examples and thus the guide will be available in all languages.Like this:
To showcase the pagination, use both
limit
andoffset
but without usingnbHits
as it is not a reliable source of information. It should explain to the user how to paginate with arrows :example:
For example, if every page should have 20 results, using the search routes it should look like this:
...
The guide should mention the numbered pagination and explain that it is not recommended.
The text was updated successfully, but these errors were encountered: