首页 > 解决方案 > Elasticsearch - buckets_path 聚合项

问题描述

我正在尝试对术语进行 AVG 计数(count_bucket),但出现错误:

"buckets_path must reference either a number value or a single value numeric metric aggregation, got: [Bucket] at aggregation [count_bucket]"

这是我的查询:count_bucket 是 count term agg 并且我想要这个的平均值。

{
    "size": 0,
    track_total_hits: true,
    "query": {
        "bool": {
            "must": queries,
            "must_not": {
                "exists": {
                    "field": supress
                }
            }
        }
    },
    "aggs": {
        "count_bucket": {
            "terms": {
                "field": field,
                "size": 500,
                "order": {
                    "_key": "desc"
                }
            }
        },
        "avg_count": {
              "avg_bucket": {
                "buckets_path": "count_bucket" 
              }
        }
    }
}

标签: elasticsearch

解决方案


错误消息中描述了该问题:术语聚合不返回单个值,这与其他聚合(例如sumor )不同value_count。因此,您无法avg_bucket对这些存储桶执行聚合。

一个好的解决方案是在每个存储桶上使用value_count聚合来返回单个值,然后您可以在该值上进行avg_bucket聚合:

这是它的外观:

{
    "query": {
        "bool": {
            "must": queries,
            "must_not": {
                "exists": {
                    "field": supress
                }
            }
        }
    },
    "aggs": {
        "count_bucket": {
            "terms": {
                "field": field,
                "size": 500,
                "order": {
                    "_key": "desc"
                }
            },
            "aggs": {
                "docs_count": {
                    "value_count": {
                        "field": field
                    }
                }
            }
        },
        "avg_count": {
                "avg_bucket": {
                    "buckets_path": "count_bucket>docs_count"
                }
            }
    }
}

推荐阅读