# How to correctly use \`search\_after\` for huge amount of records (100k+)?

**URL:** <https://discuss.elastic.co/t/how-to-correctly-use-search-after-for-huge-amount-of-records-100k/348426>\
**Category:** Elasticsearch\
**Created:** [December 1, 2023, 11:03am UTC](https://discuss.elastic.co/t/how-to-correctly-use-search-after-for-huge-amount-of-records-100k/348426 "2023-12-01T11:03:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarinTakanov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marintakanov/32/121778_2.png) [@MarinTakanov](https://discuss.elastic.co/u/MarinTakanov)\
**Post date:** [December 1, 2023, 11:03am UTC](https://discuss.elastic.co/t/how-to-correctly-use-search-after-for-huge-amount-of-records-100k/348426/1 "2023-12-01T11:03:59Z")

</div>

Can someone explain how to use `search_after` for more than 100k records without fetching 10k records just to get the `sort` value of the last record just to get the next 10k records?

Here's an example:  
I have 100 100 records. I want to get the last 100 records. I have `index.max_result_window` set to 10 000 (default value).

1. How to fetch the last 100 records, without fetching the first 10k and the the next 10k, etc. using the `sort` value of the last record of each 10k records and placing it into the `search_after` property just to fetch the next 10k records which I wont use?
2. How to fetch 100 records in the middle (e.g. 86 700 - 86 800) without fetching 10k records at a time (which again, is waste of time and resources)?
3. If I have the last 100 records, how to get the previous 100 records (from 90 900 to 100 000) and then the previous 100, etc.?

My real case is that I have a table with pagination. The user can adjust the page number and can jump between pages from the available page buttons (10 at a time). It can also jump to last, first and random page. The issues are: (1) if the user click the last page button, (2) and then click the previous page button, (3) then click a page button that is in the previous 10k records threshold, but not necessarily the previous page of the current page, (4) go to a random page.

---

<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:** [December 29, 2023, 11:04am UTC](https://discuss.elastic.co/t/how-to-correctly-use-search-after-for-huge-amount-of-records-100k/348426/2 "2023-12-29T11:04:43Z")

</div>

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