如何动态修改子聚合?
val nestedAggregation = termsAggregation
.subAggregation(filterAggregation
.subAggregation(Sum1Aggregation)
.subAggregation(Sum2Aggregation)
.subAggregation(Sum3Aggregation)
)
是否可以在创建上面的嵌套的Ajregation对象后修改它? 例如,如果我想在创建对象后在过滤器聚合中添加另一个聚合,该怎么办?
nestedAggregation.filterAggregation.subAggregation(Avg1Aggregation) -- This didn't work
-----我的代码------
val filterAggregation = AggregationBuilders
.filter(aggName)
.filter(QueryBuilders.boolQuery
.must(rangeQuery(fieldName)
.gte(startDate)
.lt(endDate)))
val nestedAggregation = terms1Aggregation
.subAggregation(filterAggregation
.subAggregation(Sum1Aggregation)
.subAggregation(Sum2Aggregation)
.subAggregation(Sum3Aggregation)
.subAggregation(Avg1Aggregation)
)
shouldSortByHours(sortKey) match {
case true => nestedAggregation
case false => {
// This is where am planning to add the avg1 aggregation to filteraggregation
}
}
更新#2 - [可能的解决方案]
if (!shouldSortByHours(sortKey)){
filterAggregation.subAggregation(Avg1Aggregation)
}
val nestedAggregation = activeSubscriptionsList
.subAggregation(filterAggregation
.subAggregation(Sum1Aggregation)
.subAggregation(Sum2Aggregation)
.subAggregation(Sum3Aggregation)
)
答案 0 :(得分:1)
我建议这样的事情:
var filterAggregation = AggregationBuilders
.filter(aggName)
.filter(QueryBuilders.boolQuery
.must(rangeQuery(fieldName)
.gte(startDate)
.lt(endDate)))
.subAggregation(Sum1Aggregation)
.subAggregation(Sum2Aggregation)
.subAggregation(Sum3Aggregation)
shouldSortByHours(sortKey) match {
case true => {
terms1Aggregation.subAggregation(filterAggregation)
}
case false => {
filterAggregation.subAggregation(Avg1Aggregation)
terms1Aggregation.subAggregation(filterAggregation)
}
}