ElasticSearch 将 Rest 查询转换为传输客户端 Java 代码

ElasticSearch Convert Rest Query TO Transport Client Java Code

在此先感谢您的帮助,

我创建了一个弹性搜索 _search 查询,如下所示:

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

现在我想在 Java Elastic Search Transport Client 6.2 中转换此查询。我尝试使用以下代码,但没有返回相同的结果。 :

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

能否告诉我如何获得相同的结果?

再次感谢!!!

这是一个好的开始,但您只需要将 scope_term 作为 sub-aggregation 添加到嵌套的 attrs_root 聚合中:

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

我找到了如下解决方案:

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