# Metric aggregation on composite buckets not working with nested fields

**URL:** https://discuss.elastic.co/t/metric-aggregation-on-composite-buckets-not-working-with-nested-fields/317712
**Category:** Elasticsearch
**Created:** [October 28, 2022, 8:30pm UTC](https://discuss.elastic.co/t/metric-aggregation-on-composite-buckets-not-working-with-nested-fields/317712 "2022-10-28T20:30:02Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Ahmed\_Syed](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmed_syed/32/112663_2.png) [@Ahmed\_Syed](https://discuss.elastic.co/u/Ahmed_Syed)
#### Post date: [October 28, 2022, 8:30pm UTC](https://discuss.elastic.co/t/metric-aggregation-on-composite-buckets-not-working-with-nested-fields/317712/1 "2022-10-28T20:30:02Z")

</div>

I am able to create buckets from different sources (one of them being a nested field) successfully. However when I do a metric aggregation, the metric aggregation is not applied on the composite bucket. For example:  
I create this index:

```auto
PUT /test5
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings" : {
      "dynamic" : "false",
      "properties" : {
        "groupId" : {
          "type" : "keyword"
        },
        "messageType" : {
          "type" : "keyword"
        },
        "payload" : {
          "type" : "nested",
          "include_in_root": true,
          "properties": {
            "request": {
              "type":"nested",
              "include_in_root":true,
              "properties": {
                "data": {
                  "type":"nested",
                  "include_in_root": true,
                  "properties": {
                    "chargingPeriods": {
                      "type": "nested",
                      "include_in_root": true,
                      "properties" : {
                        "endDateTime":{
                          "type": "date"
                        },
                        "power": {
                          "type": "double"
                        },
                        "startDateTime":{
                          "type": "date"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
}

```

Insert test data:

```auto
    POST test5/_doc/testdocu1
{
  "groupId": "563",
  "messageType": "test",
  "payload": {
    "request": {
      "data": {
        "chargingPeriods": [
          {
            "endDateTime": "2022-10-13T17:42:25Z",
            "power": 9.62857,
            "startDateTime": "2022-10-13T17:41:55Z"
          },
          {
            "endDateTime": "2022-10-13T17:42:55Z",
            "power": 9.6491,
            "startDateTime": "2022-10-13T17:42:25Z"
          },
          {
            "endDateTime": "2022-10-13T17:43:25Z",
            "power": 9.6491,
            "startDateTime": "2022-10-13T17:42:55Z"
          },
          {
            "endDateTime": "2022-10-13T17:43:55Z",
            "power": 9.66963,
            "startDateTime": "2022-10-13T17:43:25Z"
          },
          {
            "endDateTime": "2022-10-13T17:44:25Z",
            "power": 9.67128,
            "startDateTime": "2022-10-13T17:43:55Z"
          },
          {
            "endDateTime": "2022-10-13T17:44:55Z",
            "power": 9.65079,
            "startDateTime": "2022-10-13T17:44:25Z"
          },
          {
            "endDateTime": "2022-10-13T17:45:25Z",
            "power": 9.66492,
            "startDateTime": "2022-10-13T17:44:55Z"
          },
          {
            "endDateTime": "2022-10-13T17:45:55Z",
            "power": 9.68544,
            "startDateTime": "2022-10-13T17:45:25Z"
          },
          {
            "endDateTime": "2022-10-13T17:46:25Z",
            "power": 9.68544,
            "startDateTime": "2022-10-13T17:45:55Z"
          },
          {
            "endDateTime": "2022-10-13T17:46:55Z",
            "power": 9.67434,
            "startDateTime": "2022-10-13T17:46:25Z"
          }
        ]
      }
    }
  }
}

```

My query:

```auto
GET test5/_search
{
  "size": 0,
  "aggs": {
    "my_buckets": {
      "composite": {
        "sources": [
          { "sessionId": { "terms": { "field": "groupId"} } },
          {
            "date" : {
              "date_histogram": {
                "field": "payload.request.data.chargingPeriods.startDateTime",
                "fixed_interval": "2m",
                "format": "MM/dd/yyyy - hh:mm:ss",
                "order": "asc"
              }
            }
          }
        ]
      },
      "aggregations": {
        "metricAgg": {
          "max": {
            "field": "payload.request.data.chargingPeriods.power"
          }
        }
      }      
    }
  },
  "query": {
    "terms": {
      "messageType": [
        "test"
      ]
    }
  }
}

```

My output:

```auto
"aggregations" : {
    "my_buckets" : {
      "after_key" : {
        "sessionId" : "563",
        "date" : "10/13/2022 - 05:46:00"
      },
      "buckets" : [
        {
          "key" : {
            "sessionId" : "563",
            "date" : "10/13/2022 - 05:40:00"
          },
          "doc_count" : 1,
          "metricAgg" : {
            "value" : 9.68544
          }
        },
        {
          "key" : {
            "sessionId" : "563",
            "date" : "10/13/2022 - 05:42:00"
          },
          "doc_count" : 4,
          "metricAgg" : {
            "value" : 9.68544
          }
        },
        {
          "key" : {
            "sessionId" : "563",
            "date" : "10/13/2022 - 05:44:00"
          },
          "doc_count" : 4,
          "metricAgg" : {
            "value" : 9.68544
          }
        },
        {
          "key" : {
            "sessionId" : "563",
            "date" : "10/13/2022 - 05:46:00"
          },
          "doc_count" : 1,
          "metricAgg" : {
            "value" : 9.68544
          }
        }
      ]
    }
  }

```

As you can see, it is choosing the MAX power value from all the elements in chargingPeriods in the entire document rather than choosing the max value from the composite bucket. For example, the following snippet from the output:

```auto
{
  "key" : {
    "sessionId" : "563",
    "date" : "10/13/2022 - 05:40:00"
  },
  "doc_count" : 1,
  "metricAgg" : {
    "value" : 9.68544
  }
},

```

should have the metricAgg value as 9.62857

---

<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: [November 25, 2022, 8:30pm UTC](https://discuss.elastic.co/t/metric-aggregation-on-composite-buckets-not-working-with-nested-fields/317712/2 "2022-11-25T20:30:05Z")

</div>

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