# Composite aggregations and pagination

**URL:** <https://discuss.elastic.co/t/composite-aggregations-and-pagination/240268>\
**Category:** Elastic Tips and Common Fixes\
**Tags:** elasticsearch\
**Created:** [July 8, 2020, 6:10am UTC](https://discuss.elastic.co/t/composite-aggregations-and-pagination/240268 "2020-07-08T06:10:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![xeraa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xeraa/32/48181_2.png) [@xeraa](https://discuss.elastic.co/u/xeraa)\
**Post date:** [July 8, 2020, 6:10am UTC](https://discuss.elastic.co/t/composite-aggregations-and-pagination/240268/1 "2020-07-08T06:10:33Z")

</div>

Here's an example on how to do forward and reverse pagination with a composite aggregation.

Example data, for use in Kibana's Dev Tools:

```auto
POST test/_doc
{
  "date": "2020-01-01",
  "product": "a"
}
POST test/_doc
{
  "date": "2020-01-02",
  "product": "b"
}
POST test/_doc
{
  "date": "2020-01-03",
  "product": "c"
}
POST test/_doc
{
  "date": "2020-01-04",
  "product": "d"
}
POST test/_doc
{
  "date": "2020-01-05",
  "product": "e"
}

```

Run our first query:

```auto
GET /_search
{
  "size": 0,
  "aggs": {
    "my_buckets": {
      "composite": {
        "size": 2,
        "sources": [
          {
            "date": {
              "date_histogram": {
                "field": "date",
                "calendar_interval": "1d",
                "order": "desc"
              }
            }
          },
          {
            "product": {
              "terms": {
                "field": "product.keyword",
                "order": "asc"
              }
            }
          }
        ]
      }
    }
  }
}

```

Which returns:

```auto
  "aggregations" : {
    "my_buckets" : {
      "after_key" : {
        "date" : 1578096000000,
        "product" : "d"
      },
      "buckets" : [
        {
          "key" : {
            "date" : 1578182400000,
            "product" : "e"
          },
          "doc_count" : 1
        },
        {
          "key" : {
            "date" : 1578096000000,
            "product" : "d"
          },
          "doc_count" : 1
        }
      ]
    }
  }

```

Based on this you fetch the next page of the aggregation with the `after_key`:

```auto
GET /_search
{
  "size": 0,
  "aggs": {
    "my_buckets": {
      "composite": {
        "size": 2,
        "sources": [
          {
            "date": {
              "date_histogram": {
                "field": "date",
                "calendar_interval": "1d",
                "order": "desc"
              }
            }
          },
          {
            "product": {
              "terms": {
                "field": "product.keyword",
                "order": "asc"
              }
            }
          }
        ],
        "after": {
          "date": 1578096000000,
          "product": "d"
        }
      }
    }
  }
}

```

That gets you back:

```auto
  "aggregations" : {
    "my_buckets" : {
      "after_key" : {
        "date" : 1577923200000,
        "product" : "b"
      },
      "buckets" : [
        {
          "key" : {
            "date" : 1578009600000,
            "product" : "c"
          },
          "doc_count" : 1
        },
        {
          "key" : {
            "date" : 1577923200000,
            "product" : "b"
          },
          "doc_count" : 1
        }
      ]
    }
  }

```

**Now** if you want to go to the page before, instead of after, take the first bucket of this result:

```
      "key" : {
        "date" : 1578009600000,
        "product" : "c"
      }

```

And invert the `asc` and `desc`, so you get the following query for the before page:

```auto
GET /_search
{
  "size": 0,
  "aggs": {
    "my_buckets": {
      "composite": {
        "size": 2,
        "sources": [
          {
            "date": {
              "date_histogram": {
                "field": "date",
                "calendar_interval": "1d",
                "order": "asc"
              }
            }
          },
          {
            "product": {
              "terms": {
                "field": "product.keyword",
                "order": "desc"
              }
            }
          }
        ],
        "after": {
          "date": 1578009600000,
          "product": "c"
        }
      }
    }
  }
}

```

And that takes you to the previous page again:

```auto
"aggregations" : {
    "my_buckets" : {
      "after_key" : {
        "date" : 1578182400000,
        "product" : "e"
      },
      "buckets" : [
        {
          "key" : {
            "date" : 1578096000000,
            "product" : "d"
          },
          "doc_count" : 1
        },
        {
          "key" : {
            "date" : 1578182400000,
            "product" : "e"
          },
          "doc_count" : 1
        }
      ]
    }
  }

```

Source: [Elastic Search Pagination](https://discuss.elastic.co/t/elastic-search-pagination/240141/2).

---

<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 4, 2022, 8:05am UTC](https://discuss.elastic.co/t/composite-aggregations-and-pagination/240268/2 "2022-11-04T08:05:26Z")

</div>


