title: Searching objects having all nested children matching a given query in Elasticsearch
body: <p>Given an object with the following mapping:</p>
  
  <pre><code>{
      "a": {
          "properties": {
              "id": {"type": "string"}
              "b": {
                  "type": "nested",
                  "properties": {
                      "key": {"type": "string"}
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>I want to retrieve all the instances of this object having all nested children matching a given query.</p>
  
  <p>For example, suppose I want to retrieve all the instances having all children with "key" = "yes".
  Given the following instances:</p>
  
  <pre><code>{
      "id": "1",
      "b": [
          {
              "key": "yes"
          },
          {
              "key": "yes"
          }
      ] 
  },
  {
      "id": "2",
      "b": [
          {
              "key": "yes"
          },
          {
              "key": "yes"
          },
          {
              "key": "no"
          }
      ] 
  },
  </code></pre>
  
  <p>I want to retrieve only the first one (the one with "id" = "1").</p>
  
  <p>Both using filters or queries is fine to me.
  I already tried to use the "<a href="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-not-filter.html" rel="nofollow">not filter</a>" and the "<a href="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-filter.html" rel="nofollow">must_not bool filter</a>". The idea was to use a double negation to extract only objects that doesn't have fields that are different to the given one.
  However, I was not able to write down this query correctly.</p>
  
  <p>I realize that this is not a common query for a search engine, but, in my case, it can be useful.</p>
  
  <p>Is it possible to write this query ("forall nested query") using nested objects?
  In case it is not, would it be possible to write this query using parent-child?</p>
  
  <p><strong>Update</strong></p>
  
  <p>Andrei Stefan gave a good answer in case we know all the values of "key" that we want to avoid, ("no", in the example).</p>
  
  <p>I am interested also in the case you don't know the values you want to avoid, and you just want to match nested object with "key"="yes".</p>