# Terms aggregation on a nested field using Java SDK?

**URL:** <https://discuss.elastic.co/t/terms-aggregation-on-a-nested-field-using-java-sdk/353634>\
**Category:** Elasticsearch\
**Created:** [February 19, 2024, 9:18pm UTC](https://discuss.elastic.co/t/terms-aggregation-on-a-nested-field-using-java-sdk/353634 "2024-02-19T21:18:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [February 19, 2024, 9:18pm UTC](https://discuss.elastic.co/t/terms-aggregation-on-a-nested-field-using-java-sdk/353634/1 "2024-02-19T21:18:34Z")

</div>

I have a document mapping that looks like this:

```auto
      "domain": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "keyword",
            "store": true
          },
          "tld": {
            "type": "keyword",
            "store": true
          },
          "sld": {
            "type": "keyword",
            "store": true
          },
          "subdomains": {
            "type": "keyword",
            "store": true
          }
        }
      },

```

I want to get a terms aggregation of `domains.sld`, but I can't figure out how to do it using the Java SDK.

```auto
elasticsearchClient.search(s -> s.index(INDEX).size(0)
  .aggregations("nesting", n -> n.nested(
    nested -> nested.path("domain")... then what? The library offers nothing
))
, Void.class);

```

Can someone please give me an example of how to do this?

Many thanks for any help given

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [February 20, 2024, 12:11pm UTC](https://discuss.elastic.co/t/terms-aggregation-on-a-nested-field-using-java-sdk/353634/2 "2024-02-20T12:11:05Z")

</div>

Hi @ndtreviv

This example using builder style but its simple transform to other.

```
Aggregation sub_aggregation = new Aggregation.Builder()
    .terms(new TermsAggregation.Builder().field("domain.sld").size(5).build())
    .build();

Aggregation nestedAggregation = new Aggregation.Builder()
    .nested(new NestedAggregation.Builder().path("domain").build())
    .aggregations(new HashMap<>() {{
      put("avg_renevue", sub_aggregation);
    }}).build();

SearchRequest searchRequest = new SearchRequest.Builder()
    .index("idx")
    .size(0)
    .aggregations("nesting", nestedAggregation)
    .build();

```

---

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [February 20, 2024, 12:36pm UTC](https://discuss.elastic.co/t/terms-aggregation-on-a-nested-field-using-java-sdk/353634/3 "2024-02-20T12:36:38Z")

</div>

Thanks for this! I'll give it a go!

---

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [February 20, 2024, 12:44pm UTC](https://discuss.elastic.co/t/terms-aggregation-on-a-nested-field-using-java-sdk/353634/4 "2024-02-20T12:44:22Z")

</div>

How do I get results out of it?

I'm expecting to get a list of buckets, being keyed by `domain.sld` values and their `doc_counts`.

Currently trying:

```auto
results.aggregations().get("nested").nested(). then what?

```

I really wish the docs/examples were better...

---

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [February 20, 2024, 2:11pm UTC](https://discuss.elastic.co/t/terms-aggregation-on-a-nested-field-using-java-sdk/353634/5 "2024-02-20T14:11:38Z")

</div>

Trying to find a solution online. I feel like I'm the only one in the world using the newer Java client for elasticsearch.

---

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [February 20, 2024, 2:27pm UTC](https://discuss.elastic.co/t/terms-aggregation-on-a-nested-field-using-java-sdk/353634/6 "2024-02-20T14:27:34Z")

</div>

This was an issue with version 7.16.2 of the client library.

Once upgraded to 7.17.18 then `NestedAggregate` finally gets an `aggregations` method whereby you can then access the sub aggregation, in this use case like so:

```auto
StringTermsAggregate termsAggregate = results.aggregations()
  .get("nested")
  .nested()
  .aggregations()
  .get("slds")
  .sterms();

```

---

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [February 20, 2024, 2:28pm UTC](https://discuss.elastic.co/t/terms-aggregation-on-a-nested-field-using-java-sdk/353634/7 "2024-02-20T14:28:23Z")

</div>

So calling aggregations and passing a static map didn't seem to work, but this did:

```auto
            Aggregation subAggregation = new Aggregation.Builder()
                    .terms(new TermsAggregation.Builder().field("domain.subdomains").size(100).exclude(e -> e.terms(List.of(subdomain))).build())
                    .build();

            Aggregation nestedAggregation = new Aggregation.Builder()
                    .nested(new NestedAggregation.Builder().path("domain").build())
                    .aggregations("subdomains", subAggregation)
                    .build();

```

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 19, 2024, 2:28pm UTC](https://discuss.elastic.co/t/terms-aggregation-on-a-nested-field-using-java-sdk/353634/8 "2024-03-19T14:28:35Z")

</div>

Closed on March 19, 2024, 2:28pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
