How to create SubAggregation in new JAVA API Client

Hii community ,
upgrading our application from JavaAPIClient version 7.x to 8.10 , aggregation is not working
Neither i am able to find suitable example which i can consider
in our application we are creating aggregation using these code

((DateHistogramAggregationBuilder)((DateHistogramAggregationBuilder)((DateHistogramAggregationBuilder)AggregationBuilders.dateHistogram(datesAggName).field("endDate")).calendarInterval(new DateHistogramInterval("1d")).format("yyyy-MM-dd").keyed(true).subAggregation(PipelineAggregatorBuilders.bucketSort("datesSortedAgg", Arrays.asList(new FieldSortBuilder("_key"))).size(1))).subAggregation(AggregationBuilders.topHits(instancesAggName).size(1000).sort("id", SortOrder.ASC).fetchSource("id", (String)null));

in 7.x version we had subAggregation , but i am not able to create subAggregation in version 8.13

I tried below code for updated version but it is not working , plz suggest what i am missing

 Map<String , Aggregation> aggregationMap1 = new HashMap<>();
        Map<String , Aggregation> aggregationMap2 = new HashMap<>();

        Aggregation.Builder pipelineAggregatorBuilder = new Aggregation.Builder();
        TopHitsAggregation.Builder topHitsAggregationBuilder = new TopHitsAggregation.Builder();

        topHitsAggregationBuilder.size(1000)
                .source(SourceConfig.of(s->s.filter(f->f.includes("id"))))
                .sort(so->so.field(FieldSort.of(fs->fs.field("id").order(SortOrder.Asc))));

        aggregationMap1.put("datesSortedAgg" , pipelineAggregatorBuilder.bucketSort(b->b
                .sort(so->so.field(FieldSort.of(fs->fs.field("_key")))).size(1)).build());
        aggregationMap2.put(instancesAggName , topHitsAggregationBuilder.build()._toAggregation());

        Aggregation.Builder aggregationBuilders = new Aggregation.Builder();

        return aggregationBuilders.dateHistogram(d->d
                        .name(datesAggName)
                        .field("endDate")
                        .calendarInterval(CalendarInterval.Day)
                        .format("yyyy-MM-dd")
                        .keyed(true))
                .aggregations(aggregationMap1)
                .aggregations(aggregationMap2).build();

Please suggest how to create subAggregation in version 8.13

For kibana i am using these and these is working perfectly fine but i am not able to convert it to java code using Java API clinet version 8.13

GET /demo-index/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [{
              "range": {
                "endDate": {
                  "lte": "2024-04-15T10:06:45.679+0000"
                }
              }
            },
            {
              "term": {
                "joinRelation": "processInstance"
              }
            },
            {
              "terms": {
                "partitionId": [1, 2, 3]
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "datesAggName": {
      "date_histogram": {
        "field": "endDate",
        "calendar_interval": "1d",
        "format": "yyyy-MM-dd",
        "keyed": true
      },
      "aggs": {
        "datesSortedAgg": {
          "bucket_sort": {
            "sort": [
              {
                "_key": {
                  "order": "asc"
                }
              }
              ],
              "size": 1
          }
        },
        "instancesAggName": {
          "top_hits": {
            "size": 100,
            "sort": [
              {
                "id": {
                  "order": "asc"
                }
              }
            ],
            "_source": {
              "includes": ["id"]
            }
          }
        }
      }
    }
  }, 
  "sort": [
    {
      "endDate": {
        "order": "asc"
      }
    }
  ],
  "_source": false
}

Hello! I've left out some details, but this is roughly how to translate that kibana query using the new java client, dsl style:

        esClient.search(s -> s
                .index("demo-index")
                .query(q -> q
                    .constantScore(cs -> cs
                        .filter(fl -> fl
                            .bool(b -> b
                                .must(...)))))
                .aggregations("datesAggName",agg -> agg
                    .dateHistogram(dh -> dh
                        .field("endDate")
                        .calendarInterval(CalendarInterval.Day))
                        ...
                    .aggregations("datesSortedAgg",sub -> sub
                        .bucketSort(bs -> bs
                            .sort(...)
                            .size(1))
                        .aggregations("instancesAggName", subsub -> subsub
                            .topHits(...))))
                .sort(...)
            , Object.class);

Let me know if it works :slight_smile: