我想获得子聚合的总和。例如,我按智能手机分组,按运营商分组,然后是该运营商的平均价格。我想获得特定智能手机所有运营商的所有价格总和。所以基本上,我想要这样的东西:
{
"aggs": {
"group_by_smartphones": {
"terms": {
"field": "smartphone",
"order": {
"_term": "asc"
},
"size": 200
},
"aggs": {
"group_by_sum": {
"sum": {
"field": "price"
},
"aggs": {
"group_by_carrier": {
"terms": {
"field": "carrier",
"order": {
"group_by_avg": "desc"
}
},
"aggs": {
"group_by_avg": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}
}
}
除了,当我这样做时,我得到这个错误:
"type": "aggregation_initialization_exception", "reason": "Aggregator [group_by_sum] of type [sum] cannot accept sub-aggregations"
我该如何解决才能获得每部智能手机所有价格的总和?
最佳答案
你快到了,实际上 sum
和 group_by_carrier
子聚合都需要处于同一级别:
{
"aggs": {
"group_by_smartphones": {
"terms": {
"field": "smartphone",
"order": {
"_term": "asc"
},
"size": 200
},
"aggs": {
"sum_prices": {
"sum": {
"field": "price"
}
},
"group_by_carrier": {
"terms": {
"field": "carrier",
"order": {
"group_by_avg": "desc"
}
},
"aggs": {
"group_by_avg": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}
关于search - 获取总和子聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45150450/