# Nested Aggregation Query with Java OpenSearchClient

**URL:** <https://forum.opensearch.org/t/nested-aggregation-query-with-java-opensearchclient/12932>\
**Category:** OpenSearch\
**Tags:** discuss\
**Created:** [March 3, 2023, 10:01pm UTC](https://forum.opensearch.org/t/nested-aggregation-query-with-java-opensearchclient/12932 "2023-03-03T22:01:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alqayevt](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@alqayevt](https://forum.opensearch.org/u/alqayevt)\
**Post date:** [March 3, 2023, 10:01pm UTC](https://forum.opensearch.org/t/nested-aggregation-query-with-java-opensearchclient/12932/1 "2023-03-03T22:01:31Z")

</div>

**Versions** :  
org.opensearch.client:opensearch-java:2.0.0  
org.opensearch.client:opensearch-rest-client:1.3.6

**Describe the issue** :  
I have one nested type and i have to aggregate by nested type’s field. SearchResponse class returns NestedAggregate and this class doesn’t hold TermsAggregate. And i don’t know how can i get buckets with these configuration.

**Configuration** :

```auto

final String agg = "agg";

final String subAgg = "subAggr";

org.opensearch.client.opensearch.OpenSearchClient client = ...

final org.opensearch.client.opensearch.core.SearchResponse<Void> searchResponse = client.search(b -> b
                            .index("index")
                            .aggregations(agg,
                                    x -> x.nested(NestedAggregation.of(xy -> xy.path("nesteTypeObjectName")))
                                            .aggregations(subAgg,
                                                    Aggregation.of(
                                                            a -> a.terms(xz -> xz.field("fieldOfNestedTypeObject")
                                                                    .size(2000)))))
                    , Void.class);

final Map<String, org.opensearch.client.opensearch._types.aggregations.Aggregate> aggregations = searchResponse.aggregations();
final org.opensearch.client.opensearch._types.aggregations.NestedAggregate nestedAggregation = aggregations.get(agg);

```

// aggregations map holds just one aggr, doc\_count returns correct but there is nothing another related with subAggregation.

**Relevant Logs or Screenshots** :

It works correctly from dashboard.  
_example request_ :

```auto
GET index/_search
{
  "aggs": {
    "agg": {
      "nested": {
        "path": "nestedObjectName"
      },
      "aggs": {
        "subAggr": {
          "terms": {
            "field": "fieldOfNestedTypeObject",
            "size": 1000
          }
        }
      }
    }
  }
}

```

_And response_

```auto
{
  "took" : 67,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 6558,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [...]
  },
"aggregations" : {
    "agg" : {
      "doc_count" : 1,
      "subAggr" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 1,
        "buckets" : [
          {
            "key" : "any data",
            "doc_count" : 1
          }
        ]
      }
    }
  }

```

---

<div class="post-metadata">

**Author:** ![alqayevt](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@alqayevt](https://forum.opensearch.org/u/alqayevt)\
**Post date:** [March 6, 2023, 9:27am UTC](https://forum.opensearch.org/t/nested-aggregation-query-with-java-opensearchclient/12932/2 "2023-03-06T09:27:05Z")

</div>

[Bucket aggregations | Java Transport Client (deprecated) [7.17] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/_bucket_aggregations.html) - need something like that

---

<div class="post-metadata">

**Author:** ![alqayevt](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@alqayevt](https://forum.opensearch.org/u/alqayevt)\
**Post date:** [March 6, 2023, 3:54pm UTC](https://forum.opensearch.org/t/nested-aggregation-query-with-java-opensearchclient/12932/3 "2023-03-06T15:54:26Z")

</div>

[[BUG] Nested/Sub Aggregations are not sending back buckets containing the data · Issue #197 · opensearch-project/opensearch-java · GitHub](https://github.com/opensearch-project/opensearch-java/issues/197) - fixed in here if someone needs.
