Suppose I have a date/time field for each document. Now suppose that I group my documents into hourly buckets using a date_histogram aggregation. Now suppose that there are some hours with no documents. So the document count for a 12 hour period might look like this:
Hour 1: 12
2: 10
3: 0
4: 5
5: 90
6: 11
7: 0
8: 0
9: 56
10: 88
11: 33
12: 111
What I want to do is find the average number of consecutive buckets with non-zero counts. So here, we have the first cluster of 2 consecutive hours with non-zero counts, followed by hour 3 with 0 count, then 3 consecutive buckets of non-zero, followed by two buckets with 0, and finally 4 consecutive buckets of non-zero.
So in this example, what I want is the avg(2, 3, 4) = 3. Is there a clever way to do this in ES using some combination of aggregations?