# Metric Visualization: Average Bucket over all the terms of an aggregation

**URL:** <https://discuss.elastic.co/t/metric-visualization-average-bucket-over-all-the-terms-of-an-aggregation/175902>\
**Category:** Kibana\
**Created:** [April 8, 2019, 6:08pm UTC](https://discuss.elastic.co/t/metric-visualization-average-bucket-over-all-the-terms-of-an-aggregation/175902 "2019-04-08T18:08:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rami\_Batal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rami_batal/32/43696_2.png) [@Rami\_Batal](https://discuss.elastic.co/u/Rami_Batal)\
**Post date:** [April 8, 2019, 6:08pm UTC](https://discuss.elastic.co/t/metric-visualization-average-bucket-over-all-the-terms-of-an-aggregation/175902/1 "2019-04-08T18:08:49Z")

</div>

**Mapping**

```
"mappings":{
	"user_stats": {
	   "properties": {
	        "daily_stats": {
	          "type": "object"
	        }
	   }
	}
}

```

- The `_id` of each documents represent a user ID.
- The `daily_stats` is an array of objects, each object represents a day and contains the count of two types of actions.

**Example**

Here is an example for the user ID `1063`:

```
{
        "_index" : "user_stats",
        "_type" : "user_stats",
        "_id" : "1063",
        "_score" : 1.0,
        "_source" : {
            "daily_stats" : [
                {
                   "action_1_count" : 4,
                   "action_2_count" : 10
                },
                {
                   "action_1_count" : 1,
                   "action_2_count" : 3
                },
                {
                   "action_1_count" : 3,
                   "action_2_count" : 2
                }
          ]
}

```

**Goal**

I have several 100Ks of users in my index, and my goal is to **create a Metric Visualization in Kibana** that shows the overall average of `action_1_count`. This means that I have to:

1. Calculate the average of `action_1_count` for each user, over all his/her `daily_stats` objects.
2. Calculate the average of the averages of all users.

[![Metric Visualization in Kibana](https://i.stack.imgur.com/kDWGh.png)](https://i.stack.imgur.com/kDWGh.png)

**Problem**

- I am trying to do an Average Bucket and choose the Terms Aggregation for the `_id` field, but I am obliged to fix the size of the aggregation.
- As I said, my goal is to calculate the average over all user, so I don't want to fix the size parameter.

[![In Kibana Metric Visualization, we have to set the size of the term aggregation, but we need all the terms to calculate the metric.](https://i.stack.imgur.com/UjBww.png)](https://i.stack.imgur.com/UjBww.png)

**Questions**

- Is there a way to bypass the the need to fix the size of the aggregation in Kibana.
- If this is not possible, is there an alternative solution to acheive the same goal?

---

<div class="post-metadata">

**Author:** ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)\
**Post date:** [April 9, 2019, 12:29am UTC](https://discuss.elastic.co/t/metric-visualization-average-bucket-over-all-the-terms-of-an-aggregation/175902/2 "2019-04-09T00:29:03Z")

</div>

> [@Rami\_Batal](#):
>
> The `daily_stats` is an array of objects, each object represents a day and contains the count of two types of actions.

To search this data correctly with Elasticsearch, it will need not `object` mapping but `nested`: [Nested field type | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html).

Unfortunately, Kibana currently does not support data mapped as nested. You'll need to have fields that summarize the nested data with metrics, and use those fields to aggregate on.

---

<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:** [May 7, 2019, 12:29am UTC](https://discuss.elastic.co/t/metric-visualization-average-bucket-over-all-the-terms-of-an-aggregation/175902/3 "2019-05-07T00:29:04Z")

</div>

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