# Add Metric Aggregation inside the bucket aggregation query to get metrics of all buckets using DSL Query

**URL:** https://discuss.elastic.co/t/add-metric-aggregation-inside-the-bucket-aggregation-query-to-get-metrics-of-all-buckets-using-dsl-query/321217
**Category:** Elasticsearch
**Created:** [December 14, 2022, 12:19pm UTC](https://discuss.elastic.co/t/add-metric-aggregation-inside-the-bucket-aggregation-query-to-get-metrics-of-all-buckets-using-dsl-query/321217 "2022-12-14T12:19:21Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Divyank\_Mahalle](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/divyank_mahalle/32/91240_2.png) [@Divyank\_Mahalle](https://discuss.elastic.co/u/Divyank_Mahalle)
#### Post date: [December 14, 2022, 12:19pm UTC](https://discuss.elastic.co/t/add-metric-aggregation-inside-the-bucket-aggregation-query-to-get-metrics-of-all-buckets-using-dsl-query/321217/1 "2022-12-14T12:19:21Z")

</div>

Hi,

I am able to get bucket aggregation results for below DSL Query,But I want to get metric aggregation for all buckets inside the same bucket aggregation query.

Bucket aggrgation query:

```auto
{
  "size": 0,
      "query": {
        "bool": {
            "filter": [
                {
                    "range": {
                        "@timestamp": {
                            "from": "{{period_end}}||-100d",
                            "to": "{{period_end}}",
                            "include_lower": true,
                            "include_upper": true,
                            "format": "epoch_millis",
                            "boost": 1
                        }
                    }
                },
                 {
                    "terms": {
                        "function_name.keyword": [
                            "RM2"
                        ],
                        "boost": 1
                    }
                },
                 {
                    "terms": {
                        "asset_name.keyword": [
                            "R1SD MOTOR DS"
                        ],
                        "boost": 1
                    }
                }
                ]
        }
      },
  "aggregations": {
    "no_of_tag_id": {
      "terms": {
        "field": "tag.id.keyword",
        "size": 2
      }
    }
  }}

```

Output:

```auto
{
    "_shards": {
        "total": 1,
        "failed": 0,
        "successful": 1,
        "skipped": 0
    },
    "hits": {
        "hits": [],
        "total": {
            "value": 34,
            "relation": "eq"
        },
        "max_score": null
    },
    "took": 3,
    "timed_out": false,
    "aggregations": {
        "no_of_tag_id": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 15,
            "buckets": [
                {
                    "doc_count": 14,
                    "key": "M_E2MDD_CFB_d44a8194-d088-4d63-bba3-149da373cffd"
                },
                {
                    "doc_count": 5,
                    "key": "I_DRV155_CFB_d44a8194-d088-4d63-bba3-149da373cffd"
                }
            ]
        }
    }
}

```

When I added nexted metric aggregation query inside bucket aggregation, I am getting results of only metric aggregation query and not bucket aggregation.

metric+bucket aggs dsl query:

```auto
{
  "size": 0,
      "query": {
        "bool": {
            "filter": [
                {
                    "range": {
                        "@timestamp": {
                            "from": "{{period_end}}||-100d",
                            "to": "{{period_end}}",
                            "include_lower": true,
                            "include_upper": true,
                            "format": "epoch_millis",
                            "boost": 1
                        }
                    }
                },
                 {
                    "terms": {
                        "function_name.keyword": [
                            "RM2"
                        ],
                        "boost": 1
                    }
                },
                 {
                    "terms": {
                        "asset_name.keyword": [
                            "R1SD MOTOR DS"
                        ],
                        "boost": 1
                    }
                }
                ]
        }
      },
  "aggregations": {
    "no_of_tag_id": {
      "terms": {
        "field": "tag.id.keyword",
        "size": 2
      }
    }
    },
     "aggs": {
                "max_value_per_bucket": {
                    "max": {
                        "field": "tag.value"
                    }
                }
     }}

```

OUTPUT:

```auto
{
    "_shards": {
        "total": 1,
        "failed": 0,
        "successful": 1,
        "skipped": 0
    },
    "hits": {
        "hits": [],
        "total": {
            "value": 34,
            "relation": "eq"
        },
        "max_score": null
    },
    "took": 3,
    "timed_out": false,
      "aggregations": {
          "max_value_per_bucket": {
              "value": 3000
          }
      }
}

```

I want to get metric aggregation per bucket.

Expected Output:

```auto
{
    "_shards": {
        "total": 1,
        "failed": 0,
        "successful": 1,
        "skipped": 0
    },
    "hits": {
        "hits": [],
        "total": {
            "value": 34,
            "relation": "eq"
        },
        "max_score": null
    },
    "took": 2,
    "timed_out": false,
    "aggregations": {
        "no_of_tag_id": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 15,
            "buckets": [
                {
                    "doc_count": 14,
                    "key": "M_E2MDD_CFB_d44a8194-d088-4d63-bba3-149da373cffd",
                   "aggregations": {
                   "max_value_per_bucket": {
                   "value": 59
                                      }
                     }
                },
                {
                    "doc_count": 5,
                    "key": "I_DRV155_CFB_d44a8194-d088-4d63-bba3-149da373cffd",
                   "aggregations": {
                   "max_value_per_bucket": {
                   "value": 3000
                                      }
                     }
                }
            ]
        }
    }
}

```

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [December 14, 2022, 7:27pm UTC](https://discuss.elastic.co/t/add-metric-aggregation-inside-the-bucket-aggregation-query-to-get-metrics-of-all-buckets-using-dsl-query/321217/2 "2022-12-14T19:27:39Z")

</div>

Hi @Divyank_Mahalle

Try this:

```auto
{
  "size": 0,
  "aggs": {
    "no_of_tag_id": {
      "terms": {
        "field": "tag.id.keyword",
        "size": 2
      },
      "aggs": {
        "max_value_per_bucket": {
          "max": {
            "field": "tag.value"
          }
        }
      }
    }
  }
}

```

---

<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: [January 11, 2023, 7:27pm UTC](https://discuss.elastic.co/t/add-metric-aggregation-inside-the-bucket-aggregation-query-to-get-metrics-of-all-buckets-using-dsl-query/321217/3 "2023-01-11T19:27:58Z")

</div>

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