Influencing order of buckets in (Date) Range aggregation


(Jettro Coenradie) #1

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).


(Adrien Grand) #2

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


(system) #3

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