# How can make sub aggregation code in elasticsearch-java:7.17.1

**URL:** <https://discuss.elastic.co/t/how-can-make-sub-aggregation-code-in-elasticsearch-java-7-17-1/300001>\
**Category:** Elasticsearch\
**Created:** [March 18, 2022, 3:28am UTC](https://discuss.elastic.co/t/how-can-make-sub-aggregation-code-in-elasticsearch-java-7-17-1/300001 "2022-03-18T03:28:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mzinboy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mzinboy/32/100249_2.png) [@mzinboy](https://discuss.elastic.co/u/mzinboy)\
**Post date:** [March 18, 2022, 3:28am UTC](https://discuss.elastic.co/t/how-can-make-sub-aggregation-code-in-elasticsearch-java-7-17-1/300001/1 "2022-03-18T03:28:46Z")

</div>

Hi,  
I can't make code to aggregation with sub aggregation using "Elasticsearch-java:7.17.1" Java library.

There is no subAggregation method in TermsAggregation.Builder class.

How to complete the code that has with sub aggregation?

This is my code :

```auto
AggregationBuilders.terms()
         .field("AAA")
         //.subAggregation(...) // <- not exist
         .build();

```

Here is my aggregation query.

```auto
...
"aggs": {
    "agg_1th": {
      "terms": {
        "field": "AAA"
      },
      "aggs": {
        "agg_2nd": {
          "terms": {
            "field": "BBB"
          }
        }
      }
    }
 }
...

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 18, 2022, 3:52pm UTC](https://discuss.elastic.co/t/how-can-make-sub-aggregation-code-in-elasticsearch-java-7-17-1/300001/2 "2022-03-18T15:52:35Z")

</div>

From this sample repo: [GitHub - spinscale/elasticsearch-rest-client-samples: Elasticsearch REST client samples using Testcontainers](https://github.com/spinscale/elasticsearch-rest-client-samples)

```java
final SearchResponse<Void> response = client.search(builder -> builder.index(INDEX).size(0)
                        .aggregations("price_histo", aggBuilder ->
                                aggBuilder.histogram(histo -> histo.interval(10.0).field("price"))
                                        .aggregations("stock_average", a -> a.avg(avg -> avg.field("stock_available")))),
                Void.class);

```

Does this help?

---

<div class="post-metadata">

**Author:** ![mzinboy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mzinboy/32/100249_2.png) [@mzinboy](https://discuss.elastic.co/u/mzinboy)\
**Post date:** [March 20, 2022, 8:57am UTC](https://discuss.elastic.co/t/how-can-make-sub-aggregation-code-in-elasticsearch-java-7-17-1/300001/3 "2022-03-20T08:57:54Z")

</div>

I can't use builder pattern and lambda because of a lot of aggregation that are optional. But I got hint from your code. I used Aggregation.Builder().term() instead of AggregationBuilders.terms().  
Now it's possible to add sub aggregations and it was solved.  
Thank you very much for your help.

---

<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:** [April 17, 2022, 8:58am UTC](https://discuss.elastic.co/t/how-can-make-sub-aggregation-code-in-elasticsearch-java-7-17-1/300001/4 "2022-04-17T08:58:46Z")

</div>

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