感谢您的帮助
我创建了一个弹性搜索_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();
能否让我知道我如何获得相同的结果?
再次感谢!!!
答案 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);