向嵌套聚合过滤器添加多个过滤器Elasticsearch

时间:2017-12-05 17:29:49

标签: elasticsearch kibana

所以我想在聚合过滤器中为聚合部分的“内部”部分添加几个过滤器。我需要添加的其他两个过滤器位于查询部分。我能够使这段代码正常工作,它只需要从第一部分向下添加到聚合区域的第二个和第三个嵌套过滤器,我只是通过当前的“givingMatch.db_type”术语进行过滤。

以下是仅需要添加其他过滤器的当前代码:

    GET /testserver/_search
{
    "query": {
        "bool": {
            "filter": [
           {
                    "nested": {
                        "path": "givingMatch",
                        "query": {
                            "bool": {
                                "filter": {
                                    "terms": {
                                        "givingMatch.db_type": [
                                            "FECmatch",
                                            "StateMatch"
                                        ]
                                    }
                                }
                            }
                        }
                    }
                },
                {
                    "nested": {
                        "path": "givingMatch",
                        "query": {
                            "bool": {
                                "filter": {
                                    "range": {
                                        "givingMatch.Status": {
                                            "from": 0,
                                            "to": 8
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                {
                    "nested": {
                        "path": "givingMatch",
                        "query": {
                            "bool": {
                                "filter": {
                                    "range": {
                                        "givingMatch.QualityScore": {
                                            "from": 17
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            ]
        }
    },
    "aggs": {
        "categories": {
            "nested": {
                "path": "givingMatch"
            },
            "aggs": {
              "inner": {
                "filter": {
                  "terms": {
                    "givingMatch.db_type":["FECmatch","StateMatch"]
                  }
                },
                "aggs":{
                  "org_category": {
                    "terms": {
                        "field": "givingMatch.org_category",
                        "size": 1000
                    },
                    "aggs": {
                      "total": {
                        "sum":{
                          "field": "givingMatch.low_gift"
                        }
                      }
                    }
                }
                }
              }

            }
        }
    },
    "size": 0
}

给出这些结果:

...."aggregations": {
    "categories": {
      "doc_count": 93084,
      "inner": {
        "doc_count": 65492,
        "org_category": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "DEM",
              "doc_count": 28829,
              "total": {
                "value": 29859163
              }
            },
            {
              "key": "REP",
              "doc_count": 21561,
              "total": {
                "value": 69962305
              }
            },...

1 个答案:

答案 0 :(得分:1)

希望这可以节省其他人几个小时。要添加多个过滤器,聚合部分将变为:

"aggs": {
        "categories": {
            "nested": {
                "path": "givingMatch"
            },
            "aggs": {
              "inner": {
                "filter": {
                  "bool": {
                    "must": [{
                  "terms": {
                    "givingMatch.db_type":["FECmatch","StateMatch"]
                  }
                },
                {
                                    "range": {
                                        "givingMatch.QualityScore": {
                                            "from": 17
                                        }
                                    }
                                },
                                {
                                    "range": {
                                        "givingMatch.Status": {
                                            "from": 0,
                                            "to": 8
                                        }
                                    }
                                }

                ]
                }
                },
                "aggs":{
                  "org_category": {
                    "terms": {
                        "field": "givingMatch.org_category",
                        "size": 1000
                    },
                    "aggs": {
                      "total": {
                        "sum":{
                          "field": "givingMatch.low_gift"
                        }
                      }
                    }
                }
                }
              }

            }
        }
    }

这允许在嵌套的aggs中使用多个过滤器。