# ES query for supporting pagination

**URL:** <https://discuss.elastic.co/t/es-query-for-supporting-pagination/136227>\
**Category:** Elasticsearch\
**Created:** [June 18, 2018, 12:50am UTC](https://discuss.elastic.co/t/es-query-for-supporting-pagination/136227 "2018-06-18T00:50:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vivek\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vivek_singh/32/32356_2.png) [@Vivek\_Singh](https://discuss.elastic.co/u/Vivek_Singh)\
**Post date:** [June 18, 2018, 12:50am UTC](https://discuss.elastic.co/t/es-query-for-supporting-pagination/136227/1 "2018-06-18T00:50:46Z")

</div>

I have data in ES as:

```
"hits": {
    "total": 177,
    "max_score": 1,
    "hits": [
            {
             "_id": "random_id_1",
             "_score": 1,
             "_source": {
                 "title": "Find series",
                 "description": "some random text",
                 "createdAt": 1527848716000,
                 "category": [
                     "STARS"
                 ],
                 "updatedAt": 1527848716000
               }
            },
            {
             "_id": "random_id_2",
             "_score": 1,
             "_source": {
                 "title": "Find series 2",
                 "description": "some random text 2",
                 "createdAt": 1527848716001,
                 "category": [
                     "SOLARS"
                 ],
                 "updatedAt": 1527848716001
               }
            }
         ]

```

What is wanted is to display n data from each category. For which query formed using aggregation as:

```
    {
	"size": 0,
    "aggs": {
    "summary": {
      "terms": {
        "field": "category"
      },                                                              
      "aggs": {                                                          
        "summary": {                                             
          "top_hits": {                                                  
            "sort": [                                                    
              {                                                        
                "createdAt": "desc"                                     
              }                                                        
            ],
            "size": 100
          }                                                            
        }                                                              
      }
    }
  }
}

```

But what i am unable to get is pagination. If i use from under nested aggregation, i can have pagination, but if data gets updated, from will be of no use. What i want is keyset pagination based on \_id, but am not able to figure out how to achieve the same? Any help in here. Thanks

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [June 18, 2018, 9:55am UTC](https://discuss.elastic.co/t/es-query-for-supporting-pagination/136227/2 "2018-06-18T09:55:39Z")

</div>

> [@Vivek\_Singh](#):
>
> But what i am unable to get is pagination.

Pagination of the top-level terms agg (you're only seeing the first 10 here) or of the top hits under each term?

> i can have pagination, but if data gets updated, from will be of no use.

This is always the case. Only the `scroll` api supports paging over a fixed "point-in-time" view of data while updates are being received and that API only supports paging through docs.

> What i want is keyset pagination based on \_id,

The doc \_ids? I'm not sure that makes sense as your top\_hits are sorted by date and your terms aggs are the top N terms by popularity.

Perhaps we should start with what business problem you're trying to solve?

---

<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:** [July 16, 2018, 9:55am UTC](https://discuss.elastic.co/t/es-query-for-supporting-pagination/136227/3 "2018-07-16T09:55:55Z")

</div>

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