# Aggregation Response Bucket Order

**URL:** <https://discuss.elastic.co/t/aggregation-response-bucket-order/303544>\
**Category:** Elasticsearch\
**Created:** [April 28, 2022, 5:12pm UTC](https://discuss.elastic.co/t/aggregation-response-bucket-order/303544 "2022-04-28T17:12:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![EricPSU](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ericpsu/32/21110_2.png) [@EricPSU](https://discuss.elastic.co/u/EricPSU)\
**Post date:** [April 28, 2022, 5:12pm UTC](https://discuss.elastic.co/t/aggregation-response-bucket-order/303544/1 "2022-04-28T17:12:57Z")

</div>

I'm looking to understand the order of the buckets returned when using aggs in my query. I do not want to sort the bucket values, rather I want to sort the buckets themselves. Regardless of the order of the aggs in the query, the order of the returned buckets is the same. The buckets are not ordered alphabetically, but it appears Elastic does have some sort of ordering technique since the order is consistent, I just don't understand what it is.

For example, if I run this query:

```auto
POST /foo/_search?size=0
{
  "aggs": {
    "field1": {
      "terms": {
        "field": "field1.keyword"
      }
    },
    "field2": {
      "terms": {
        "field": "field2.keyword"
      }
    },
    "field3": {
      "terms": {
        "field": "field3.keyword"
      }
    }
  }
}

```

The resulting buckets are in the following order:

```auto
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    ...
  },
  "hits" : {
    ...
  },
  "aggregations" : {
    "field2" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        ...
    },
    "field1" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 141,
      "buckets" : [
        ...
    },
    "field3" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        ...
      ]
    }
  }
}

```

---

<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:** [May 26, 2022, 5:13pm UTC](https://discuss.elastic.co/t/aggregation-response-bucket-order/303544/2 "2022-05-26T17:13:31Z")

</div>

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