# Elastic search pagination on top hits

**URL:** <https://discuss.elastic.co/t/elastic-search-pagination-on-top-hits/233777>\
**Category:** Elasticsearch\
**Created:** [May 21, 2020, 4:55pm UTC](https://discuss.elastic.co/t/elastic-search-pagination-on-top-hits/233777 "2020-05-21T16:55:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hariskhalique](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@hariskhalique](https://discuss.elastic.co/u/hariskhalique)\
**Post date:** [May 21, 2020, 4:55pm UTC](https://discuss.elastic.co/t/elastic-search-pagination-on-top-hits/233777/1 "2020-05-21T16:55:46Z")

</div>

Hi,  
I am looking for a way to paginate my results under "products" aggregation following is my query.

```
 {
      "size": 0,
      "aggs": {
          "sub_category": {
              "terms": {
                  "size": 10000,
                  "field": "cat_name.keyword"
              },
              "aggs": {
                  "product": {
                      "cardinality": {
                          "field": "product_id"
                      }
                  }
              }
          },
          "by_color": {
              "terms": {
                  "field": "color_title.keyword",
                  "size": 10000
              },
              "aggs": {
                  "product": {
                      "cardinality": {
                          "field": "product_id"
                      }
                  }
              }
          },
          "by_brand": {
              "terms": {
                  "field": "brand_name.keyword",
                  "size": 10000
              },
              "aggs": {
                  "product": {
                      "cardinality": {
                          "field": "product_id"
                      }
                  }
              }
          },
          "max_price_range": {
              "max": {
                  "field": "item_price"
              }
          },
          "min_price_range": {
              "min": {
                  "field": "item_price"
              }
          },
          "products": {
              "terms": {
                  "field": "product_id",
                  "size": 20
              },
              "aggs": {
                  "data": {
                      "top_hits": {
                          "size": 1,
                          "_source": {
                              "includes": []
                          }
                      }
                  }
              }
          }
      },
      "query": {
          "bool": {
              "filter": {
                  "script": {
                      "script": {
                          "source": "doc['image_src.keyword'].size()!=0",
                          "lang": "painless"
                      }
                  }
              }
          }
      }
  }

```

In my query I am doing aggregation and found total count base on color, brand etc. In a same query I am getting products as well using top hits. I like to paginate my product data but pagination should not affect total count of color, brand and etc.

I there any way to do that.

Thanks in advance

---

<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:** [June 18, 2020, 4:55pm UTC](https://discuss.elastic.co/t/elastic-search-pagination-on-top-hits/233777/2 "2020-06-18T16:55:56Z")

</div>

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