Sub Aggregations in java API client

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

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

How to construct this with a new client?

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

Thanks

Hi @Anna_Mikhilev

I did this example, maybe help you.

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

Yes, thanks
it helped :grinning:

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