# Elastic Search Pagination

**URL:** <https://discuss.elastic.co/t/elastic-search-pagination/240141>\
**Category:** Elasticsearch\
**Created:** [July 7, 2020, 10:14am UTC](https://discuss.elastic.co/t/elastic-search-pagination/240141 "2020-07-07T10:14:40Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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, 12:36am UTC](https://discuss.elastic.co/t/elastic-search-pagination/240141/2 "2020-07-08T00:36:11Z")

</div>

You could use the first key (kind of make it your own `before_key`) and turn around the sort order. Quick example:

Test data:

```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 the 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"
              }
            }
          }
        ]
      }
    }
  }
}

```

That 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
        }
      ]
    }
  }

```

---

_[View the full topic](https://discuss.elastic.co/t/elastic-search-pagination/240141)._
