# How to use pagination per batch

**URL:** <https://discuss.elastic.co/t/how-to-use-pagination-per-batch/216940>\
**Category:** Elasticsearch\
**Created:** [January 29, 2020, 4:45am UTC](https://discuss.elastic.co/t/how-to-use-pagination-per-batch/216940 "2020-01-29T04:45:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Toshif](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@Toshif](https://discuss.elastic.co/u/Toshif)\
**Post date:** [January 29, 2020, 4:45am UTC](https://discuss.elastic.co/t/how-to-use-pagination-per-batch/216940/1 "2020-01-29T04:45:29Z")

</div>

Hello people,  
My query is retrieving 10000 records from elasticSearch in 1 hit.  
I used pagination (from & size) but its giving me only specified records.  
I want 10000 records but in a batch size of 50.  
(I am not able to understand how scroll is used with from & size.)  
How can we accomplish that one ?

---

<div class="post-metadata">

**Author:** ![Magnus\_Kessler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnus_kessler/32/42001_2.png) [@Magnus\_Kessler](https://discuss.elastic.co/u/Magnus_Kessler)\
**Post date:** [January 29, 2020, 6:46am UTC](https://discuss.elastic.co/t/how-to-use-pagination-per-batch/216940/2 "2020-01-29T06:46:01Z")

</div>

When you retrieve documents through [scroll](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-scroll), each new invocation retrieves the next 'page' of data. You cannot specify a `from` or `to` parameter. Scroll allows to retrieve any number of documents and is typically used to export data from an index. It should not be used for casual retrieval of a few pages and not be the standard pagination mechanism for the display of interactive pages. Each time a scroll is used, the affected shards are not allowed to perform background merges until the scroll has been released.

Pagination with `from` and `to` on the other hand should only be used to retrieve the first few pages of a data set (e.g. up to maybe 10 pages of size 50), as pagination becomes very expensive with increasing depth.

You may also want to investigate if [search after](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-search-after) suits the needs of the application. This mechanism is a lot less taxing on the index than the scroll mechanism and can be used to retrieve larger amounts of data in an interactive setup.

---

<div class="post-metadata">

**Author:** ![Toshif](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@Toshif](https://discuss.elastic.co/u/Toshif)\
**Post date:** [January 29, 2020, 7:17am UTC](https://discuss.elastic.co/t/how-to-use-pagination-per-batch/216940/3 "2020-01-29T07:17:09Z")

</div>

Yes it will give the records as per the size in 1st hit, but what about other records ?  
And how do we get number of records retrieved from the response ?  
Below is my search response :  
SearchResponse response = highLevelRestClient.search(searchRequest, RequestOptions.DEFAULT);

---

<div class="post-metadata">

**Author:** ![Magnus\_Kessler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnus_kessler/32/42001_2.png) [@Magnus\_Kessler](https://discuss.elastic.co/u/Magnus_Kessler)\
**Post date:** [January 29, 2020, 8:25am UTC](https://discuss.elastic.co/t/how-to-use-pagination-per-batch/216940/4 "2020-01-29T08:25:31Z")

</div>

When you use the scroll mechanism, the first request returns a `scroll_id` in the response. You pass this value on the next request back to Elasticsearch for the next slice of data. This will return another scroll\_id.

---

<div class="post-metadata">

**Author:** ![Toshif](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@Toshif](https://discuss.elastic.co/u/Toshif)\
**Post date:** [January 29, 2020, 8:43am UTC](https://discuss.elastic.co/t/how-to-use-pagination-per-batch/216940/5 "2020-01-29T08:43:56Z")

</div>

Got it. Thanx

---

<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:** [February 26, 2020, 8:53am UTC](https://discuss.elastic.co/t/how-to-use-pagination-per-batch/216940/6 "2020-02-26T08:53:24Z")

</div>

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