Aggregation Response Bucket Order

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:

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:

{
  "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" : [
        ...
      ]
    }
  }
}

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