我想做一个过滤器聚合,嵌套聚合,然后是3个同级聚合。我已经对此进行了测试,并且可以使用REST API进行工作。这是我使用的查询:
{
"aggs": {
"filter_agg": {
"filter": {
...
},
"aggs": {
"nested_agg": {
"nested": {
"path": "myProperty"
},
"aggs": {
"sibling_agg_1": {
...
},
"sibling_agg_2": {
...
},
"sibling_agg_3": {
...
}
}
}
}
}
}
}
现在,我想使用代码库中的Java API来实现此查询。我不确定如何进行同级汇总,因为唯一可用的方法FilterAggregationBuilder
是subAggregation()
。
在Java API的上下文中,这些兄弟聚合是子聚合吗?
我开始编写的Java代码如下所示:
client.prepareSearch(index)
.setTypes(types)
.setSearchType(SearchType.COUNT)
.addAggregation(myFilterAggregation
.subAggregation(AggregationBuilders.nested("nested_agg").path("myProperty"))
然后,我唯一可以继续做的就是构建more-SearchRequestBuilder
是subAggregation
实现此目标的正确方法吗?在这种情况下,REST API和Java API似乎有所不同。
您可以通过向子聚合中添加子聚合来做到这一点nested
(请注意,我选择了一种terms
聚合,因为从查询DSL尚不清楚它使用了哪种特定的聚合类型作为同级聚合):
// create the nested aggregation
NestedBuilder nestedAgg = AggregationBuilders.nested("nested_agg").path("myProperty");
// now add each of your sub-aggregation to it
nestedAgg.subAggregation(AggregationBuilders.terms("sibling_agg_1").field("field1"));
nestedAgg.subAggregation(AggregationBuilders.terms("sibling_agg_2").field("field2"));
nestedAgg.subAggregation(AggregationBuilders.terms("sibling_agg_3").field("field3"));
// finally add the nested aggregation to the filter aggregation
client.prepareSearch(index)
.setTypes(types)
.setSearchType(SearchType.COUNT)
.addAggregation(myFilterAggregation
.subAggregation(nestedAgg);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句