Sub Aggregation in new JAVA Api Client

TermsAggregationBuilder term = AggregationBuilders.terms("agg1").field("test");
term.size(100);

FilterAggregationBuilder filterAgg = new FilterAggregationBuilder("agg2", new MatchAllQueryBuilder()).subAggregation(term);

How can I achieve the same using the new AggregationBuilder for the Java API Client ?

Aggregation term2 = AggregationBuilders.terms().name("agg1").field("test").build()._toAggregation();

Aggregation filterAgg = AggregationBuilders.filter().matchAll(QueryBuilders.matchAll().build()).build()._toAggregation();

Now I need to add subAggregation 'term' to 'filterAgg' that I'm not able to figure out

Hi @Sarthak_Agarwal

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

    SearchRequest searchRequest = new SearchRequest.Builder()
        .index("idx_name")
        .size(0)
        .aggregations(map)
        .build();

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