最近从同事那里学到了很多ES查询的新姿势,总结一波.
总和桶聚合(Sum Bucket Aggregation)
使用场景: 获取某分组条件下所有桶的指定度量的和
比如: 根据某个条件分组,获取前1000条数据出现的数量和.
可以用笨办法定义变量,循环遍历分组,拿到count再求和的方式,但不够逼格,既然ES提供了方法,直接调用即可.
传送门:https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/36aggregationsju-he-fen-679029/363guan-dao-ju-540828-pipeline-aggregations/zong-he-tong-ju-540828-sum-bucket-aggregation
例1-DSL写法:
"aggs": {"all": {"terms": {"field": "topics","size": }
},"sum":{"sum_bucket":{"buckets_path":"all>_count"}
}
}
结果:
"aggregations": {
"all": {
"doc_count_error_upper_bound": 11656,
"sum_other_doc_count": 2575137,
"buckets": [
{
"key": "xx",
"doc_count": 129636