# How to create SubAggregation in new JAVA API Client

**URL:** <https://discuss.elastic.co/t/how-to-create-subaggregation-in-new-java-api-client/357529>\
**Category:** Elasticsearch\
**Tags:** language-clients, aggregations\
**Created:** [April 16, 2024, 4:43pm UTC](https://discuss.elastic.co/t/how-to-create-subaggregation-in-new-java-api-client/357529 "2024-04-16T16:43:41Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![ltrotta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ltrotta/32/124003_2.png) [@ltrotta](https://discuss.elastic.co/u/ltrotta)\
**Post date:** [April 17, 2024, 4:30pm UTC](https://discuss.elastic.co/t/how-to-create-subaggregation-in-new-java-api-client/357529/2 "2024-04-17T16:30:47Z")

</div>

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

```auto
        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 🙂

---

_[View the full topic](https://discuss.elastic.co/t/how-to-create-subaggregation-in-new-java-api-client/357529)._
