Home > Enterprise >  Adding a filter range to each bucket in a date histogram
Adding a filter range to each bucket in a date histogram

Time:12-30

I am trying to aggregate by day and select only certain hours in days. For example, from 12:00 to 17:00.

"aggs": {
    "myDatehistogram": {
      "date_histogram": {
        "field": "@timestamp",
        "interval": "1d",
        "format": "yyyy-MM-dd H:m:s"
      }, 
        "script": {
           "script": "def hod = doc.datetime.date.getHourOfDay(); return hod >= min && hod <= max",
           "params": {
             "min": 12,
             "max": 17
           }
        }
    }
  }

I get the error: ""Found two aggregation type definitions in [myDatehistogram]: [date_histogram] and [script]"

CodePudding user response:

The filter should be located in the query part, like this:

{
  "query": {
    "script": {
      "script": "def hod = doc.datetime.date.getHourOfDay(); return hod >= min && hod <= max",
      "params": {
        "min": 12,
        "max": 17
      }
    }
  },
  "aggs": {
    "myDatehistogram": {
      "date_histogram": {
        "field": "@timestamp",
        "interval": "1d",
        "format": "yyyy-MM-dd H:m:s"
      }
    }
  }
}
  • Related