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

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 :

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

Here is my aggregation query.

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

From this sample repo: GitHub - spinscale/elasticsearch-rest-client-samples: Elasticsearch REST client samples using Testcontainers

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?

1 Like

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.

1 Like

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