# Daily average over terms: how to handle "missing" terms

**URL:** <https://discuss.elastic.co/t/daily-average-over-terms-how-to-handle-missing-terms/233285>\
**Category:** Elasticsearch\
**Created:** [May 19, 2020, 9:15am UTC](https://discuss.elastic.co/t/daily-average-over-terms-how-to-handle-missing-terms/233285 "2020-05-19T09:15:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kenran](https://avatars.discourse-cdn.com/v4/letter/k/91b2a8/32.png) [@kenran](https://discuss.elastic.co/u/kenran)\
**Post date:** [May 19, 2020, 9:15am UTC](https://discuss.elastic.co/t/daily-average-over-terms-how-to-handle-missing-terms/233285/1 "2020-05-19T09:15:07Z")

</div>

My logs contain information about machines and their production. Now I wish to know the average amount produced per day. I think I'd to that like this:

```auto
{
  "size": 0,
  "aggs": {
    "per_day": {
      "date_histogram": {
    	"field": "startTime",
    	"calendar_interval": "day"
      },
      "aggs": {
      	"daily_amounts": {
      		"terms": {
      			"field": "machine.keyword"
      		},
      		"aggs": {
      			"total_amount": {
          			"sum": {
          				"field": "amount"
          			}
      			}
      		}
      	},
      	"daily_avg": {
      		"avg_bucket": {
      			"buckets_path": "daily_amounts>total_amount"
      		}
      	}
      }
    }
  }
}

```

I know there are, say, six machines. But if one of those doesn't produce anything in one day, I don't have log entries for that and thus the machine doesn't appear in the terms agg.  
How can I handle this?

I tried using a bucket script aggregation like so:

```auto
{
  "size": 0,
  "aggs": {
    "per_day": {
      "date_histogram": {
    	"field": "startTime",
    	"calendar_interval": "day"
      },
      "aggs": {
      	"daily_amounts": {
      		"terms": {
      			"field": "machine.keyword"
      		},
      		"aggs": {
      			"total_amount": {
          			"sum": {
          				"field": "amount"
          			}
      			}
      		}
      	},
      	"daily_avg": {
      		"bucket_script": {
      			"buckets_path": { "foo": "daily_amounts>total_amount" },
      			"script": "params.foo / 6.0"
      		}
      	}
      }
    }
  }
}

```

But this complains about `buckets_path must reference either a number value or a single value numeric metric aggregation, got: [Object[]] at aggregation [daily_amounts]`.  
Any help is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![kenran](https://avatars.discourse-cdn.com/v4/letter/k/91b2a8/32.png) [@kenran](https://discuss.elastic.co/u/kenran)\
**Post date:** [May 19, 2020, 11:41am UTC](https://discuss.elastic.co/t/daily-average-over-terms-how-to-handle-missing-terms/233285/2 "2020-05-19T11:41:33Z")

</div>

I found a way to do this by using a sum bucket aggregation for the `total_amount` and _then_ dividing by the total number of machines via `bucket_script`. It seems clumsy to me though.

I'd love to hear about possible easier ways.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 16, 2020, 11:41am UTC](https://discuss.elastic.co/t/daily-average-over-terms-how-to-handle-missing-terms/233285/3 "2020-06-16T11:41:35Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
