# Pagination of the elastic buckets with size, from and order by a timestamp field

**URL:** <https://discuss.elastic.co/t/pagination-of-the-elastic-buckets-with-size-from-and-order-by-a-timestamp-field/217803>\
**Category:** Elasticsearch\
**Created:** [February 4, 2020, 12:34pm UTC](https://discuss.elastic.co/t/pagination-of-the-elastic-buckets-with-size-from-and-order-by-a-timestamp-field/217803 "2020-02-04T12:34:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shivamkrpandey](https://avatars.discourse-cdn.com/v4/letter/s/b9e5f3/32.png) [@shivamkrpandey](https://discuss.elastic.co/u/shivamkrpandey)\
**Post date:** [February 4, 2020, 12:34pm UTC](https://discuss.elastic.co/t/pagination-of-the-elastic-buckets-with-size-from-and-order-by-a-timestamp-field/217803/1 "2020-02-04T12:34:12Z")

</div>

I want to paginate the buckets with "size" and "from" ordered by the "Timestamp" field. I have tried the bucket\_sort but it did not work as it expects another sibling aggregation and cannot aggregate on the Timestamp field here. Elasticserch 6.7.2

My sample data is,

```
    [{
        "Name": "Event A",
        "Timestamp": "2020-02-02T09:45:20.954Z"
    },
    {
        "Name": "Event B",
        "Timestamp": "2020-02-04T09:45:20.954Z"
    },
    {
        "Name": "Event C",
        "Timestamp": "2020-02-03T09:45:20.954Z"
    },
    {
        "Name": "Event A",
        "Timestamp": "2020-02-01T09:45:20.954Z"
    },
    {
        "Name": "Event D",
        "Timestamp": "2020-01-15T09:45:20.954Z"
    }]

```

and current result is ordered by the doc\_count but I want to order by date. My expected result is,

```
    "buckets": [
            {
            "key": "Event B",
            "doc_count": 1
            }
            ,
            {
            "key": "Event C",
            "doc_count": 1
            }
            ,
            {
            "key": "Event A",
            "doc_count": 2
            }
            ,
            {
            "key": "Event D",
            "doc_count": 1
            }
      ]

```

Thanks in advance.

Edit 1 -  
I have tried the query to get the ordered buckets without "from",

```
{
    "size": 0,
    "aggs": {
        "eventNames": {
            "terms": {
                "field": "Name",
                "size": 5,
                "order": {
                    "firstOccurredAggs": "desc"
                }
            },
            "aggs": {
                "firstOccurredAggs": {
                    "min": {
                        "field": "Timestamp"
                    }
                }
            }
        }
    }
}

```

But I was not able to paginate the results.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 5, 2020, 9:45am UTC](https://discuss.elastic.co/t/pagination-of-the-elastic-buckets-with-size-from-and-order-by-a-timestamp-field/217803/2 "2020-02-05T09:45:58Z")

</div>

You may want to check out the [composite agrgegation](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/search-aggregations-bucket-composite-aggregation.html)

---

<div class="post-metadata">

**Author:** ![shivamkrpandey](https://avatars.discourse-cdn.com/v4/letter/s/b9e5f3/32.png) [@shivamkrpandey](https://discuss.elastic.co/u/shivamkrpandey)\
**Post date:** [February 5, 2020, 10:39am UTC](https://discuss.elastic.co/t/pagination-of-the-elastic-buckets-with-size-from-and-order-by-a-timestamp-field/217803/3 "2020-02-05T10:39:12Z")

</div>

Hi spinscale, I tried composite too but not able to use "from" with it.

```
{
  "size": 0,
  "aggs": {
    "my_buckets": {
      "composite": {
        "sources": [
          {
            "eventNames": {
              "terms": {
                "field": "Name"
              }
            }
          },
          {
            "timestamps": {
              "terms": {
                "field": "Timestamp",
                "order": "desc"
              }
            }
          }
        ]
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 7, 2020, 2:02pm UTC](https://discuss.elastic.co/t/pagination-of-the-elastic-buckets-with-size-from-and-order-by-a-timestamp-field/217803/4 "2020-02-07T14:02:20Z")

</div>

you need to use size/after within the aggretation, see the end of the documentation.

---

<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:** [March 6, 2020, 2:06pm UTC](https://discuss.elastic.co/t/pagination-of-the-elastic-buckets-with-size-from-and-order-by-a-timestamp-field/217803/5 "2020-03-06T14:06:59Z")

</div>

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