# 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:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ansh\_Kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ansh_kumar/32/123368_2.png) [@Ansh\_Kumar](https://discuss.elastic.co/u/Ansh_Kumar)\
**Post date:** [April 16, 2024, 4:43pm UTC](https://discuss.elastic.co/t/how-to-create-subaggregation-in-new-java-api-client/357529/1 "2024-04-16T16:43:42Z")

</div>

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

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

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

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

```

---

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