0
{
  "aggs": {
    "my-agg-name": {
      "terms": {
        "field": "my-field"
      },
      "aggs": {
        "my-sub-agg-name": {
          "avg": {
            "field": "my-other-field"
          }
        }
      }
    }
  }
}

I wanted to perform the above elastic search query in java using the new elasticsearch java api client . I am using elastic search version 7.16 . Can someone please help me in building the same query using the new elasticsearch java api client .

Arun
  • 11
  • 2

1 Answers1

0

Here's one you can try:

SearchResponse<YourClass> response = client.search(b -> b
                .index("your_index")                 
                .aggregations("first_agg",a->a.terms(t->t.field("my_field"))
                 .aggregations("sub_agg", s->s.avg(av->av.field("other_field")))),
                YourClass.class);
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Pompompurin
  • 165
  • 3
  • 14