1

So I would like to add a couple more filters to the aggregate filter for the "inner" portion of the aggregate section. The other two filters I need to add are in the query section. I was able to get this code to work correctly, it just needs the second and third nested filters added from the first section down into the aggregate area, where I am only filtering by the "givingMatch.db_type" terms currently.

Here is the current code that just needs the additional filters added:

    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
}

Giving these results:

...."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 Answer 1

6

Hopefully this will save someone else a few hours. To add multiple filters, the aggregate section would become:

GET materielelectrique_search_alias/product/_search?explain=false
{
  "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"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

This allows for multiple filters within the nested aggs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.