From f3f36ec5c67d42bcb7a7e8377023cdc29f024c3c Mon Sep 17 00:00:00 2001 From: gui machiavelli Date: Tue, 21 Jun 2022 18:54:42 +0200 Subject: [PATCH 01/11] pagination guide: zeroth draft --- learn/advanced/pagination.md | 125 +++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 learn/advanced/pagination.md diff --git a/learn/advanced/pagination.md b/learn/advanced/pagination.md new file mode 100644 index 0000000000..ac7cd8adf1 --- /dev/null +++ b/learn/advanced/pagination.md @@ -0,0 +1,125 @@ +# Pagination of search results + +In a perfect world, users would not need to look beyond the first search result to find what they were looking for. In practice, this is difficult to achieve due to design constraints, performance trade-offs, and even inneffective query terms. Because of that, it is usually necessary to create a pagination interface so users can browse through long lists of results. + +In this guide, we will discuss some of Meilisearch's current limitations, how these limitations impact common pagination interface patterns, and the recommended way of handling pagination when using Meilisearch. + +## Choosing the right pagination interface + +For performance reasons, Meilisearch cannot provide you with the exact number of possible results for a query. Instead, you can only know the `estimatedTotalHits` when using our search API. As its name indicates, `estimatedTotalHits` is only an estimate of how many documents match your user's query. + +Additionally, Meilisearch is optimized to return the most relevant results as fast as possible. The negative side of this design decision is that queries might become slower the further away we get from the first results. + +Because you cannot know the number of total hits and slower queries the further we move from the first result, we do not recommend creating interfaces with page selectors that allow users to jump to any arbitrary result page. If this type of interface is crucial for your user's search experience, this document's last section lists a number of tips that might help you work around Meilisearch's current limitations. + +Instead of a full page selector, we recommend creating pagination interfaces centered around previous and next buttons. + +::: note +By default, Meilisearch only returns a maximum of 1000 search results. Consult our [index setting reference](/reference/api/settings.md) to know how to change this. +::: + +## Recommended: Previous and next buttons + +Pagination interfaces that rely solely on previous and next buttons are similar to page selectors, but do not allow users to jump to an arbitrary results page. + +![Placeholder image for prev/next interface]() + +Though this approach offers less precision than a full-blown page selector, it does not require knowing the number of total search results. + +### Implementation + +Previous and next buttons can be implemented using the [`limit`](/reference/api/search.md#limit) and [`offset`](/reference/api/search.md#offset) search parameters. + +Paginating requires providing an `offset` that is equal to your `limit` times the page number times: `offset = limit * page number`. + +For example, if you set `limit` to `20` and want the first page of search results, your `offset` must be 0: `offset = 0 * 20`. + +```sh +curl \ + -X POST 'http://localhost:7700/indexes/movies/search' \ + -H 'Content-Type: application/json' \ + --data-binary '{ + "q": "shifu", + "limit": 20, + "offset": 0 + }' +``` + +If you keep the same `limit` and want the third page of results, `offset` must be `40`: `offset = 2 * 20`. + +```sh +curl \ + -X POST 'http://localhost:7700/indexes/movies/search' \ + -H 'Content-Type: application/json' \ + --data-binary '{ + "q": "shifu", + "limit": 20, + "offset": 40 + }' +``` + +As you might have noticed, we start counting page numbers from 0. This means the first page of results is 0, the second page is 1, and so forth. + +It is often helpful to disable navigation buttons when the user cannot move to next or the "previous page. The "previous" button should be disabled whenever your `offset` is 0. + +To disable the "next" button, we recommend setting your `limit` to the number of displayed results plus 1. For example, if you want each page to display 20 search results, set `limit` to 21. If the number of returned items is less than 21, we can safely assume we are in the last page. + +```js +const results = await index.search("x", { limit: 21, offset: 0 }) + +// Previous button +if (offset === 0 ) { + // last page + document.querySelector("#previous_button").disabled +} + +// Next button +if (results.hits.length < limit ) { + // last page + document.querySelector("#next_button").disabled +} +``` + +## Not recommended: Page selection + +As its name states, this type of pagination is made of a numbered list of pages accompanied by next and previous buttons. + +[UI screenshot] + +![](https://vuejsexamples.com/content/images/2018/11/vue-pagination.gif) + +This is a very common type of interface and offers users a significant amount of precision when navigating results. Despite being similar to our recommended pagination solution, the page selector requires knowing the exact number of total matches. This makes it difficult to implement when using Meilisearch. + +### Implementation + +The general implementation of a page selection interface is similar to our recommended solution, using previous and next buttons: you must use the `limit` and `offset` search parameters to set your page size and navigate between search results. + +To create the page list, however, you must know the exact number of total results. For example, if you have 100 results and your search result pages contain 10 results each, your selector must show a list of numbers going from 1 to 10. Meilisearch, however, can only give you an estimate of the total number of search results. + +We recommend two different workarounds to create this kind of pagination interface with Meilisearch. + +#### Set a hard limit of results during your search + +By default, a search request returns 20 search results. You can change this value to a much higher number and treat it as the effective maximum of search results you will show a user. Doing so means the size of your `hits` array is the exact number of search results you have to paginate. + +For example, if you set `limit` to 300, every search request made to Meilisearch returns at most 300 documents. If a query returns a `hits` array with 200 items and you want each page to display 20 results, you can create a page selector with 10 pages. + +This method provides control and reliability. It also prevents users from paginating too far, which avoids performance loss. However, this limits the number of results your users can see. + +::: note +We do not recommend setting high values for `limit` as it can negatively impact performance. +::: + +#### Accept the unreliable number + +You can still use `estimatedTotalHits` to calculate the number of search result pages. This means your page count is likely to change until Meilisearch retrives the last search result. + +For example, a query's `estimatedTotalHits` might be `100` when you fetch the first page of search results. If you are showing 20 results per page, this means your interface will display a page selector with 5 pages. When you fetch the fifth and last page in Meilisearch's current estimate, however, `estimatedTotalHits` might change to `300`. Your page list, previously displaying 5 pages, must now show 10 total pages. + +This method gives users access to all search results. However, it also results in an interface that might occasionally change in front of the user. + +## Unsatisfied? Let us know + +We are actively working on improving Meilisearch pagination. Is our recommended pagination method not suitable for the application you're developing? Is page selection critical for your website? Please let us know in this GitHub discussion. + +Is creating pagination interfaces in Meilisearch for you this is a huge issue, please provide feedback on [this discussion](https://github.com/meilisearch/product/discussions/483). \ No newline at end of file From 4f76ad2430308914f5f16946b321aea88df5f04d Mon Sep 17 00:00:00 2001 From: gui machiavelli Date: Wed, 22 Jun 2022 19:50:09 +0200 Subject: [PATCH 02/11] pagination guide: slightly improved draft --- learn/advanced/pagination.md | 90 ++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 49 deletions(-) diff --git a/learn/advanced/pagination.md b/learn/advanced/pagination.md index ac7cd8adf1..8cef0a8b82 100644 --- a/learn/advanced/pagination.md +++ b/learn/advanced/pagination.md @@ -4,18 +4,16 @@ In a perfect world, users would not need to look beyond the first search result In this guide, we will discuss some of Meilisearch's current limitations, how these limitations impact common pagination interface patterns, and the recommended way of handling pagination when using Meilisearch. -## Choosing the right pagination interface +## Choosing the right pagination UI -For performance reasons, Meilisearch cannot provide you with the exact number of possible results for a query. Instead, you can only know the `estimatedTotalHits` when using our search API. As its name indicates, `estimatedTotalHits` is only an estimate of how many documents match your user's query. +For performance reasons, Meilisearch cannot provide the exact number of possible results for a query. When using the search endpoint, responses contain an `estimatedTotalHits` field. As its name indicates, `estimatedTotalHits` is only an estimate of how many documents match your user's query. -Additionally, Meilisearch is optimized to return the most relevant results as fast as possible. The negative side of this design decision is that queries might become slower the further away we get from the first results. +Because of this, we do not recommend creating interfaces with page selectors that allow users to jump to an arbitrary results page. If page selection is crucial to the software you are developing, this document's last section lists a number of tips that might help you work around Meilisearch's current limitations. -Because you cannot know the number of total hits and slower queries the further we move from the first result, we do not recommend creating interfaces with page selectors that allow users to jump to any arbitrary result page. If this type of interface is crucial for your user's search experience, this document's last section lists a number of tips that might help you work around Meilisearch's current limitations. - -Instead of a full page selector, we recommend creating pagination interfaces centered around previous and next buttons. +Instead of a full page selector, [we recommend creating pagination interfaces centered around previous and next buttons](#recommended-previous-and-next-buttons). ::: note -By default, Meilisearch only returns a maximum of 1000 search results. Consult our [index setting reference](/reference/api/settings.md) to know how to change this. +By default, Meilisearch returns a maximum of 1000 search results. Consult our [index setting reference](/reference/api/settings.md) if you need to change this. ::: ## Recommended: Previous and next buttons @@ -30,53 +28,47 @@ Though this approach offers less precision than a full-blown page selector, it d Previous and next buttons can be implemented using the [`limit`](/reference/api/search.md#limit) and [`offset`](/reference/api/search.md#offset) search parameters. -Paginating requires providing an `offset` that is equal to your `limit` times the page number times: `offset = limit * page number`. +Paginating requires providing an `offset` that is equal to your `limit` times the page number times: `offset = limit * page number`. We recommend starting page count from 0 instead of 1 when calculating offset values. For example, if you set `limit` to `20` and want the first page of search results, your `offset` must be 0: `offset = 0 * 20`. -```sh -curl \ - -X POST 'http://localhost:7700/indexes/movies/search' \ - -H 'Content-Type: application/json' \ - --data-binary '{ - "q": "shifu", - "limit": 20, - "offset": 0 - }' +```js +const results = await index.search("x", { limit: 20, offset: 0 }); ``` If you keep the same `limit` and want the third page of results, `offset` must be `40`: `offset = 2 * 20`. -```sh -curl \ - -X POST 'http://localhost:7700/indexes/movies/search' \ - -H 'Content-Type: application/json' \ - --data-binary '{ - "q": "shifu", - "limit": 20, - "offset": 40 - }' +```js +const results = await index.search("x", { limit: 20, offset: 40 }); ``` -As you might have noticed, we start counting page numbers from 0. This means the first page of results is 0, the second page is 1, and so forth. +Once a query returns less than your configured `limit`, you have reached the last results page. -It is often helpful to disable navigation buttons when the user cannot move to next or the "previous page. The "previous" button should be disabled whenever your `offset` is 0. +It is often helpful to disable navigation buttons when the user cannot move to the "next" or "previous" page. The "previous" button should be disabled whenever your `offset` is 0. -To disable the "next" button, we recommend setting your `limit` to the number of displayed results plus 1. For example, if you want each page to display 20 search results, set `limit` to 21. If the number of returned items is less than 21, we can safely assume we are in the last page. +To disable the "next" button, we recommend setting your query's `limit` to the number of displayed results plus one. The extra result indicates that you have at least one item beyond what you can display in a single results page: ```js const results = await index.search("x", { limit: 21, offset: 0 }) -// Previous button if (offset === 0 ) { - // last page - document.querySelector("#previous_button").disabled + // If offset equals 0, we're on the first results page + // and can disable the "previous" buttons + document.querySelector("#previous_button").disabled = true; +} else { + document.querySelector("#previous_button").disabled = false; } -// Next button -if (results.hits.length < limit ) { - // last page - document.querySelector("#next_button").disabled +if (results.hits.length < 21 ) { + // If Meilisearch returns 20 items or less, + // we don't have any more results to + // and can disable the "next" + document.querySelector("#next_button").disabled = true; +} else { + // If Meilisearch returns exactly 21 results + // and our page can only show 20 items at a time, + // we have at least one more page with 1 result in it + document.querySelector("#next_button").disabled = false; } ``` @@ -84,17 +76,17 @@ if (results.hits.length < limit ) { As its name states, this type of pagination is made of a numbered list of pages accompanied by next and previous buttons. -[UI screenshot] - -![](https://vuejsexamples.com/content/images/2018/11/vue-pagination.gif) +![placeholder page selection UI](https://vuejsexamples.com/content/images/2018/11/vue-pagination.gif) -This is a very common type of interface and offers users a significant amount of precision when navigating results. Despite being similar to our recommended pagination solution, the page selector requires knowing the exact number of total matches. This makes it difficult to implement when using Meilisearch. +This is is a common UI pattern that offers users a significant amount of precision when navigating results. ### Implementation -The general implementation of a page selection interface is similar to our recommended solution, using previous and next buttons: you must use the `limit` and `offset` search parameters to set your page size and navigate between search results. +The general implementation of a page selection interface is similar to our [recommended solution](#recommended-previous-and-next-buttons), but you must also display a page list. -To create the page list, however, you must know the exact number of total results. For example, if you have 100 results and your search result pages contain 10 results each, your selector must show a list of numbers going from 1 to 10. Meilisearch, however, can only give you an estimate of the total number of search results. +To create the page list, however, you must know the exact number of total results. For example, if you have 100 results and your search result pages contain 10 results each, your selector must show a list of numbers going from 1 to 10. + +Since Meilisearch can only give you an estimate of total search results, it can be difficult to implement page selectors when using Meilisearch. We recommend two different workarounds to create this kind of pagination interface with Meilisearch. @@ -104,22 +96,22 @@ By default, a search request returns 20 search results. You can change this valu For example, if you set `limit` to 300, every search request made to Meilisearch returns at most 300 documents. If a query returns a `hits` array with 200 items and you want each page to display 20 results, you can create a page selector with 10 pages. +// perhaps a code sample here? + This method provides control and reliability. It also prevents users from paginating too far, which avoids performance loss. However, this limits the number of results your users can see. ::: note -We do not recommend setting high values for `limit` as it can negatively impact performance. +We do not recommend caution when setting high values for `limit` as it can negatively impact performance. ::: #### Accept the unreliable number -You can still use `estimatedTotalHits` to calculate the number of search result pages. This means your page count is likely to change until Meilisearch retrives the last search result. +You use `estimatedTotalHits` to calculate the number of search result pages. This means your page count is likely to change until Meilisearch retrives the last search result. -For example, a query's `estimatedTotalHits` might be `100` when you fetch the first page of search results. If you are showing 20 results per page, this means your interface will display a page selector with 5 pages. When you fetch the fifth and last page in Meilisearch's current estimate, however, `estimatedTotalHits` might change to `300`. Your page list, previously displaying 5 pages, must now show 10 total pages. +For example, a query's `estimatedTotalHits` might be `100` when you fetch the first page of search results. If you are showing 20 results per page, this means your interface will display a page selector with 5 pages. When you fetch the fifth and last page according to Meilisearch's current estimate, however, `estimatedTotalHits` might change to `300`. Your page list, previously displaying 5 pages, must now show 10 total pages. -This method gives users access to all search results. However, it also results in an interface that might occasionally change in front of the user. +This method gives users access to all search results. However, it also results in an interface that might feel unreliable due to constant and unpredictable changes. ## Unsatisfied? Let us know -We are actively working on improving Meilisearch pagination. Is our recommended pagination method not suitable for the application you're developing? Is page selection critical for your website? Please let us know in this GitHub discussion. - -Is creating pagination interfaces in Meilisearch for you this is a huge issue, please provide feedback on [this discussion](https://github.com/meilisearch/product/discussions/483). \ No newline at end of file +Is the current state of pagination in Meilisearch negatively impacting you? Please share your thoughts with us in this [GitHub discussion](https://github.com/meilisearch/product/discussions/483). We are actively working on improving this aspect of Meilisearch and your input is greatly appreciated. From 9250fc03452414d8a562b546c16ccb744cf81d26 Mon Sep 17 00:00:00 2001 From: gui machiavelli Date: Tue, 28 Jun 2022 16:47:30 +0200 Subject: [PATCH 03/11] Apply suggestions from code review Co-authored-by: Tommy <68053732+dichotommy@users.noreply.github.com> --- learn/advanced/pagination.md | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/learn/advanced/pagination.md b/learn/advanced/pagination.md index 8cef0a8b82..d2479e081b 100644 --- a/learn/advanced/pagination.md +++ b/learn/advanced/pagination.md @@ -1,16 +1,16 @@ # Pagination of search results -In a perfect world, users would not need to look beyond the first search result to find what they were looking for. In practice, this is difficult to achieve due to design constraints, performance trade-offs, and even inneffective query terms. Because of that, it is usually necessary to create a pagination interface so users can browse through long lists of results. +In a perfect world, users would not need to look beyond the first search result to find what they were looking for. In practice, however, it is usually necessary to create some kind of pagination interface so users can browse through long lists of results. In this guide, we will discuss some of Meilisearch's current limitations, how these limitations impact common pagination interface patterns, and the recommended way of handling pagination when using Meilisearch. ## Choosing the right pagination UI -For performance reasons, Meilisearch cannot provide the exact number of possible results for a query. When using the search endpoint, responses contain an `estimatedTotalHits` field. As its name indicates, `estimatedTotalHits` is only an estimate of how many documents match your user's query. +For performance reasons, Meilisearch cannot provide the exact number of results for a query. When using the search endpoint, responses contain an `estimatedTotalHits` field. As its name indicates, `estimatedTotalHits` is only an estimate of how many documents match your user's query. -Because of this, we do not recommend creating interfaces with page selectors that allow users to jump to an arbitrary results page. If page selection is crucial to the software you are developing, this document's last section lists a number of tips that might help you work around Meilisearch's current limitations. +Because of this, we do not recommend creating interfaces with page selectors that allow users to jump to a specific page. If page selection is crucial to the software you are developing, see the [last section of this page](#not-recommended-page-selection) for tips that might help you work around Meilisearch's current limitations. -Instead of a full page selector, [we recommend creating pagination interfaces centered around previous and next buttons](#recommended-previous-and-next-buttons). +We recommend creating pagination interfaces centered around previous and next buttons. ::: note By default, Meilisearch returns a maximum of 1000 search results. Consult our [index setting reference](/reference/api/settings.md) if you need to change this. @@ -18,11 +18,11 @@ By default, Meilisearch returns a maximum of 1000 search results. Consult our [i ## Recommended: Previous and next buttons -Pagination interfaces that rely solely on previous and next buttons are similar to page selectors, but do not allow users to jump to an arbitrary results page. +Using previous and next buttons for pagination means that users can easily page through results, but don't have the ability to jump to an arbitrary results page. ![Placeholder image for prev/next interface]() -Though this approach offers less precision than a full-blown page selector, it does not require knowing the number of total search results. +Though this approach offers less precision than a full-blown page selector, it does not require knowing the precise number of search results. This makes it a good fit for Meilisearch's current capabilities. ### Implementation @@ -36,17 +36,17 @@ For example, if you set `limit` to `20` and want the first page of search result const results = await index.search("x", { limit: 20, offset: 0 }); ``` -If you keep the same `limit` and want the third page of results, `offset` must be `40`: `offset = 2 * 20`. +If you want the third page of results, `offset` must be `40`: `offset = 2 * 20`. ```js const results = await index.search("x", { limit: 20, offset: 40 }); ``` -Once a query returns less than your configured `limit`, you have reached the last results page. +Once a query returns fewer `hits` than your configured `limit`, you have reached the last results page. It is often helpful to disable navigation buttons when the user cannot move to the "next" or "previous" page. The "previous" button should be disabled whenever your `offset` is 0. -To disable the "next" button, we recommend setting your query's `limit` to the number of displayed results plus one. The extra result indicates that you have at least one item beyond what you can display in a single 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: ```js const results = await index.search("x", { limit: 21, offset: 0 }) @@ -61,8 +61,8 @@ if (offset === 0 ) { if (results.hits.length < 21 ) { // If Meilisearch returns 20 items or less, - // we don't have any more results to - // and can disable the "next" + // we don't have any more results to display + // and can disable the "next" button document.querySelector("#next_button").disabled = true; } else { // If Meilisearch returns exactly 21 results @@ -74,15 +74,15 @@ if (results.hits.length < 21 ) { ## Not recommended: Page selection -As its name states, this type of pagination is made of a numbered list of pages accompanied by next and previous buttons. +This type of pagination consists of a numbered list of pages accompanied by next and previous buttons. ![placeholder page selection UI](https://vuejsexamples.com/content/images/2018/11/vue-pagination.gif) -This is is a common UI pattern that offers users a significant amount of precision when navigating results. +This is a common UI pattern that offers users a significant amount of precision when navigating results. However, due to Meilisearch's [limitations](#choosing-the-right-pagination-ui), it is not a good fit for pagination with Meilisearch. ### Implementation -The general implementation of a page selection interface is similar to our [recommended solution](#recommended-previous-and-next-buttons), but you must also display a page list. +The general implementation of a page selection interface is similar to our [recommended solution](#recommended-previous-and-next-buttons), but also includes a numbered page list. To create the page list, however, you must know the exact number of total results. For example, if you have 100 results and your search result pages contain 10 results each, your selector must show a list of numbers going from 1 to 10. @@ -90,7 +90,7 @@ Since Meilisearch can only give you an estimate of total search results, it can We recommend two different workarounds to create this kind of pagination interface with Meilisearch. -#### Set a hard limit of results during your search +#### Set a hard limit on search results By default, a search request returns 20 search results. You can change this value to a much higher number and treat it as the effective maximum of search results you will show a user. Doing so means the size of your `hits` array is the exact number of search results you have to paginate. @@ -101,7 +101,7 @@ For example, if you set `limit` to 300, every search request made to Meilisearch This method provides control and reliability. It also prevents users from paginating too far, which avoids performance loss. However, this limits the number of results your users can see. ::: note -We do not recommend caution when setting high values for `limit` as it can negatively impact performance. +We recommend caution when setting high values for `limit` as it can negatively impact performance. ::: #### Accept the unreliable number From a99a123bcb1128cb3fdb8bea2dea3b80fa86beab Mon Sep 17 00:00:00 2001 From: gui machiavelli Date: Tue, 28 Jun 2022 16:48:10 +0200 Subject: [PATCH 04/11] Apply suggestions from code review Co-authored-by: Tommy <68053732+dichotommy@users.noreply.github.com> --- learn/advanced/pagination.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/learn/advanced/pagination.md b/learn/advanced/pagination.md index d2479e081b..fa35fab70a 100644 --- a/learn/advanced/pagination.md +++ b/learn/advanced/pagination.md @@ -106,7 +106,7 @@ We recommend caution when setting high values for `limit` as it can negatively i #### Accept the unreliable number -You use `estimatedTotalHits` to calculate the number of search result pages. This means your page count is likely to change until Meilisearch retrives the last search result. +In this pagination method, use `estimatedTotalHits` to calculate the number of search result pages. This means your number of results and page count are likely to change until Meilisearch retrieves the last search result. For example, a query's `estimatedTotalHits` might be `100` when you fetch the first page of search results. If you are showing 20 results per page, this means your interface will display a page selector with 5 pages. When you fetch the fifth and last page according to Meilisearch's current estimate, however, `estimatedTotalHits` might change to `300`. Your page list, previously displaying 5 pages, must now show 10 total pages. From d63d87d3d4e30a57bf5d50bd94be686ed97f0dcd Mon Sep 17 00:00:00 2001 From: gui machiavelli Date: Tue, 28 Jun 2022 17:11:44 +0200 Subject: [PATCH 05/11] Apply suggestions from code review Co-authored-by: Tommy <68053732+dichotommy@users.noreply.github.com> --- learn/advanced/pagination.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/learn/advanced/pagination.md b/learn/advanced/pagination.md index fa35fab70a..0c6c78dbb8 100644 --- a/learn/advanced/pagination.md +++ b/learn/advanced/pagination.md @@ -28,7 +28,7 @@ Though this approach offers less precision than a full-blown page selector, it d Previous and next buttons can be implemented using the [`limit`](/reference/api/search.md#limit) and [`offset`](/reference/api/search.md#offset) search parameters. -Paginating requires providing an `offset` that is equal to your `limit` times the page number times: `offset = limit * page number`. We recommend starting page count from 0 instead of 1 when calculating offset values. +Paginating requires providing an `offset` that is equal to your `limit` times the page number: `offset = limit * page number`. We recommend starting page count from 0 instead of 1 when calculating offset values. For example, if you set `limit` to `20` and want the first page of search results, your `offset` must be 0: `offset = 0 * 20`. From 9d070d1ae09012b2e435c7505e722bd8c3b9de18 Mon Sep 17 00:00:00 2001 From: gui machiavelli Date: Wed, 29 Jun 2022 12:45:36 +0200 Subject: [PATCH 06/11] pagination guide: first draft --- learn/advanced/pagination.md | 71 +++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/learn/advanced/pagination.md b/learn/advanced/pagination.md index 0c6c78dbb8..f2bff6a617 100644 --- a/learn/advanced/pagination.md +++ b/learn/advanced/pagination.md @@ -1,20 +1,18 @@ # Pagination of search results -In a perfect world, users would not need to look beyond the first search result to find what they were looking for. In practice, however, it is usually necessary to create some kind of pagination interface so users can browse through long lists of results. +In a perfect world, users would not need to look beyond the first search result to find what they were looking for. In practice, however, it is usually necessary to create some kind of pagination interface to browse through long lists of results. In this guide, we will discuss some of Meilisearch's current limitations, how these limitations impact common pagination interface patterns, and the recommended way of handling pagination when using Meilisearch. ## Choosing the right pagination UI -For performance reasons, Meilisearch cannot provide the exact number of results for a query. When using the search endpoint, responses contain an `estimatedTotalHits` field. As its name indicates, `estimatedTotalHits` is only an estimate of how many documents match your user's query. +There are quite a few pagination interfaces you might want to implement in your application. Many common UI patterns have a page selector allowing users to jump to any search results page. To create a page selector, you must know the exact number of total results so you can calculate the precise number of results pages. -Because of this, we do not recommend creating interfaces with page selectors that allow users to jump to a specific page. If page selection is crucial to the software you are developing, see the [last section of this page](#not-recommended-page-selection) for tips that might help you work around Meilisearch's current limitations. +For performance reasons, however, Meilisearch cannot provide the exact number of results for a query. Instead, When using the search endpoint, responses contain an `estimatedTotalHits` field. As its name indicates, `estimatedTotalHits` is only an estimate of how many documents match your user's query. -We recommend creating pagination interfaces centered around previous and next buttons. +Because of this, we do not recommend creating interfaces with page selectors. If page selection is crucial to the software you are developing, see the [last section of this page](#not-recommended-page-selection) for tips that might help you work around Meilisearch's current limitations. -::: note -By default, Meilisearch returns a maximum of 1000 search results. Consult our [index setting reference](/reference/api/settings.md) if you need to change this. -::: +We recommend creating pagination interfaces centered around [previous and next buttons](#recommended-previous-and-next-buttons). ## Recommended: Previous and next buttons @@ -26,48 +24,63 @@ Though this approach offers less precision than a full-blown page selector, it d ### Implementation +#### `limit` and `offset` + Previous and next buttons can be implemented using the [`limit`](/reference/api/search.md#limit) and [`offset`](/reference/api/search.md#offset) search parameters. -Paginating requires providing an `offset` that is equal to your `limit` times the page number: `offset = limit * page number`. We recommend starting page count from 0 instead of 1 when calculating offset values. +`limit` sets the size of page. If you set `limit` to 20, Meilisearch's response will contain a maximum of 20 search results. `offset` skips a number of search results. If you set `offset` to 40, Meilisearch's response will skip the first 40 search results. + +You can use both parameters together to effectively create search pages. + +#### Search pages and calculating `offset` -For example, if you set `limit` to `20` and want the first page of search results, your `offset` must be 0: `offset = 0 * 20`. +If you set `limit` to 20 and `offset` to 0, you get the first twenty results. We can call this our first page. ```js const results = await index.search("x", { limit: 20, offset: 0 }); ``` -If you want the third page of results, `offset` must be `40`: `offset = 2 * 20`. +Likewise, if you set `limit` to 20 and `offset` to 40, you skip the first 40 search results and get documents ranked from 41 through 60. We can call this the third results page. ```js const results = await index.search("x", { limit: 20, offset: 40 }); ``` +Use this formula to quickly calculate a page's offset value: `offset = limit * (page number - 1)`. + Once a query returns fewer `hits` than your configured `limit`, you have reached the last results page. -It is often helpful to disable navigation buttons when the user cannot move to the "next" or "previous" page. The "previous" button should be disabled whenever your `offset` is 0. +#### Disabling navigation buttons for first and last pages + +It is often helpful to disable navigation buttons when the user cannot move to the "next" or "previous" page. + +The "previous" button should be disabled whenever your `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: +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: ```js const results = await index.search("x", { limit: 21, offset: 0 }) +// If offset equals 0, we're on the first results page if (offset === 0 ) { - // If offset equals 0, we're on the first results page - // and can disable the "previous" buttons document.querySelector("#previous_button").disabled = true; -} else { +} + +// If offset is bigger than 0, we're not on the first results page +if (offset > 0 ) { document.querySelector("#previous_button").disabled = false; } +// If Meilisearch returns 20 items or less, +// we are on the last page if (results.hits.length < 21 ) { - // If Meilisearch returns 20 items or less, - // we don't have any more results to display - // and can disable the "next" button document.querySelector("#next_button").disabled = true; -} else { - // If Meilisearch returns exactly 21 results - // and our page can only show 20 items at a time, - // we have at least one more page with 1 result in it +} + +// If Meilisearch returns exactly 21 results +// and our page can only show 20 items at a time, +// we have at least one more page with one result in it +if (results.hits.length === 21 ) { document.querySelector("#next_button").disabled = false; } ``` @@ -84,13 +97,13 @@ This is a common UI pattern that offers users a significant amount of precision The general implementation of a page selection interface is similar to our [recommended solution](#recommended-previous-and-next-buttons), but also includes a numbered page list. -To create the page list, however, you must know the exact number of total results. For example, if you have 100 results and your search result pages contain 10 results each, your selector must show a list of numbers going from 1 to 10. +To create a numbered page list, however, you must know the exact number of total results. For example, if you have 100 results and your search result pages contain 10 results each, your selector must show a list of numbers going from 1 to 10. -Since Meilisearch can only give you an estimate of total search results, it can be difficult to implement page selectors when using Meilisearch. +Since Meilisearch can only give you an estimate of total search results, it is difficult to implement page selectors when using Meilisearch. We recommend two different workarounds to create this kind of pagination interface with Meilisearch. -#### Set a hard limit on search results +#### Use `limit` to set a maximum number of search results By default, a search request returns 20 search results. You can change this value to a much higher number and treat it as the effective maximum of search results you will show a user. Doing so means the size of your `hits` array is the exact number of search results you have to paginate. @@ -98,15 +111,15 @@ For example, if you set `limit` to 300, every search request made to Meilisearch // perhaps a code sample here? -This method provides control and reliability. It also prevents users from paginating too far, which avoids performance loss. However, this limits the number of results your users can see. +This method provides control and reliability. It also prevents users from paginating too far, which might result in performance gains. However, it limits the number of results your users can see. Additionally, we recommend caution when setting high values for `limit` as it can negatively impact performance. ::: note -We recommend caution when setting high values for `limit` as it can negatively impact performance. +By default, Meilisearch returns a maximum of 1000 search results. Consult our [index setting reference](/reference/api/pagination.md#maxtotalhits-1) if you need to change this. ::: -#### Accept the unreliable number +#### Use `estimatedTotalHits` -In this pagination method, use `estimatedTotalHits` to calculate the number of search result pages. This means your number of results and page count are likely to change until Meilisearch retrieves the last search result. +Though we advise against it, you can use `estimatedTotalHits` to calculate the number of search result pages. This means your number of results and page count are likely to change until Meilisearch retrieves the last search result. For example, a query's `estimatedTotalHits` might be `100` when you fetch the first page of search results. If you are showing 20 results per page, this means your interface will display a page selector with 5 pages. When you fetch the fifth and last page according to Meilisearch's current estimate, however, `estimatedTotalHits` might change to `300`. Your page list, previously displaying 5 pages, must now show 10 total pages. From 655c2627f7b02dd63136117cb05a198351f736aa Mon Sep 17 00:00:00 2001 From: gui machiavelli Date: Mon, 4 Jul 2022 16:42:13 +0200 Subject: [PATCH 07/11] Update learn/advanced/pagination.md Co-authored-by: Guillaume Mourier --- learn/advanced/pagination.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/learn/advanced/pagination.md b/learn/advanced/pagination.md index f2bff6a617..f1be11ad01 100644 --- a/learn/advanced/pagination.md +++ b/learn/advanced/pagination.md @@ -16,7 +16,7 @@ We recommend creating pagination interfaces centered around [previous and next b ## Recommended: Previous and next buttons -Using previous and next buttons for pagination means that users can easily page through results, but don't have the ability to jump to an arbitrary results page. +Using previous and next buttons for pagination means that users can easily navigate through results, but don't have the ability to jump to an arbitrary results page. ![Placeholder image for prev/next interface]() From 60890b667dbfd3d0f5bd0552081d70a0d0cfa6c8 Mon Sep 17 00:00:00 2001 From: gui machiavelli Date: Tue, 5 Jul 2022 19:15:05 +0200 Subject: [PATCH 08/11] Address review feedback --- learn/advanced/pagination.md | 162 ++++++++++++++++++++++++++++------- 1 file changed, 129 insertions(+), 33 deletions(-) diff --git a/learn/advanced/pagination.md b/learn/advanced/pagination.md index f1be11ad01..2dc3a42524 100644 --- a/learn/advanced/pagination.md +++ b/learn/advanced/pagination.md @@ -1,4 +1,4 @@ -# Pagination of search results +# Search result pagination In a perfect world, users would not need to look beyond the first search result to find what they were looking for. In practice, however, it is usually necessary to create some kind of pagination interface to browse through long lists of results. @@ -6,13 +6,13 @@ In this guide, we will discuss some of Meilisearch's current limitations, how th ## Choosing the right pagination UI -There are quite a few pagination interfaces you might want to implement in your application. Many common UI patterns have a page selector allowing users to jump to any search results page. To create a page selector, you must know the exact number of total results so you can calculate the precise number of results pages. +There are quite a few pagination interfaces you might want to implement in your application. Many common UI patterns have a page selector allowing users to jump to any search results page. To create a page selector, you must know the exact number of total results so you can calculate the precise number of result pages. For performance reasons, however, Meilisearch cannot provide the exact number of results for a query. Instead, When using the search endpoint, responses contain an `estimatedTotalHits` field. As its name indicates, `estimatedTotalHits` is only an estimate of how many documents match your user's query. Because of this, we do not recommend creating interfaces with page selectors. If page selection is crucial to the software you are developing, see the [last section of this page](#not-recommended-page-selection) for tips that might help you work around Meilisearch's current limitations. -We recommend creating pagination interfaces centered around [previous and next buttons](#recommended-previous-and-next-buttons). +Many other pagination UIs are fully compatible with Meilisearch, such as infinite scrolling and buttons that manually load more results on click. For an experience similar to page selection, we recommend creating pagination interfaces centered around [previous and next buttons](#recommended-previous-and-next-buttons). ## Recommended: Previous and next buttons @@ -28,61 +28,106 @@ Though this approach offers less precision than a full-blown page selector, it d Previous and next buttons can be implemented using the [`limit`](/reference/api/search.md#limit) and [`offset`](/reference/api/search.md#offset) search parameters. -`limit` sets the size of page. If you set `limit` to 20, Meilisearch's response will contain a maximum of 20 search results. `offset` skips a number of search results. If you set `offset` to 40, Meilisearch's response will skip the first 40 search results. +`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. -You can use both parameters together to effectively create search pages. +For example, you can use Meilisearch's JavaScript SDK to get the first ten films in a movies database: + +```js +const results = await index.search("tarkovsky", { limit: 10, offset: 0 }); +``` +You can use both parameters together to effectively create search pages. #### Search pages and calculating `offset` -If you set `limit` to 20 and `offset` to 0, you get the first twenty results. We can call this our first page. +If you set `limit` to 20 and `offset` to 0, you get the first twenty search results. We can call this our first page. ```js -const results = await index.search("x", { limit: 20, offset: 0 }); +const results = await index.search("tarkovsky", { limit: 20, offset: 0 }); ``` Likewise, if you set `limit` to 20 and `offset` to 40, you skip the first 40 search results and get documents ranked from 41 through 60. We can call this the third results page. ```js -const results = await index.search("x", { limit: 20, offset: 40 }); +const results = await index.search("tarkovsky", { limit: 20, offset: 40 }); ``` -Use this formula to quickly calculate a page's offset value: `offset = limit * (page number - 1)`. +Use this formula to quickly calculate a page's offset value: `offset = limit * (target page number - 1)`. Once a query returns fewer `hits` than your configured `limit`, you have reached the last results page. +#### Keeping track of the current page number + +Even though this UI pattern does not allow users to jump to a specific page, it is still useful to keep track of the current page number. + +The following JavaScript snippet stores the page number in an HTML element, `.pagination`, and updates it every time the user moves to a different search results page: + +```js +function updatePageNumber(elem) { + const directionBtn = elem.id + // Get the page number stored in the pagination element + let pageNumber = parseInt(document.querySelector('.pagination').dataset.pageNumber) + + // Update page number + if (directionBtn === 'previous_button') { + pageNumber = pageNumber - 1 + } else if (directionBtn === 'next_button') { + pageNumber = pageNumber + 1 + } + + // Store new page number in the pagination element + document.querySelector('.pagination').dataset.pageNumber = pageNumber +} + +// Add data to our HTML element stating the user is on the first page +document.querySelector('.pagination').dataset.pageNumber = 0 +// Each time a user clicks on the previous or next buttons, update the page number +document.querySelector('#previous_button').onclick = function () { updatePageNumber(this) } +document.querySelector('#next_button').onclick = function () { updatePageNumber(this) } +``` + #### Disabling navigation buttons for first and last pages It is often helpful to disable navigation buttons when the user cannot move to the "next" or "previous" page. The "previous" button should be disabled whenever your `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: +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. -```js -const results = await index.search("x", { limit: 21, offset: 0 }) - -// If offset equals 0, we're on the first results page -if (offset === 0 ) { - document.querySelector("#previous_button").disabled = true; -} +The following JavaScript snippet runs checks whether we should disable a button every time the user navigates to another search results page: -// If offset is bigger than 0, we're not on the first results page -if (offset > 0 ) { - document.querySelector("#previous_button").disabled = false; +```js +function updatePageNumber() { + const pageNumber = parseInt(document.querySelector('.pagination').dataset.pageNumber) + + const offset = pageNumber * 20 + const results = await index.search('x', { limit: 21, offset }) + + // If offset equals 0, we're on the first results page + if (offset === 0 ) { + document.querySelector('#previous_button').disabled = true; + } + + // If offset is bigger than 0, we're not on the first results page + if (offset > 0 ) { + document.querySelector('#previous_button').disabled = false; + } + + // If Meilisearch returns 20 items or less, + // we are on the last page + if (results.hits.length < 21 ) { + document.querySelector('#next_button').disabled = true; + } + + // If Meilisearch returns exactly 21 results + // and our page can only show 20 items at a time, + // we have at least one more page with one result in it + if (results.hits.length === 21 ) { + document.querySelector('#next_button').disabled = false; + } } -// If Meilisearch returns 20 items or less, -// we are on the last page -if (results.hits.length < 21 ) { - document.querySelector("#next_button").disabled = true; -} - -// If Meilisearch returns exactly 21 results -// and our page can only show 20 items at a time, -// we have at least one more page with one result in it -if (results.hits.length === 21 ) { - document.querySelector("#next_button").disabled = false; -} +document.querySelector('#previous_button').onclick = function () { updatePageNumber(this) } +document.querySelector('#next_button').onclick = function () { updatePageNumber(this) } ``` ## Not recommended: Page selection @@ -109,7 +154,58 @@ By default, a search request returns 20 search results. You can change this valu For example, if you set `limit` to 300, every search request made to Meilisearch returns at most 300 documents. If a query returns a `hits` array with 200 items and you want each page to display 20 results, you can create a page selector with 10 pages. -// perhaps a code sample here? +In the following JavaScript snippet, each time a user searches, we make a new query with `limit` set to 300. Once we receive the search results, we store them in a variable and create a list of numbered pages. When users click on any number in the page list, we display a new page: + +```js +// Retrieve search results and create the page selector +function getSearchResults(searchTerm) { + // The amount of results we want to display in each page + const hitsPerPage = 10 + + // The maximum amount of results we will display + const { hits } = await index.search(searchTerm, { limit: 300 }) + + // Clear the previous buttons… + const pagination = document.querySelector('.pagination') + pagination.innerHTML = '' + + // …and create a new button for each search results page + for (let page = 0; page < hits.length; page += hitsPerPage) { + const numberBtn = document.createElement('button'); + numberBtn.innerText = page / hitsPerPage + + // When the user clicks on a page number, show that results page + numberBtn.onclick = function () { + const pageNumber = parseInt(this.innerText) + changePage(pageNumber, hits, hitsPerPage) + } + + pagination.appendChild(numberBtn) + } + + // Display first search results page + changePage(1, hits, hitsPerPage) +} + +// Display a page of search results +function changePage(pageNumber, hits, hitsPerPage) { + const offset = (pageNumber - 1) * hitsPerPage + const paginatedHits = hits.slice(offset, offset + hitsPerPage) + const resultList = document.querySelector('.results') + + resultList.innerHTML = '' + + paginatedHits.map(hit => { + const resultItem = document.createElement('div'); + resultItem.innerText = hit.title + resultList.appendChild(resultItem) + }) +} + +// Get the search bar and retrieve results every time the user makes a new query +const searchBar = document.querySelector('.searchBar') +searchBar.onChange(function () { getSearchResults('tarkovsky') }) +``` This method provides control and reliability. It also prevents users from paginating too far, which might result in performance gains. However, it limits the number of results your users can see. Additionally, we recommend caution when setting high values for `limit` as it can negatively impact performance. From 1693adaf93dc36e584c09cb966ba64958c9606c8 Mon Sep 17 00:00:00 2001 From: gui machiavelli Date: Wed, 6 Jul 2022 15:42:00 +0200 Subject: [PATCH 09/11] Apply suggestions from code review Co-authored-by: Maryam <90181761+maryamsulemani97@users.noreply.github.com> --- learn/advanced/pagination.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/learn/advanced/pagination.md b/learn/advanced/pagination.md index 2dc3a42524..6defdf7338 100644 --- a/learn/advanced/pagination.md +++ b/learn/advanced/pagination.md @@ -8,7 +8,7 @@ In this guide, we will discuss some of Meilisearch's current limitations, how th There are quite a few pagination interfaces you might want to implement in your application. Many common UI patterns have a page selector allowing users to jump to any search results page. To create a page selector, you must know the exact number of total results so you can calculate the precise number of result pages. -For performance reasons, however, Meilisearch cannot provide the exact number of results for a query. Instead, When using the search endpoint, responses contain an `estimatedTotalHits` field. As its name indicates, `estimatedTotalHits` is only an estimate of how many documents match your user's query. +For performance reasons, however, Meilisearch cannot provide the exact number of results for a query. Instead, when using the [search endpoint](/reference/api/search.md), responses contain an `estimatedTotalHits` field. As its name indicates, `estimatedTotalHits` is only an estimate of how many documents match your user's query. Because of this, we do not recommend creating interfaces with page selectors. If page selection is crucial to the software you are developing, see the [last section of this page](#not-recommended-page-selection) for tips that might help you work around Meilisearch's current limitations. @@ -28,7 +28,7 @@ Though this approach offers less precision than a full-blown page selector, it d Previous and next buttons can be implemented using the [`limit`](/reference/api/search.md#limit) and [`offset`](/reference/api/search.md#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. +`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: @@ -37,15 +37,16 @@ const results = await index.search("tarkovsky", { limit: 10, offset: 0 }); ``` You can use both parameters together to effectively create search pages. + #### Search pages and calculating `offset` -If you set `limit` to 20 and `offset` to 0, you get the first twenty search results. We can call this our first page. +If you set `limit` to `20` and `offset` to `0`, you get the first twenty search results. We can call this our first page. ```js const results = await index.search("tarkovsky", { limit: 20, offset: 0 }); ``` -Likewise, if you set `limit` to 20 and `offset` to 40, you skip the first 40 search results and get documents ranked from 41 through 60. We can call this the third results page. +Likewise, if you set `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. ```js const results = await index.search("tarkovsky", { limit: 20, offset: 40 }); @@ -89,9 +90,9 @@ document.querySelector('#next_button').onclick = function () { updatePageNumber( It is often helpful to disable navigation buttons when the user cannot move to the "next" or "previous" page. -The "previous" button should be disabled whenever your `offset` is 0, as this indicates your user is on the first results page. +The "Previous" button should be disabled whenever your `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. +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: @@ -152,9 +153,9 @@ We recommend two different workarounds to create this kind of pagination interfa By default, a search request returns 20 search results. You can change this value to a much higher number and treat it as the effective maximum of search results you will show a user. Doing so means the size of your `hits` array is the exact number of search results you have to paginate. -For example, if you set `limit` to 300, every search request made to Meilisearch returns at most 300 documents. If a query returns a `hits` array with 200 items and you want each page to display 20 results, you can create a page selector with 10 pages. +For example, if you set `limit` to `300`, every search request made to Meilisearch returns at most 300 documents. If a query returns a `hits` array with 200 items and you want each page to display 20 results, you can create a page selector with 10 pages. -In the following JavaScript snippet, each time a user searches, we make a new query with `limit` set to 300. Once we receive the search results, we store them in a variable and create a list of numbered pages. When users click on any number in the page list, we display a new page: +In the following JavaScript snippet, each time a user searches, we make a new query with `limit` set to `300`. Once we receive the search results, we store them in a variable and create a list of numbered pages. When users click on any number in the page list, we display a new page: ```js // Retrieve search results and create the page selector From 7b8fbc741e88cd1cd091953d589322f95a9108e6 Mon Sep 17 00:00:00 2001 From: gui machiavelli Date: Wed, 6 Jul 2022 15:44:10 +0200 Subject: [PATCH 10/11] Apply suggestions from code review Co-authored-by: Maryam <90181761+maryamsulemani97@users.noreply.github.com> --- learn/advanced/pagination.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/learn/advanced/pagination.md b/learn/advanced/pagination.md index 6defdf7338..a8d7354594 100644 --- a/learn/advanced/pagination.md +++ b/learn/advanced/pagination.md @@ -224,4 +224,4 @@ This method gives users access to all search results. However, it also results i ## Unsatisfied? Let us know -Is the current state of pagination in Meilisearch negatively impacting you? Please share your thoughts with us in this [GitHub discussion](https://github.com/meilisearch/product/discussions/483). We are actively working on improving this aspect of Meilisearch and your input is greatly appreciated. +Is the current state of pagination in Meilisearch negatively impacting you? Please share your thoughts with us in this [GitHub discussion](https://github.com/meilisearch/product/discussions/483). We are actively working on improving this aspect of Meilisearch, and your input is greatly appreciated. From a12ddd348cf1477f67300954adb82bde59363629 Mon Sep 17 00:00:00 2001 From: gui machiavelli Date: Wed, 6 Jul 2022 16:08:10 +0200 Subject: [PATCH 11/11] address review feedback --- .vuepress/config.js | 1 + learn/advanced/pagination.md | 14 +++++--------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/.vuepress/config.js b/.vuepress/config.js index 395bd548d8..95f02aa241 100644 --- a/.vuepress/config.js +++ b/.vuepress/config.js @@ -174,6 +174,7 @@ module.exports = { '/learn/advanced/asynchronous_operations', '/learn/advanced/filtering_and_faceted_search', '/learn/advanced/geosearch', + '/learn/advanced/pagination', '/learn/advanced/sorting', { title: 'Updating Meilisearch', diff --git a/learn/advanced/pagination.md b/learn/advanced/pagination.md index a8d7354594..9decfb54c8 100644 --- a/learn/advanced/pagination.md +++ b/learn/advanced/pagination.md @@ -14,12 +14,10 @@ Because of this, we do not recommend creating interfaces with page selectors. If Many other pagination UIs are fully compatible with Meilisearch, such as infinite scrolling and buttons that manually load more results on click. For an experience similar to page selection, we recommend creating pagination interfaces centered around [previous and next buttons](#recommended-previous-and-next-buttons). -## Recommended: Previous and next buttons +## Recommended: "Previous" and "Next" buttons Using previous and next buttons for pagination means that users can easily navigate through results, but don't have the ability to jump to an arbitrary results page. -![Placeholder image for prev/next interface]() - Though this approach offers less precision than a full-blown page selector, it does not require knowing the precise number of search results. This makes it a good fit for Meilisearch's current capabilities. ### Implementation @@ -52,7 +50,7 @@ Likewise, if you set `limit` to `20` and `offset` to `40`, you skip the first 40 const results = await index.search("tarkovsky", { limit: 20, offset: 40 }); ``` -Use this formula to quickly calculate a page's offset value: `offset = limit * (target page number - 1)`. +You can use this formula to calculate a page's offset value: `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. @@ -135,13 +133,11 @@ document.querySelector('#next_button').onclick = function () { updatePageNumber( This type of pagination consists of a numbered list of pages accompanied by next and previous buttons. -![placeholder page selection UI](https://vuejsexamples.com/content/images/2018/11/vue-pagination.gif) - This is a common UI pattern that offers users a significant amount of precision when navigating results. However, due to Meilisearch's [limitations](#choosing-the-right-pagination-ui), it is not a good fit for pagination with Meilisearch. ### Implementation -The general implementation of a page selection interface is similar to our [recommended solution](#recommended-previous-and-next-buttons), but also includes a numbered page list. +The general implementation of a page selection interface is similar to our [recommended solution](#recommended-previous-and-next-buttons), with one signficant addition: it includes a numbered page list. To create a numbered page list, however, you must know the exact number of total results. For example, if you have 100 results and your search result pages contain 10 results each, your selector must show a list of numbers going from 1 to 10. @@ -218,10 +214,10 @@ By default, Meilisearch returns a maximum of 1000 search results. Consult our [i Though we advise against it, you can use `estimatedTotalHits` to calculate the number of search result pages. This means your number of results and page count are likely to change until Meilisearch retrieves the last search result. -For example, a query's `estimatedTotalHits` might be `100` when you fetch the first page of search results. If you are showing 20 results per page, this means your interface will display a page selector with 5 pages. When you fetch the fifth and last page according to Meilisearch's current estimate, however, `estimatedTotalHits` might change to `300`. Your page list, previously displaying 5 pages, must now show 10 total pages. +For example, a query's `estimatedTotalHits` might be `100` when you fetch the first page of search results. If you are showing 20 results per page, this means your interface will display a page selector with 5 pages. When you fetch the fifth and last page according to Meilisearch's current estimate, however, `estimatedTotalHits` might change to `300`. Your page list, previously displaying 5 pages, must now show 15 total pages. This method gives users access to all search results. However, it also results in an interface that might feel unreliable due to constant and unpredictable changes. ## Unsatisfied? Let us know -Is the current state of pagination in Meilisearch negatively impacting you? Please share your thoughts with us in this [GitHub discussion](https://github.com/meilisearch/product/discussions/483). We are actively working on improving this aspect of Meilisearch, and your input is greatly appreciated. +Is the current state of pagination in Meilisearch creating problems for you? Please share your thoughts with us in this [GitHub discussion](https://github.com/meilisearch/product/discussions/483). We are actively working on improving this aspect of Meilisearch and your input is greatly appreciated.