title: Obtaining boolean clause match counts in Elasticsearch
body: <p>We have some Elasticsearch queries that take the following form:</p>
  
  <pre><code>{
    "query": {
      "bool": {
        "should": [
          {
            "query_string": {
              "default_field": "content",
              "query": "Lorem ipsum dolor sit amet"
            }
          },
          {
            "query_string": {
              "default_field": "content",
              "query": "Nunc ac auctor massa"
            }
          }
        ]
      }
    }
  }
  </code></pre>
  
  <p>We'd like to surface the hit counts on each boolean clause. Our current brute force approach has been to execute a secondary multi-search under the covers, splitting each clause into its own separate query to get the individual counts. This can get very expensive as we support up to 50 of these clauses which can potentially result into another 50 queries executed behind the scenes.</p>
  
  <p>We've looked for alternative ways to extract counts such as <a href="https://stackoverflow.com/questions/7896183/get-matched-terms-from-lucene-query">Get matched terms from Lucene query</a> or 
  <a href="https://stackoverflow.com/questions/2851473/lucene-get-matched-terms-in-query">lucene get matched terms in query</a> but all of them involve bean counting the actual hits. This is prohibitive as we can potentially have thousands of them.</p>
  
  <p>Is there another more efficient approach/technique (preferably in Elasticsearch) for getting those counts that we might have missed?</p>