title: Query on nested type with aggregation on nested types returns unexpected results
body: <p>We are using elasticsearch 5.6.4. As mentioned in the ES <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/_filtering_queries_and_aggregations.html" rel="nofollow noreferrer">documentation</a>, </p>
  
  <blockquote>
    <p>aggregation operates in the context of the query scope, any filter
    applied to the query will also apply to the aggregation.</p>
  </blockquote>
  
  <p>Now, what I have is this : 
  An index with mapping :</p>
  
  <pre><code>{
      "properties":{
          "asset":{
              "properties":{
                  "customerId":{
                      "type":"long"
                  }
              }
          },
          "software":{
              "type": "nested",
              "properties":{
                  "id":{
                      "type":"long"
                  },
                  "name":{
                      "type":"text",
                       "fields": {
                          "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                          }
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>I have created several documents to perform various tests here. Docs are indexed on customerId. I have in all 10 documents each having 2 or more softwares. For testing aggregations on softwares, I created softwares with same ID across multiple documents. e.g. I have software with Id as 12 twice in doc with customerId 1 and 2 and 3. Also, Doc with customerId 2 has two softwares with Id as 12.</p>
  
  <p>So there are 4 softwares with Id as 12 across documents 1, 2 and 3.
  The aggregation result includes only the documents with customerId 1 and not 2 and 3 ,when this query is run :</p>
  
  <pre><code>{
    "query" : {
      "term":{
          "asset.customerId":1
      }
    },
    "aggregations" : {
      "aggs" : {
        "nested" : {
          "path" : "software"
        },
        "aggregations" : {
          "software.id.agg" : {
            "terms" : {
              "field" : "software.id",
              "size" : 10,
              "min_doc_count" : 1,
              "shard_min_doc_count" : 0,
              "show_term_doc_count_error" : false,
              "order" : [
                {
                  "_count" : "desc"
                },
                {
                  "_term" : "asc"
                }
              ]
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>But when the query filter is run on a nested type (software.id), aggregation result includes all the docs (1, 2 and 3) and hence the buckets which should be filtered out because of the query are also present. : </p>
  
  <pre><code>{
    "query" : {
      "nested" : {
              "query" : {
                "match_phrase_prefix" : {
                  "software.id" : {
                    "query" : 12,
                    "slop" : 100,
                    "max_expansions" : 50,
                    "boost" : 1.0
                  }
                }
              },
              "path" : "software",
              "ignore_unmapped" : false,
              "score_mode" : "none",
              "boost" : 1.0
            }
    },
    "aggregations" : {
      "aggs" : {
        "nested" : {
          "path" : "software"
        },
        "aggregations" : {
          "software.id.agg" : {
            "terms" : {
              "field" : "software.id",
              "size" : 10,
              "min_doc_count" : 1,
              "shard_min_doc_count" : 0,
              "show_term_doc_count_error" : false,
              "order" : [
                {
                  "_count" : "desc"
                },
                {
                  "_term" : "asc"
                }
              ]
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>What's the correct way to provide the query filter on nested type so that it is applied on aggregation?</p>