# Required Paging in aggregation

**URL:** <https://forum.opensearch.org/t/required-paging-in-aggregation/12582>\
**Category:** OpenSearch\
**Tags:** discuss\
**Created:** [February 20, 2023, 1:09pm UTC](https://forum.opensearch.org/t/required-paging-in-aggregation/12582 "2023-02-20T13:09:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gupta10900](https://avatars.discourse-cdn.com/v4/letter/g/59ef9b/32.png) [@gupta10900](https://forum.opensearch.org/u/gupta10900)\
**Post date:** [February 20, 2023, 1:09pm UTC](https://forum.opensearch.org/t/required-paging-in-aggregation/12582/1 "2023-02-20T13:09:09Z")

</div>

**Versions** (relevant - OpenSearch/Dashboard/Server OS/Browser):

AWS OpenSearch 2.3

**Describe the issue** :

After filtering the data from index count of doc is 220000. There are distinct 99000 values when I apply aggregation on that. but Query return only 65535 rows as max.

“size”: 2,  
“query”: {  
“bool”: {  
“must”: [  
{“term”: {“Hits.AdminId”: “\*\*\*\*” }},  
{“match\_phrase”: {“Hits.DistributionQueueID”: “00000000-0000-0000-0000-000000000000” }},  
{“match\_phrase”: {“Hits.SourceType.keyword”: “test” }}  
]  
}  
},“aggs”: {  
“data”: {  
“terms”: { “field”: “Hits.Recipient.keyword”,“size”: 65536}  
}  
}

I want data with the help of pagination of 1000 record.

**Configuration** :

**Relevant Logs or Screenshots** :

---

<div class="post-metadata">

**Author:** ![pdz](https://avatars.discourse-cdn.com/v4/letter/p/51bf81/32.png) [@pdz](https://forum.opensearch.org/u/pdz)\
**Post date:** [February 20, 2023, 1:54pm UTC](https://forum.opensearch.org/t/required-paging-in-aggregation/12582/2 "2023-02-20T13:54:43Z")

</div>

You can do this with Composite Aggregation:

```auto
  "size": 0,
  "aggs": {
    "my_buckets": {
      "composite": {
        "size": 1000,
        "sources": [
          {
            "data": {
              "terms": { "field": "Hits.Recipient.keyword" }
            }
          }
        ]
      }
    }
  }

```

You will get non-null “after\_key” in response, so you have to use that in “after” param in next call:

```auto
  "size": 0,
  "aggs": {
    "my_buckets": {
      "composite": {
        "size": 1000,
        "after": { ... },
        "sources": [
          {
            "data": {
              "terms": { "field": "Hits.Recipient.keyword" }
            }
          }
        ]
      }
    }
  }

```

---

<div class="post-metadata">

**Author:** ![gupta10900](https://avatars.discourse-cdn.com/v4/letter/g/59ef9b/32.png) [@gupta10900](https://forum.opensearch.org/u/gupta10900)\
**Post date:** [February 21, 2023, 9:13am UTC](https://forum.opensearch.org/t/required-paging-in-aggregation/12582/3 "2023-02-21T09:13:48Z")

</div>

Thanks for the Solution.

But one more query is there any way to get data with limit and offset.

In my case, I am calling this data from Core API and return to the application. In application call implemented on limit and offset.

---

<div class="post-metadata">

**Author:** ![pdz](https://avatars.discourse-cdn.com/v4/letter/p/51bf81/32.png) [@pdz](https://forum.opensearch.org/u/pdz)\
**Post date:** [February 21, 2023, 10:05am UTC](https://forum.opensearch.org/t/required-paging-in-aggregation/12582/4 "2023-02-21T10:05:35Z")

</div>

No, I don’t think so

---

<div class="post-metadata">

**Author:** ![mkhl](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@mkhl](https://forum.opensearch.org/u/mkhl)\
**Post date:** [July 5, 2025, 7:47am UTC](https://forum.opensearch.org/t/required-paging-in-aggregation/12582/5 "2025-07-05T07:47:40Z")

</div>

> [@gupta10900](#):
>
> But one more query is there any way to get data with limit and offset.

Hi.  
May you confirm that “after\_key” is absent if the last bucket returned? From what I see it’s always present in response even it’s not really needed at the last page. WDYT?
