How to create a histogram in elasticsearch which takes into account date ranges How to create a histogram in elasticsearch which takes into account date ranges elasticsearch elasticsearch

How to create a histogram in elasticsearch which takes into account date ranges


I find the question quite interesting.

Personal search didn't bring a reasonable way of achieving this, one of the reason being how would you define the start and end dates for your date histogram (as it typically uses the field parameter for figuring this)?

Someone more advanced with usage of bucket and pipeline aggregations might be able to help out, but closest I got would be by "cheating" and building a filters aggregation to achieve the goal:

{  "size": 0,   "aggs": {    "date_histo": {      "filters": {        "filters": {          "2017-01-01": {            "bool": {              "filter": [                {"range": {"start_date": {"lte": "2017-01-01"}}},                {"range": {"end_date": {"gte": "2017-01-01"}}}              ]            }          },          "2017-01-02": {            "bool": {              "filter": [                {"range": {"start_date": {"lte": "2017-01-02"}}},                {"range": {"end_date": {"gte": "2017-01-02"}}}              ]            }          },          ...        }      }    }  }}

Not very pretty, but might still be worth considering as a starting point to a better answer.