-1

Thanks in Advance for Help,

I have created a elastic search _search query as below :

{
    "size" : 0,
  "aggs": {
    "attrs_root": {
      "nested": {
        "path": "tags"
      },
      "aggs": {
        "scope_term": {
          "terms": {
            "field": "tags.scope.keyword"
          },
          "aggs": {
            "tag_term": {
              "terms": {
                "field": "tags.tag.keyword"
              }
            }
          }
        }
      }
    }
  }
} 

Now I want to convert this query in Java Elastic Search Transport Client 6.2 . I tried with below code but it is not returning same results. :

               NestedAggregationBuilder nested = AggregationBuilders.nested("attrs_root", "tags");
        NestedAggregationBuilder subAggregation = nested
                .subAggregation(AggregationBuilders.terms("scope_term").field("tags.scope.keyword"));
        subAggregation = subAggregation.subAggregation(AggregationBuilders.terms("tag_term").field("tags.tag.keyword"));
        requestBuilder.addAggregation(nested);
        response = requestBuilder.execute().actionGet();

Could you please let me know how i can get same results?

Thank Again !!!

Prakash Panjwani
  • 2,540
  • 5
  • 23
  • 34

2 Answers2

2

That's a good start, but you just need to add scope_term as a sub-aggregation to your nested attrs_root aggregation:

NestedAggregationBuilder nested = AggregationBuilders.nested("attrs_root", "tags");
TermsAggregationBuilder field = AggregationBuilders.terms("scope_term").field("tags.scope.keyword");
field.subAggregation(AggregationBuilders.terms("tag_term").field("tags.tag.keyword"));

// add the next line
nested.subAggregation(field);
Val
  • 207,596
  • 13
  • 358
  • 360
  • Hi Val, How cab i set maximum number of result in same example, and if i want to introduce pagination how is it possible with buckets? – Prakash Panjwani Jun 08 '19 at 10:36
  • You can use the size parameter. But if you need pagination, I suggest using the [`composite` aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html) instead – Val Jun 09 '19 at 12:55
0

I found solution as below :

 NestedAggregationBuilder nested = AggregationBuilders.nested("attrs_root", "tags");
            TermsAggregationBuilder field = AggregationBuilders.terms("scope_term").field("tags.scope.keyword");
            field.subAggregation(AggregationBuilders.terms("tag_term").field("tags.tag.keyword"));

            nested.subAggregation(field);

            requestBuilder.addAggregation(nested);
Prakash Panjwani
  • 2,540
  • 5
  • 23
  • 34