# Numeric metrics aggregation on Nested key-value set

**URL:** <https://discuss.elastic.co/t/numeric-metrics-aggregation-on-nested-key-value-set/237096>\
**Category:** Elasticsearch\
**Created:** [June 15, 2020, 9:12am UTC](https://discuss.elastic.co/t/numeric-metrics-aggregation-on-nested-key-value-set/237096 "2020-06-15T09:12:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yonatan\_Omer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yonatan_omer/32/42450_2.png) [@Yonatan\_Omer](https://discuss.elastic.co/u/Yonatan_Omer)\
**Post date:** [June 15, 2020, 9:12am UTC](https://discuss.elastic.co/t/numeric-metrics-aggregation-on-nested-key-value-set/237096/1 "2020-06-15T09:12:14Z")

</div>

Hi

Our index contains docs with a nested set of key-values, we would need to perform metric aggregation per key.

So we for example, we would need the average **cpuTime** per **taskId** for such docs:

```auto
    {
       "tasks" : [ 
          {  
             "taskId": "global_task_proc",
             "cpuTime": 600
         },
         {
             "taskId": "task_queue",
             "cpuTime": 594
         }
       ]
    }

```

---

<div class="post-metadata">

**Author:** ![gab.bernasconi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gab.bernasconi/32/57955_2.png) [@gab.bernasconi](https://discuss.elastic.co/u/gab.bernasconi)\
**Post date:** [June 15, 2020, 10:00am UTC](https://discuss.elastic.co/t/numeric-metrics-aggregation-on-nested-key-value-set/237096/2 "2020-06-15T10:00:09Z")

</div>

Hi,

I think you are looking for the [Nested Aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html)

Let me know if this is what you were looking for!

---

<div class="post-metadata">

**Author:** ![Yonatan\_Omer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yonatan_omer/32/42450_2.png) [@Yonatan\_Omer](https://discuss.elastic.co/u/Yonatan_Omer)\
**Post date:** [June 15, 2020, 10:59am UTC](https://discuss.elastic.co/t/numeric-metrics-aggregation-on-nested-key-value-set/237096/3 "2020-06-15T10:59:04Z")

</div>

Hi thanks, I did look on the example there.  
the example in the docs takes the maximum price of ANY reseller. but we need something like the maximum price for EACH reseller.

```auto
    {
      "resellers": [
        {
          "reseller": "companyA",
          "price": 350
        },
        {
          "reseller": "companyB",
          "price": 500
        }
      ]
    }

```

so the expected output might be something like:

```auto
    [
    {
       "key": "companyA",
       "maximum" : 670,
    },
    {
       "key": "companyB",
       "maximum" : 1120,
    }

    ]

```

---

<div class="post-metadata">

**Author:** ![gab.bernasconi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gab.bernasconi/32/57955_2.png) [@gab.bernasconi](https://discuss.elastic.co/u/gab.bernasconi)\
**Post date:** [June 15, 2020, 12:20pm UTC](https://discuss.elastic.co/t/numeric-metrics-aggregation-on-nested-key-value-set/237096/4 "2020-06-15T12:20:42Z")

</div>

Can you try running this query and see if it works?

```auto
GET /<YOUR_INDEX_PATTERN_HERE>/_search
{
    "query" : {
        <YOUR_QUERY_HERE>
    },
    "aggs" : {
        "resellers" : {
            "nested" : {
                "path" : "resellers"
            },
            "aggs" : {
                "resellers": {
                    "terms": { "field": "resellers.reseller" },
                    "aggs": {
                        "max_price" : { "max" : { "field" : "resellers.price" } }
                    }
                }
            }
        }
    }
}

```

I'm not 100% sure that a `terms` aggregation would work here, but I cannot try it at the moment. Let me know how this turns out.

---

<div class="post-metadata">

**Author:** ![Yonatan\_Omer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yonatan_omer/32/42450_2.png) [@Yonatan\_Omer](https://discuss.elastic.co/u/Yonatan_Omer)\
**Post date:** [June 16, 2020, 7:54am UTC](https://discuss.elastic.co/t/numeric-metrics-aggregation-on-nested-key-value-set/237096/5 "2020-06-16T07:54:08Z")

</div>

> [@gab.bernasconi](#):
>
> ```auto
> {
> "query" : {
> <YOUR_QUERY_HERE>
> },
> "aggs" : {
> "resellers" : {
> "nested" : {
> "path" : "resellers"
> },
> "aggs" : {
> "resellers": {
> "terms": { "field": "resellers.reseller" },
> "aggs": {
> "max_price" : { "max" : { "field" : "resellers.price" } }
> }
> }
> }
> }
> }
> }
> 
> ```

Hi this is the exact solution, worked on the first try.  
Thank you so much!

---

<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:** [July 14, 2020, 7:54am UTC](https://discuss.elastic.co/t/numeric-metrics-aggregation-on-nested-key-value-set/237096/6 "2020-07-14T07:54:13Z")

</div>

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