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