Terms aggregation on a nested field using Java SDK?

I have a document mapping that looks like this:

      "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.

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

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();

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

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:

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

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

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

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:

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

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

            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!

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