Multiple field aggregation of Elasticsearch data ranges

The structure of my documents is as follows:

{
    "element": "A",
    "date": "2014-01-01",
    "valid_until": "2014-02-01"
},
{
    "element": "A",
    "date": "2014-02-01",
    "valid_until": "9999-12-31"
}

The date "9999-12-31" is here to say: "it has not expired." Such a range always exists, therefore, for a given "A" element, date> valid_until can never overlap. Therefore, I can calculate how many elements I have using a pseudo code, for example: COUNT elements WHERE date <date_to_count AND valid_until> = date_to_count

Where "date_to_count" is the date I want to count the values. Since I want to calculate this at several points in time, I can either use a date histogram or aggregate a date range. However, a date range only works with one field type. Ideally, I would like to be able to do this:

"aggs": {
   "foo": {
       "date_range": {
          "fields": ["date", "valid_until"],
          "ranges": [
              {"from": "2014-01-01", "to": {"2014-02-01"}},
              {"from": "2014-02-01", "to": {"2014-03-01"}},
              {"from": "2014-03-01", "to": {"2014-04-01"}}
          ]
       }
   }
}

"" "from", "valid_until" "to".

script, :/.

, , script /, , , "ctx.to", "context.to", undefined.

!

+4
2

date_range date_histogram , , . , , API , :

"query": {
  "filtered": {
    "filter": {
      "bool" {
        "must": [
          { "range": { "date": { "gte": "2014-01-01" }}},
          { "range": { "valid_until": { "lt": "2014-02-01" }}}
        ]
      }
    }
  }
}
0

. , Elasticsearch 5.2

"aggs": {
   "range1": {
       "date_range": {
          "fields": "date",
          "ranges": [
              {"from": "2014-01-01", "to": {"2014-02-01"}},
              {"from": "2014-02-01", "to": {"2014-03-01"}},
              {"from": "2014-03-01", "to": {"2014-04-01"}}
          ]
       },
   "range2": {
       "date_range": {
          "field": "valid_until",
          "ranges": [
              {"from": "2014-01-01", "to": {"2014-02-01"}},
              {"from": "2014-02-01", "to": {"2014-03-01"}},
              {"from": "2014-03-01", "to": {"2014-04-01"}}
          ]
       }
   }
}
0

All Articles