title: Document count aggregation via query in Elasticsearch (like facet.query in solr)
body: <p>I have a main query and i need the number of matches for a couple of sub-queries.
  In solr words I need a <a href="https://cwiki.apache.org/confluence/display/solr/Faceting#Faceting-Thefacet.queryParameter" rel="nofollow">facet.query</a>. What I am missing is a simple <code>doc_count</code> aggregation like the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html" rel="nofollow">value_count aggregation</a>.</p>
  
  <p>Any suggestions?</p>
  
  <p>I found two possible solutions which I do not like:</p>
  
  <ol>
  <li>Use <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html" rel="nofollow">filter aggregation</a> with <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html" rel="nofollow">value_count</a> metric on <code>_id</code>:</li>
  </ol>
  
  <p>example:</p>
  
  <pre><code>GET _search
  {
      "query": {
          "match_main": {}
      },
      "aggs": {
              "facetvalue1": {
                  "filter": {
                       "bool": { 
                      "should": [
                        {"match": { "name": "fred" }},
                        {"term": { "lastname": "krueger" }}
                       ]
                       }
                  },
                  "aggs": {
                      "count": {
                          "value_count": {
                              "field": "_id"
                          }
                      }
                  }
              },
              "facetvalue2": {
                  "filter": {
                        "term": { "name": "freddy" }
                  },
                  "aggs": {
                      "count": {
                          "value_count": {
                              "field": "_id"
                          }
                      }
                  }
              }
      }
  }
  </code></pre>
  
  <ol start="2">
  <li>Use <a href="https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-multi-search.html" rel="nofollow">Multi Search API</a></li>
  </ol>
  
  <p>example:</p>
  
  <pre><code>GET _msearch
  {"index":"myindex"}
  {"query":{"match_main": {}}}
  {"index":"myindex"}
  {"size": 0, "query":{"match_main": {}}, "filter": {"bool": {"should":[{"match": { "name": "fred" }},{"term": { "lastname": "krueger" }}]}}}
  {"index":"myindex"}
  {"size": 0, "query":{"match_main": {}},"filter": {"term": { "name": "freddy" }}}
  </code></pre>
  
  <p>I see that solution 2 is faster but imagine <code>match_main</code> as complex query!
  So I would prefer solution 1 if there would be an <code>doc_count:{}</code> instead of <code>value_count:{"field":"_id"}</code>.</p>
  
  <p>But back to my basic question: what is the counterpart of the solr <code>facet.query</code> in elasticsearch?</p>