Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch: aggregation min_doc_count for weeks doesn't work

I've the following aggregation with interval=week and min_doc_count=0

{
  "aggs": {
    "scores_by_date": {
      "date_histogram": {
      "field": "date",
      "format": "yyyy-MM-dd",
      "interval": "week",
      "min_doc_count": 0
    }
  }
}

and date filter from Jan-01-2015 to Feb-23-2015

{
  "range": {
    "document.date": {
      "from": "2015-01-01",
      "to": "2015-02-23"
    }
  }
}

I expected Elasticsearch to fill seven weeks even if empty and return buckets but end up only with one item in it

{
  "aggregations": {
    "scores_by_date": {
      "buckets": [
        {
          "key_as_string": "2015-01-05",
          "key": 1420416000000,
          "doc_count": 5
        }
      ]
    }
  }
}

Elasticsearch version: 1.4.0

What is wrong with my aggregation or how can I say Elasticsearch to fill missing weeks?

like image 593
sultan Avatar asked Feb 11 '26 03:02

sultan


1 Answers

You can try specifying extended bounds (there's documentation discussing this feature on the official doc page for histogram aggregations). The most relevant nugget from those docs is this:

With extended_bounds setting, you now can "force" the histogram aggregation to start building buckets on a specific min values and also keep on building buckets up to a max value (even if there are no documents anymore). Using extended_bounds only makes sense when min_doc_count is 0 (the empty buckets will never be returned if min_doc_count is greater than 0).

So your aggregation may have to look something like this to force ES to return empty buckets in that range:

{
  "aggs": {
    "scores_by_date": {
      "date_histogram": {
      "field": "date",
      "format": "yyyy-MM-dd",
      "interval": "week",
      "min_doc_count": 0,
      "extended_bounds" : {
        "min" : "2015-01-01",
        "max" : "2015-02-23"
      }
    }
  }
}
like image 118
rchang Avatar answered Feb 15 '26 11:02

rchang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!