# ES pagination under the hood

**URL:** <https://discuss.elastic.co/t/es-pagination-under-the-hood/329679>\
**Category:** Elasticsearch\
**Created:** [April 10, 2023, 5:32pm UTC](https://discuss.elastic.co/t/es-pagination-under-the-hood/329679 "2023-04-10T17:32:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eslearner58](https://avatars.discourse-cdn.com/v4/letter/e/77aa72/32.png) [@eslearner58](https://discuss.elastic.co/u/eslearner58)\
**Post date:** [April 10, 2023, 5:32pm UTC](https://discuss.elastic.co/t/es-pagination-under-the-hood/329679/1 "2023-04-10T17:32:06Z")

</div>

I am trying to implement pagination for quite heavy ES queries so I did some research on the options available, but documentation didn't answer some of my questions:

1. [search\_after](https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#search-after) is the recommended way for doing the deep pagination because, unlike the offset style pagination, it doesn't require to load all the previous pages in memory and allows to resume the data from the cursor position (assuming no new documents appear). But how are this cursor and sorted query results being stored in ES internally? Should I be concerned with high inodes consumption if I'm using search\_after with the queries that yield millions of hits (even if I only paginate through hundreds of them)? what about memory usage?

2. Since search\_after only allows to paginate in one direction, I am thinking of a workaround for going back: reverse the sort order and use the first entry's sort values to go to a previous page. What are the performance implications of reversing the query sort order like this? How does it change the resource consumption by ES?

I have found the official ES documentation to be quite terse when it comes to "how things actually work" so I will appreciate any deep dive / explanations / references here for a more solid understanding.

Thank you!

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [April 21, 2023, 12:17am UTC](https://discuss.elastic.co/t/es-pagination-under-the-hood/329679/2 "2023-04-21T00:17:07Z")

</div>

Welcome to our community! 😃

I think part of this comes down to this section on the docs page you linked to and which I have bolded;

> Repeat this process by updating the `search_after` array every time you retrieve a new page of results. **If a [refresh](https://www.elastic.co/guide/en/elasticsearch/reference/current/near-real-time.html) occurs between these requests, the order of your results may change, causing inconsistent results across pages.** To prevent this, you can create a [point in time (PIT)](https://www.elastic.co/guide/en/elasticsearch/reference/current/point-in-time-api.html) to preserve the current index state over your searches.

So it doesn't save the sort order for you, it re-does it every time. You might want to consider using a [point-in-time] [Point in time API | Elasticsearch Guide [8.7] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.7/point-in-time-api.html)) request?

That said I know it probablyt doesn't answer the underlying question of how things work under the hood, but that's not an area I am super knowledgable on. I'll see if I can find someone who can comment though.

---

<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:** [May 19, 2023, 12:17am UTC](https://discuss.elastic.co/t/es-pagination-under-the-hood/329679/3 "2023-05-19T00:17:52Z")

</div>

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