# Random access pagination with search\_after on Elasticsearch

**URL:** <https://discuss.elastic.co/t/random-access-pagination-with-search-after-on-elasticsearch/346203>\
**Category:** Elasticsearch\
**Created:** [November 1, 2023, 1:37pm UTC](https://discuss.elastic.co/t/random-access-pagination-with-search-after-on-elasticsearch/346203 "2023-11-01T13:37:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cerenimo](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@cerenimo](https://discuss.elastic.co/u/cerenimo)\
**Post date:** [November 1, 2023, 1:37pm UTC](https://discuss.elastic.co/t/random-access-pagination-with-search-after-on-elasticsearch/346203/1 "2023-11-01T13:37:31Z")

</div>

There are more than 10 thousand documents in my index, but I cannot access all documents with search. I may also have performance problems with the scroll API. I found a method on how to overcome this with search\_after in this [article](https://dev.to/tariqabughofa/3-approaches-to-scroll-through-data-in-elasticsearch-4ii3#:%7E:text=Random%20access%20with%20search_after).

With this article, I can list all documents without using function score, but it seems that pagination cannot be done using a query. Is there a correct way to search and page through all content?

I want to see the documents by filtering them according to some conditions, but while there should be more than 10k results as a result of filtering and search API, there is max 10k documents. While navigating between pages, it is necessary to be able to move to the 7th page while on the 3rd page. At this point, search\_after or scroll are not useful.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 1, 2023, 9:05pm UTC](https://discuss.elastic.co/t/random-access-pagination-with-search-after-on-elasticsearch/346203/2 "2023-11-01T21:05:32Z")

</div>

Welcome

> [@cerenimo](#):
>
> there is max 10k documents

Have a look at [The search API | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-your-data.html#track-total-hits)

About deep pagination, you can use:

- the [`size` and `from` parameters](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html) to display by default up to 10000 records to your users. If you want to change this limit, you can change `index.max_result_window` setting but be aware of the consequences (ie memory).
- the [search after](https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#search-after) feature to do deep pagination.
- the [Scroll API](https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#scroll-search-results) if you want to extract a resultset to be consumed by another tool later. (Not recommended anymore)

Note that from a end user perspective, going randomly to page 1076 (for example) does not make really sense IMO.  
What is your use case?

---

<div class="post-metadata">

**Author:** ![cerenimo](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@cerenimo](https://discuss.elastic.co/u/cerenimo)\
**Post date:** [November 2, 2023, 6:56am UTC](https://discuss.elastic.co/t/random-access-pagination-with-search-after-on-elasticsearch/346203/3 "2023-11-02T06:56:01Z")

</div>

Thanks for your response.  
There are more than 100 thousand documents that must come page by page to have a good user experience. That's why I don't want to use Infinite Scroll. It isn't very likely from the end user perspective, but I still think it would be healthier in terms of UX. I cannot transition the pages as I want with the search\_after option in your second suggestion, I have tried it before. I will try your first suggestion, but I think we cannot understand what its effects will be in terms of performance.  
Thanks again for your response.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 2, 2023, 7:11am UTC](https://discuss.elastic.co/t/random-access-pagination-with-search-after-on-elasticsearch/346203/4 "2023-11-02T07:11:16Z")

</div>

But what will make the user think that going to page XYZ will help him to find what he wants?

When you are using Google for example, how many times are you going to page XYZ? I bet almost never.  
Because you are expecting to have the most relevant information on the first page.

That's the same idea with Elasticsearch.

What kind of data are you indexing? What is the use case?

---

<div class="post-metadata">

**Author:** ![cerenimo](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@cerenimo](https://discuss.elastic.co/u/cerenimo)\
**Post date:** [November 2, 2023, 9:22am UTC](https://discuss.elastic.co/t/random-access-pagination-with-search-after-on-elasticsearch/346203/5 "2023-11-02T09:22:52Z")

</div>

Our customers expect us to list all the content on their platforms because they will do actions such as reviewing, updating, and deleting the content one by one.

They want all content to be accessible and I want to offer them a free environment.. As you said, it is a very low probability, but I am trying to move towards expectations.

When searching on Google, I will likely find what I am looking for on the first page, and in my project, they will want to browse through the pages freely and make reviews.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 2, 2023, 9:45am UTC](https://discuss.elastic.co/t/random-access-pagination-with-search-after-on-elasticsearch/346203/6 "2023-11-02T09:45:57Z")

</div>

I see...

I think that this usage has a human limit. May be you can just increase the `index.max_result_window` setting to 100000?

---

<div class="post-metadata">

**Author:** ![cerenimo](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@cerenimo](https://discuss.elastic.co/u/cerenimo)\
**Post date:** [November 2, 2023, 11:51am UTC](https://discuss.elastic.co/t/random-access-pagination-with-search-after-on-elasticsearch/346203/7 "2023-11-02T11:51:18Z")

</div>

Thank you, I will try.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 30, 2023, 11:51am UTC](https://discuss.elastic.co/t/random-access-pagination-with-search-after-on-elasticsearch/346203/8 "2023-11-30T11:51:58Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
