ElasticSearch将其余查询转换为运输客户端Java代码

时间:2019-06-07 04:17:34

标签: elasticsearch elasticsearch-plugin elasticsearch-aggregation

感谢您的帮助

我创建了一个弹性搜索_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();

能否让我知道我如何获得相同的结果?

再次感谢!!!

2 个答案:

答案 0 :(得分:2)

这是一个好的开始,但是您只需要向嵌套的scope_term聚合中添加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);

答案 1 :(得分:0)

我找到了以下解决方案:

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