title: Elasticsearch - How to filter nested aggregation bucket?
body: <p>I'm trying to filter buckets for nested aggregations.</p>
  
  <p>Mapping:</p>
  
  <pre><code>{
    "dev.directory.3" : {
      "mappings" : {
        "profile" : {
          "properties" : {
            "events" : {
              "type" : "nested",
              "properties" : {
                "id" : {
                  "type" : "integer"
                },
                "name" : {
                  "type" : "string",
                  "index" : "not_analyzed"
                },
              }
            },
            "title" : {
              "type" : "string"
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>Index data:</p>
  
  <pre><code>"hits" : {
      "total" : 1,
      "max_score" : 1.0,
      "hits" : [ {
        "_index" : "dev.directory.3",
        "_type" : "profile",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "title" : "Project manager",
          "events" : [ 
            {
              "id" : 1,
              "name" : "Event A",
            }, 
            {
              "id" : 2,
              "name" : "Event B",
            },
            {
              "id" : 3,
              "name" : "Event C",
            },
            {
              "id" : 4,
              "name" : "Event D",
            } 
          ],
        }
      }
    ]
  }
  </code></pre>
  
  <p>I'm using this query and aggregation definition</p>
  
  <pre><code>{
      "query": {
          "nested": {
              "path": "events",
              "query": {
                  "bool": {
                      "filter": [{
                          "terms": {
                              "events.id": [1, 2]
                          }
                      }]
                  }
              },
              "inner_hits": {}
          }
      },
      "aggs": {
          "events.name12": {
              "filter": {},
              "aggs": {
                  "inner": {
                      "nested": {
                          "path": "events"
                      },
                      "aggs": {
                          "events.name": {
                              "terms": {
                                  "field": "events.name"
                              }
                          },
                          "events.name_count": {
                              "cardinality": {
                                  "field": "events.name"
                              }
                          }
                      }
                  }
              }
          }
      },
      "size": 20,
      "_source": ["email", "company_name", "events"]
  }
  </code></pre>
  
  <p>What I am getting is from aggregation result is:</p>
  
  <pre><code>"aggregations": {
      "events.name12": {
        "doc_count": 2,
        "filtered": {
          "doc_count": 4,
          "events.name": {
            "buckets": [
              {
                "key": "Event A",
                "doc_count": 1
              },
              {
                "key": "Event B",
                "doc_count": 1
              },
              {
                "key": "Event C",
                "doc_count": 1
              },
              {
                "key": "Event D",
                "doc_count": 1
              }
            ]
          },
          "events.name_count": {
            "value": 4
          }
        }
      }
  </code></pre>
  
  <p>I struggling with filtering buckets list only to provided event ids, so the result should be like: </p>
  
  <pre><code>"aggregations": {
      "events.name12": {
        "doc_count": 2,
        "filtered": {
          "doc_count": 2,
          "events.name": {
            "buckets": [
              {
                "key": "Event A",
                "doc_count": 1
              },
              {
                "key": "Event B",
                "doc_count": 1
              }
            ]
          },
          "events.name_count": {
            "value": 2
          }
        }
      }
  </code></pre>