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

时间:2019-01-15 07:26:40

标签: elasticsearch elasticsearch-aggregation

我想写下一个弹性聚合,该聚合仅在其内部存储桶的长度大于1时才返回键。

"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
                }
            }
        }
    }
}

这是我的汇总,它返回:

"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
                        }
                    ]
                }
            }
       ]
    }
 }

仅在产品定义存储桶中有两个以上键的情况下,才需要产品定义中的键。在此示例中,它应该仅返回2和3.键,而不返回1,因为1.键的存储桶仅包含1个键,即

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

1 个答案:

答案 0 :(得分:1)

您可以利用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"
                }
            }
        }
    }
}