title: Elasticsearch - get count of child docs, even if count is zero
body: <h3>Objective: execute 1 search against documents of Parent type, and include in the results the count of children for each parent document.</h3>
  
  <p>(Elasticsearch v5) </p>
  
  <p>Data model has 2 document types: Parent and Child. </p>
  
  <p>I have found that I can do the following query:</p>
  
  <pre><code>GET /stack/parent_doc/_search/
  {
    "query": {
      "has_child": {
        "type": "child_doc",
        "inner_hits": {
          "_source": false,
          "size": 0
        },
        "query": {
          "match_all": {}
        }
      }
    }
  }
  </code></pre>
  
  <p>and I get back all parents <em>which have at least one child</em> and their counts of child documents, as below.  This is pretty close, but I also want to have <strong>parents which have no children included.</strong></p>
  
  <pre><code>{
      "took": 4077,
      "timed_out": false,
      "_shards": {
          "total": 20,
          "successful": 20,
          "failed": 0
      },
      "hits": {
          "total": 4974405,
          "max_score": 1,
          "hits": [{
                  "_index": "stack",
                  "_type": "parent_doc",
                  "_id": "f34e4848-fd63-35a3-84d3-82cbc8796473",
                  "_score": 1,
                  "_source": {
                      "field": "value"
                  },
                  "inner_hits": {
                      "child_doc": {
                          "hits": {
                              "total": 1,
                              "max_score": 0,
                              "hits": []
                          }
                      }
                  }
              },
              {
                  "_index": "stack",
                  "_type": "parent_doc",
                  "_id": "f34e1ece-2274-35f6-af37-37138825db20",
                  "_score": 1,
                  "_source": {
                      "field": "value"
                  },
                  "inner_hits": {
                      "child_doc": {
                          "hits": {
                              "total": 5,
                              "max_score": 0,
                              "hits": []
                          }
                      }
                  }
              }
          ]
      }
  }
  </code></pre>
  
  <p>If I remove the <code>match_all</code> part of the query, then ES seems to ignore the <code>has_child</code> clause entirely, returning all Parent documents regardless of whether or not they have children (which is what I want) but without the <code>inner_hits</code>, so I don't get the count.  </p>
  
  <pre><code>  "query": {
      "match_all": {}
    }
  </code></pre>
  
  <p>Is there a way to do this in a single query?</p>