title: How to filter an elasticsearch global aggregation?
body: <p>What I want to achieve: I want my "age" aggregation to not be filtered by the query filter and I want to be able to apply filters to it.</p>
  
  <p>So if I start with this query:</p>
  
  <pre><code>{
      "query":{
          "filtered":{
              "filter":{ "terms":{ "family_name":"Brown" } } //filter_1
          }
      },
      "aggs":{
          "young_age":{
              "filter":{
                  "range":{ "lt":40, "gt":18 } //filter_2
              },
              "aggs":{
                  "age":{
                      "terms":{
                          "field":"age"
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>My aggregation "young_age" will be filtered by both filter_1 and filter_2. I don't want my aggregation to be filtered by filter_1.</p>
  
  <p>As I was looking into the documentation, I thought global aggregation would solve my problem, and I wrote that query:</p>
  
  <pre><code>{
      "query":{
          "filtered":{
              "filter":{ "terms":{ "family_name":"Brown" } } //filter_1
          }
      },
      "aggs":{
          "young_age":{
              "global":{}, //&lt;----------- add global
              "filter":{
                  "range":{ "lt":40, "gt":18 } //filter_2
              },
              "aggs":{
                  "age":{
                      "terms":{
                          "field":"age"
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>But then elastic search complains about my filter_2:</p>
  
  <p>"""
  Found two aggregation type definitions [age] in [global] and [filter]
  """</p>
  
  <p>And of course if I remove the filter_2:</p>
  
  <pre><code>{
      "query":{
          "filtered":{
              "filter":{
                  "terms":{
                      "family_name":"Brown"
                  }
              }
          }
      },
      "aggs":{
          "young_age":{
              "global":{},
              "aggs":{
                  "age":{
                      "terms":{
                          "field":"age"
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Then my aggregation won't be filtered by filter_1 (as expected).</p>
  
  <p>So how am I suppose to apply filter_2 to my global aggregation? Or how am I supposed to achieved that? I remember writing something similar with the facet filters...</p>