Metric aggregation on composite buckets not working with nested fields

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:

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:

    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:

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:

"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:

{
  "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

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