ElasticSearch 根据桶长度(桶中唯一键的数量)过滤结果

ElasticSearch filtering results according buckets length ( number of unique keys in a bucket)

我想写下一个弹性聚合,只有当它的内桶长度大于 1 时,它才 return 是一个键。

"aggs": {
    "product_definitions": {
        "terms": {
            "field": "definition_name",
            "size": 200,
            "exclude": "NO_MATCH",
            "min_doc_count": 5
        },
        "aggs": {
            "product_instances": {
                "terms": {
                    "field": "data_source_name",
                    "size": 100
                }
            }
        }
    }
}

这是我的汇总 returns:

"aggregations": {
    "product_definitions": {
        "doc_count_error_upper_bound": 10,
        "sum_other_doc_count": 29281,
        "buckets": [
            {
                "key": "DANA ANTRİKOT KG",
                "doc_count": 13,
                "product_instances": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                        {
                            "key": "SariyerMarketCom",
                            "doc_count": 13
                        }
                    ]
                }
            },
            {
                "key": "Keskinoğlu Piliç Salam 700G",
                "doc_count": 10,
                "product_instances": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                        {
                            "key": "HappyCenterComTr",
                            "doc_count": 9
                        },
                        {
                            "key": "SanalMarketComTr",
                            "doc_count": 1
                        }
                    ]
                }
            },
            {
                "key": "Doğuş Filiz Çayı 1000 G",
                "doc_count": 9,
                "product_instances": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                        {
                            "key": "HappyCenterComTr",
                            "doc_count": 7
                        },
                        {
                            "key": "SanalMarketComTr",
                            "doc_count": 2
                        }
                    ]
                }
            }
       ]
    }
 }

仅当产品实例存储桶具有两个以上的键时,我才需要产品定义中的键。在此示例中,它应该只有 return 2. 和 3. 键而不是 1. 因为 1. 键的桶只包含 1 个键,即

"buckets": [
              {
                  "key": "SariyerMarketCom",
                   "doc_count": 13
              }
            ]

您可以利用 bucket_selector pipeline aggregations 来实现这一点,如下所示:

"aggs": {
    "product_definitions": {
        "terms": {
            "field": "definition_name",
            "size": 200,
            "exclude": "NO_MATCH",
            "min_doc_count": 5
        },
        "aggs": {
            "product_instances": {
                "terms": {
                    "field": "data_source_name",
                    "size": 100
                }
            },
            "minimum_2": {
                "bucket_selector": {
                    "buckets_path": {
                        "count": "product_instances._bucket_count" 
                    },
                    "script": "params.count >= 2"
                }
            }
        }
    }
}