ElasticSearch按术语聚合键进行过滤

时间:2019-02-21 09:00:44

标签: elasticsearch kibana data-storage elasticsearch-6

我有一个ES查询,它以以下格式返回我的数据”

"by_group": {
    "doc_count_error_upper_bound": 55,
    "sum_other_doc_count": 1094497,
    "buckets": [{
        "key": "a838c7df-1ea9-48f1-aa71-69936b54f47d",
        "doc_count": 69,
        "by_subGroup": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [{
                    "key": "k1",
                    "doc_count": 45
                },
                {
                    "key": "k2",
                    "doc_count": 7
                },
                {
                    "key": "k3",
                    "doc_count": 6
                },
                {
                    "key": "k6",
                    "doc_count": 6
                }
            ]
        }
    }]
}

我想使用子组的键来过滤结果(by_group)。 例如 我只想要由key:k1,k2而没有k3的by_group。

是否可以通过这种方式进行过滤?

我当前的查询如下:

{
    "size": 0,
    "query": {
    },
    "aggs": {
            "aggs": {
                "by_group": {
                    "terms": {
                        "field": "field1",
                        "size": 10
                    },
                    "aggs": {
                        "by_subGroup": {
                            "terms": {
                                "field": "field2",
                                "size": 1000
                            }
                        }
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

汇总使用Filter

{
    "size": 0,
    "query": {
    },
    "aggs": {
            "aggs": {
                "by_group": {
                    "terms": {
                        "field": "field1",
                        "size": 10
                    },
                    "aggs": {
                        "by_subGroup": {
                            "filter": {
                                "terms" : {
                                    "field2": ["k1","k2]
                                }
                            },
                            "terms": {
                                "field": "field2",
                                "size": 1000
                            }
                        }
                    }
                }
            }
        }
    }
}