title: Match document if it contains multiple nested documents in elasticsearch
body: <p>I have a document that contains arrays of nested documents. I have a requirement to return matches if the document contains all of the specified nested documents. </p>
  
  <p>here is the relevant part of the mapping: </p>
  
  <pre><code>"element": {
    "dynamic": "false",
    "properties": {
      "tenantId": {
        "type": "string",
        "index": "not_analyzed"
      },
      "fqn": {
        "type": "string",
        "index": "not_analyzed"
      },
      "id": {
        "type": "string",
        "index": "not_analyzed"
      },
      "name": {
        "type": "string",
        "index": "not_analyzed"
      },
      "type": {
        "type": "string",
        "index": "not_analyzed"
      },
      "location": {
        "type": "string",
        "index": "not_analyzed"
      },
      "tags": {
        "type": "nested",
        "properties": {
          "id": {
            "type": "string",
            "index": "not_analyzed"
          },
          "dataSourceId": {
            "type": "long",
            "index": "not_analyzed"
          },
          "name": {
            "type": "string",
            "index": "not_analyzed"
          },
          "value": {
            "type": "string",
            "index": "not_analyzed"
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>The goal is to be able to return elements that contain all of a list of tags (although the element is permitted to contain additional tags beyond the search requirement).</p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>{
     "query": {
       "bool": {
         "filter": {
            "nested": {
               "path": "tags",
               "query": {
                  "bool": {
                     "must": [
                        {
                          "bool": {
                            "must":{
                               "term": { "tags.name": "name1" },
                               "term": { "tags.value": "value1" }
                            }
                          }
                        },
                        {
                          "bool": {
                            "must":{
                               "term": { "tags.name": "name2" },
                               "term": { "tags.value": "value2" }
                            }
                          }
                        }
                     ]
                  }
               }
            }
         }
      }
    }
  }
  </code></pre>
  
  <p>The problem with this approach is that it returns 0 hits with multiple tag values (it works fine for a single value). I believe that this is because the query is requiring that a tag have multiple names and values in order to match, which obviously can't happen. Does anyone know how to query for elements that contain all of a list of tags? </p>
  
  <p>edit: this is using elasticsearch 5.0</p>