# Influencing order of buckets in (Date) Range aggregation

**URL:** <https://discuss.elastic.co/t/influencing-order-of-buckets-in-date-range-aggregation/85039>\
**Category:** Elasticsearch\
**Created:** [May 9, 2017, 8:39am UTC](https://discuss.elastic.co/t/influencing-order-of-buckets-in-date-range-aggregation/85039 "2017-05-09T08:39:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jettro](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jettro/32/3418_2.png) [@jettro](https://discuss.elastic.co/u/jettro)\
**Post date:** [May 9, 2017, 8:39am UTC](https://discuss.elastic.co/t/influencing-order-of-buckets-in-date-range-aggregation/85039/1 "2017-05-09T08:39:58Z")

</div>

Hi, i have a question about using the (date) range aggregation. The implementation of the order in which the buckets are returned seems to have changed from 1.x to 5.x. At the moment I think the buckets are ordered based on the lowest value in the from. It would be nice if I can influence this by specifying the order to be as I provide them in the ranges. It would also be helpful if I can specify the order to be descending instead of ascending. Is this possible? Below the code to show the result of the query.

```
POST /date_range/item/_bulk
{"index":{}}
{"year":1998}
{"index":{}}
{"year":1998}
{"index":{}}
{"year":1998}
{"index":{}}
{"year":2001}
{"index":{}}
{"year":2005}
{"index":{}}
{"year":2005}
{"index":{}}
{"year":2010}
{"index":{}}
{"year":2011}
{"index":{}}
{"year":2003}
{"index":{}}
{"year":2015}

GET /date_range/_search
{
  "aggs": {
    "byRange": {
      "range": {
        "field": "year",
        "ranges": [
          {
            "key": "a", 
            "from": 2000,
            "to": 2010
          },
          {
            "key": "b",
            "from": 1990,
            "to": 2000
          },
          {
            "key": "c", 
            "from": 2010,
            "to": 2020
          }
        ],
        "keyed": true
      }
    }
  },
  "size": 0
}

```

And the response

```
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 10,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "byRange": {
      "buckets": {
        "b": {
          "from": 1990,
          "to": 2000,
          "doc_count": 3
        },
        "a": {
          "from": 2000,
          "to": 2010,
          "doc_count": 4
        },
        "c": {
          "from": 2010,
          "to": 2020,
          "doc_count": 3
        }
      }
    }
  }
}

```

As for the results, I would like to return them in the order a,b,c (keys in alphabetical order or the order in which the ranges are provided) or c,a,b (order by from desc).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [May 9, 2017, 9:07am UTC](https://discuss.elastic.co/t/influencing-order-of-buckets-in-date-range-aggregation/85039/2 "2017-05-09T09:07:21Z")

</div>

Maybe you can sort the buckets when you get them back on client-side?

---

<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:** [June 6, 2017, 9:16am UTC](https://discuss.elastic.co/t/influencing-order-of-buckets-in-date-range-aggregation/85039/3 "2017-06-06T09:16:40Z")

</div>

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