# 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:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anna\_Mikhilev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anna_mikhilev/32/103354_2.png) [@Anna\_Mikhilev](https://discuss.elastic.co/u/Anna_Mikhilev)\
**Post date:** [August 15, 2022, 7:09am UTC](https://discuss.elastic.co/t/sub-aggregations-in-java-api-client/312072/1 "2022-08-15T07:09:06Z")

</div>

Hi,  
I am trying to rewrite our code for building multiple aggregations to using the new java API client 7.16.3.  
I can't find an example for sub aggregations

```auto
TermsAggregationBuilder aggregationBuilder AggregationBuilders.terms(aggregateBy.getAttributeName());
aggregationBuilder.subAggregation(...

```

How to construct this with a new client?

```auto
 TermsAggregation.Builder termsBuilder = AggregationBuilders.terms();
 termsBuilder.field(aggregateBy.getAttributeName());
termsBuilder.name(aggregateBy.getAttributeName());
  termsBuilder.build()._toAggregation().aggregations( ???

```

Thanks

---

<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);

```

---

<div class="post-metadata">

**Author:** ![Anna\_Mikhilev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anna_mikhilev/32/103354_2.png) [@Anna\_Mikhilev](https://discuss.elastic.co/u/Anna_Mikhilev)\
**Post date:** [August 16, 2022, 6:53am UTC](https://discuss.elastic.co/t/sub-aggregations-in-java-api-client/312072/3 "2022-08-16T06:53:13Z")

</div>

Yes, thanks  
it helped 😀

---

<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:** [September 13, 2022, 6:53am UTC](https://discuss.elastic.co/t/sub-aggregations-in-java-api-client/312072/4 "2022-09-13T06:53:51Z")

</div>

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