# How to support pagination in aggregation

**URL:** <https://discuss.elastic.co/t/how-to-support-pagination-in-aggregation/251413>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-security\
**Created:** [October 8, 2020, 10:20am UTC](https://discuss.elastic.co/t/how-to-support-pagination-in-aggregation/251413 "2020-10-08T10:20:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raj3](https://avatars.discourse-cdn.com/v4/letter/r/59ef9b/32.png) [@Raj3](https://discuss.elastic.co/u/Raj3)\
**Post date:** [October 8, 2020, 10:20am UTC](https://discuss.elastic.co/t/how-to-support-pagination-in-aggregation/251413/1 "2020-10-08T10:20:52Z")

</div>

{  
"aggs": {  
"thread": {  
"terms": {  
"field": "session\_id"  
}  
}  
}  
}

i want to add pagination in field session\_id how can it?

---

<div class="post-metadata">

**Author:** ![Hendrik\_Muhs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_muhs/32/25802_2.png) [@Hendrik\_Muhs](https://discuss.elastic.co/u/Hendrik_Muhs)\
**Post date:** [October 8, 2020, 10:47am UTC](https://discuss.elastic.co/t/how-to-support-pagination-in-aggregation/251413/2 "2020-10-08T10:47:45Z")

</div>

Please have a look at [composite aggregations](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html), aggs are limited to 1 request, with composite you can page agg results.

---

<div class="post-metadata">

**Author:** ![Raj3](https://avatars.discourse-cdn.com/v4/letter/r/59ef9b/32.png) [@Raj3](https://discuss.elastic.co/u/Raj3)\
**Post date:** [October 8, 2020, 1:53pm UTC](https://discuss.elastic.co/t/how-to-support-pagination-in-aggregation/251413/3 "2020-10-08T13:53:54Z")

</div>

Can you give me a example? and please also give me solution if any possibility for adding limit inside the aggregation.

---

<div class="post-metadata">

**Author:** ![Hendrik\_Muhs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_muhs/32/25802_2.png) [@Hendrik\_Muhs](https://discuss.elastic.co/u/Hendrik_Muhs)\
**Post date:** [October 8, 2020, 2:02pm UTC](https://discuss.elastic.co/t/how-to-support-pagination-in-aggregation/251413/4 "2020-10-08T14:02:12Z")

</div>

Please have a look at the provided link, it has tons of examples. If you have a specific question that is not answered in the docs, I happily help.

I do not understand what you mean with "adding limit inside aggregation", best if you can give an example and/or elaborate on what you want to do.

---

<div class="post-metadata">

**Author:** ![Raj3](https://avatars.discourse-cdn.com/v4/letter/r/59ef9b/32.png) [@Raj3](https://discuss.elastic.co/u/Raj3)\
**Post date:** [October 9, 2020, 3:52am UTC](https://discuss.elastic.co/t/how-to-support-pagination-in-aggregation/251413/5 "2020-10-09T03:52:48Z")

</div>

{  
"size": "0",  
"query": {  
"match\_all": {}  
}  
"aggs": {  
"analytics": {  
"filter": {  
"term": {  
"event\_type": "analytics"  
}  
},  
"aggs": {  
"thread": {  
"terms": {  
"field": "session\_id",  
},  
"aggs": {  
"tops": {  
"top\_hits": {  
"sort": [  
{  
"created\_at": {  
"order": "desc"  
}  
}  
],  
"size": 1  
}  
}  
}  
}  
}  
}  
}

Here in my above example i filter by event\_type(analytics) and group by session\_id. sort according to created\_at. so in above example i get the latest information of each session. like if we have 50 session then according to above my example it gives latest information of 50 session.

but, my target is support pagination in session like i pass 1 to 10 value then it gives only latest information of 1 to 10 session. if i pass the value 11 to 20 then it gives only latest information of 11 to 20 session.

---

<div class="post-metadata">

**Author:** ![Hendrik\_Muhs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_muhs/32/25802_2.png) [@Hendrik\_Muhs](https://discuss.elastic.co/u/Hendrik_Muhs)\
**Post date:** [October 9, 2020, 11:16am UTC](https://discuss.elastic.co/t/how-to-support-pagination-in-aggregation/251413/6 "2020-10-09T11:16:09Z")

</div>

How many sessions in total do you expect?  
When you say sessions 1 to 10, 11 to 20, ... What order do you expect? Note that new incoming data might change the order between 2 search requests.

Depending on the size you have several options.

---

<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 6, 2020, 11:16am UTC](https://discuss.elastic.co/t/how-to-support-pagination-in-aggregation/251413/7 "2020-11-06T11:16:20Z")

</div>

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