# NestedAggregation in co.elastic.clients:elasticsearch-java

**URL:** <https://discuss.elastic.co/t/nestedaggregation-in-co-elastic-clients-elasticsearch-java/363829>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [July 26, 2024, 7:26am UTC](https://discuss.elastic.co/t/nestedaggregation-in-co-elastic-clients-elasticsearch-java/363829 "2024-07-26T07:26:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![S\_R](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s_r/32/123391_2.png) [@S\_R](https://discuss.elastic.co/u/S_R)\
**Post date:** [July 26, 2024, 7:26am UTC](https://discuss.elastic.co/t/nestedaggregation-in-co-elastic-clients-elasticsearch-java/363829/1 "2024-07-26T07:26:09Z")

</div>

Hi,

I'm currently migrating my project to use the new client `co.elastic.clients:elasticsearch-java:7.17.22`. Previously, with `org.elasticsearch:elasticsearch:7.8.1`, I created nested aggregations in my JUnit tests like this:

```java
TermsAggregationBuilder expectedTerms = new TermsAggregationBuilder("agg");
        expectedTerms.field("data.Nested.NestedInner.NestedInnerField");
        expectedTerms.size(1000);
NestedAggregationBuilder expectedInnerNested = new NestedAggregationBuilder("nested", "data.Nested.NestedInner");
expectedInnerNested.subAggregation(expectedTerms);

NestedAggregationBuilder expectedParentNested = new NestedAggregationBuilder("nested", "data.Nested");
expectedParentNested.subAggregation(expectedInnerNested);

```

This would return a JSON structure like:

```json
{
   "nested": {
      "nested": {
         "path": "data.Nested"
      },
      "aggregations": {
         "nested": {
            "nested": {
               "path": "data.Nested.NestedInner"
            },
            "aggregations": {
               "agg": {
                  "terms": {
                     "field": "data.Nested.NestedInner.NestedInnerField",
                     "size": 1000,
                     "min_doc_count": 1,
                     "shard_min_doc_count": 0,
                     "show_term_doc_count_error": false,
                     "order": [
                        { "_count": "desc" },
                        { "_key": "asc" }
                     ]
                  }
               }
            }
         }
      }
   }
}

```

Now, with the new client, I tried to create the same nested aggregation using:

```java
String jsonString = "{\"nested\":{\"nested\":{\"path\":\"data.Nested\"},\"aggregations\":{\"nested\":{\"nested\":{\"path\":\"data.Nested.NestedInner\"},\"aggregations\":{\"agg\":{\"terms\":{\"field\":\"data.Nested.NestedInner.NestedInnerField\",\"size\":1000,\"min_doc_count\":1,\"shard_min_doc_count\":0,\"show_term_doc_count_error\":false,\"order\":[{\"_count\":\"desc\"},{\"_key\":\"asc\"}]}}}}}}}";

return new NestedAggregation.Builder()
    .withJson(stringReader)
    .build();

```

or

```auto
new Aggregation
            .Builder()
                .withJson(stringReader)
                .build();

```

However, I received the following error:

```auto
co.elastic.clients.json.JsonpMappingException: Error deserializing co.elastic.clients.elasticsearch._types.aggregations.NestedAggregation: Unknown field 'nested' (JSON path: nested) (line no=1, column no=10, offset=9)

```

or

```auto
Caused by: co.elastic.clients.json.JsonpMappingException: Error deserializing co.elastic.clients.elasticsearch._types.aggregations.NestedAggregation: Unknown field 'nested' (JSON path: nested.nested) (line no=1, column no=20, offset=19)
	at co.elastic.clients.json.ObjectDeserializer.parseUnknownField

```

Can anyone help me understand what I'm doing wrong? How can I properly create a `NestedAggregation` with sub-aggregations using the new client?

Thanks!

---

<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:** [July 26, 2024, 9:15am UTC](https://discuss.elastic.co/t/nestedaggregation-in-co-elastic-clients-elasticsearch-java/363829/2 "2024-07-26T09:15:04Z")

</div>

Hello and welcome!

The first part of the json structure you sent:

```auto
   "nested": {
      "nested": {
         "path": "data.Nested"
      },

```

isn't part of the aggregation structure, that's why the new client doesn't recognize it. I was trying to understand what it actually is by sending the request to the server using the Kibana dev tools, but it doesn't seem to be a query, could you give me more information on this?

In the meantime, this is how to write a nested aggregation with the 7.17.21 version of the java client:

```java
 esClient.search(s -> s.index("my-index")
     .aggregations("nested", a -> a
         .nested(n -> n.path("data.Nested.NestedInner"))
         .aggregations("agg", ag -> ag
             .terms(t -> t
                 .field("data.Nested.NestedInner.NestedInnerField")
                 .size(1000)
                 .minDocCount(1)
                 .showTermDocCountError(false)
                  .order(NamedValue.of("_count", SortOrder.Desc),NamedValue.of("_key",SortOrder.Asc)))))
     , Object.class);

```

There's one field missing which is `shard_min_doc_count`, we just recently noticed it was missing from TermsAggregation, we already [merged a fix](https://github.com/elastic/elasticsearch-specification/pull/2175) for it which will be available in the 7.17.23 version coming out in a few days.

---

<div class="post-metadata">

**Author:** ![S\_R](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s_r/32/123391_2.png) [@S\_R](https://discuss.elastic.co/u/S_R)\
**Post date:** [July 26, 2024, 10:11am UTC](https://discuss.elastic.co/t/nestedaggregation-in-co-elastic-clients-elasticsearch-java/363829/3 "2024-07-26T10:11:22Z")

</div>

Hi,

> [@ltrotta](#):
>
> isn't part of the aggregation structure, that's why the new client doesn't recognize it. I was trying to understand what it actually is by sending the request to the server using the Kibana dev tools, but it doesn't seem to be a query, could you give me more information on this?

We have a junit test to create nestedAggregations + subAggregations:

```auto
TermsAggregationBuilder expectedTerms = new TermsAggregationBuilder("agg");
        expectedTerms.field("data.Nested.NestedInner.NestedInnerField");
        expectedTerms.size(1000);
        NestedAggregationBuilder expectedInnerNested = new NestedAggregationBuilder("nested", "data.Nested.NestedInner");
        expectedInnerNested.subAggregation(expectedTerms);
        NestedAggregationBuilder expectedParentNested = new NestedAggregationBuilder("nested1", "data.Nested");
        expectedParentNested.subAggregation(expectedInnerNested);

```

expectedParentNested will have

```auto
{
   "nested":{
      "nested":{
         "path":"data.Nested"
      },
      "aggregations":{
         "nested":{
            "nested":{
               "path":"data.Nested.NestedInner"
            },
            "aggregations":{
               "agg":{
                  "terms":{
                     "field":"data.Nested.NestedInner.NestedInnerField",
                     "size":1000,
                     "min_doc_count":1,
                     "shard_min_doc_count":0,
                     "show_term_doc_count_error":false,
                     "order":[
                        {
                           "_count":"desc"
                        },
                        {
                           "_key":"asc"
                        }
                     ]
                  }
               }
            }
         }
      }
   }
}

```

As far as I understand this test, we wanted to have

```auto
"nested":{
         "path":"data.Nested",
         "name": "nested"
      }

```

instead of

```auto
 "nested":{
      "nested":{
         "path":"data.Nested"
      }

```

In java docs, I found `NestedAggregationBuilder(name, path), name - is name of Aggregation but not a node 'Nested'

A new client:

```auto
Aggregation.of(builder -> builder
                .nested(n -> n.path("data.Nested"))
                .aggregations("nested", n -> n.nested(s ->s.path("data.Nested.NestedInner").name("nested1"))).aggregations("agg",
                        a -> a.terms(t -> t.field("data.Nested.NestedInner.NestedInnerField").size(100))));

```

```auto
nested:{
"path": data.Nested.NestedInner
"name: nested1
}

```

I think it is OK.

---

<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:** [July 26, 2024, 1:18pm UTC](https://discuss.elastic.co/t/nestedaggregation-in-co-elastic-clients-elasticsearch-java/363829/4 "2024-07-26T13:18:57Z")

</div>

Hope it helped! If you're still having issues with aggregations here are some other examples of nested/sub aggregations:

> [@Springdata elasticsearch 5.2 nested aggregation without subAggregation](https://discuss.elastic.co/t/springdata-elasticsearch-5-2-nested-aggregation-without-subaggregation/362052):
>
> Help Needed: Nested Aggregations with Java API Client v8 I'm working on a project using Elasticsearch with the Java API Client v8, and I'm struggling to correctly implement nested aggregations. I'd greatly appreciate any help or guidance. Index Mapping Here's the relevant part of my index mapping: { "actions" : { "mappings" : { "properties" : { "actionRoles" : { "type" : "nested", "properties" : { "roleName" : { "type" : "keywor…

> [@How to create SubAggregation in new JAVA API Client](https://discuss.elastic.co/t/how-to-create-subaggregation-in-new-java-api-client/357529/2):
>
> 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 …

---

<div class="post-metadata">

**Author:** ![S\_R](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s_r/32/123391_2.png) [@S\_R](https://discuss.elastic.co/u/S_R)\
**Post date:** [July 26, 2024, 1:47pm UTC](https://discuss.elastic.co/t/nestedaggregation-in-co-elastic-clients-elasticsearch-java/363829/5 "2024-07-26T13:47:08Z")

</div>

Thank you 😉
