# Sub Aggregations in java API client

**URL:** <https://discuss.elastic.co/t/sub-aggregations-in-java-api-client/312072>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [August 15, 2022, 7:09am UTC](https://discuss.elastic.co/t/sub-aggregations-in-java-api-client/312072 "2022-08-15T07:09:06Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [August 15, 2022, 12:31pm UTC](https://discuss.elastic.co/t/sub-aggregations-in-java-api-client/312072/2 "2022-08-15T12:31:54Z")

</div>

Hi @Anna_Mikhilev

I did this example, maybe help you.

```auto
    Map<String, Aggregation> map = new HashMap<>();

    Aggregation subAggregation = new Aggregation.Builder()
        .avg(new AverageAggregation.Builder().field("revenue").build())
        .build();

    Aggregation aggregation = new Aggregation.Builder()
        .terms(new TermsAggregation.Builder().field("director.keyword").build())
        .aggregations(new HashMap<>() {{
          put("avg_renevue", subAggregation);
        }}).build();

    map.put("agg_director", aggregation);

```

---

_[View the full topic](https://discuss.elastic.co/t/sub-aggregations-in-java-api-client/312072)._
