title: Elasticsearch count occurrences of a value in a nested document
body: <p>Is there a way to count how many times a specific value in a nested type (for instance <code>services.service: "map"</code>) can be found on all of the documents?</p>
  
  <p>A document can look like this:</p>
  
  <pre><code>{
    "services": [
      {
        "service": "map",
        "version": 2,
        "provider": "none"
      },
      {
        "service": "map",
        "version": 0,
        "provider": "none"
      },
      {
        "service": "language",
        "version": 2,
        "provider": "none"
      }
    ],
    "date": "2019-04-26T19:17:20.197Z"
  }
  </code></pre>
  
  <p>Using this mapping:</p>
  
  <pre><code>{
    "mappings": {
      "stat": {
        "properties": {
          "services": {
            "type": "nested",
            "properties": {
              "version": {
                "type": "keyword"
              },
              "provider": {
                "type": "keyword"
              },
              "service": {
                "type": "keyword"
              }
            }
          },
          "date": {
            "type": "date"
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>I got as far as returning the count per document (see query below) but I prefer to get the final amount of occurrences summing values from all of my documents.</p>
  
  <pre><code>{
   "query": {
     "nested": {
       "path": "services",
       "query": {
           "match" : {
               "services.service" : "map"
           }
       },
       "inner_hits": {
         "_source" : false,
         "docvalue_fields" : ["services.service.keyword"]
       }
     }
   }
  }
  </code></pre>