【问题标题】:ElasticSearch filtering results according buckets length ( number of unique keys in a bucket)ElasticSearch 根据桶长度过滤结果(桶中唯一键的数量)
【发布时间】:2019-01-15 07:26:40
【问题描述】:

我想写一个弹性聚合,只有当它的内部桶的长度大于 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
              }
            ]

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation


    【解决方案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"
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 我试过了,但它给出了错误 "type": "illegal_argument_exception", "reason": "[script] unknown field [source], parser not found" 顺便说一下我使用弹性搜索版本5.5
    • 5.5.好的。我已经修复了我的答案,请再试一次
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多