title: ElasticSearch equivalent query for SQL group by multiple columns
body: <p>I have a few million documents with name and version (both of type keyword) as properties in each. What is the equivalent Elastic query for group by name, version?</p>
  
  <p>I have tried the following query:</p>
  
  <pre><code>{
  "size":0,
  "query": {
      "bool": {
          "filter": {
              "range": {
                  "time": {
                      "gte": "2017-01-28",
                      "lte": "2017-02-28"
                  }
              }
          }
      }
  },
  "aggs": {
      "group_by_name": {
          "terms": {
              "field": "name"
          },
          "aggs": {
              "group_by_version": {
                  "terms": {
                      "field": "version"
                  }   
              }
          }   
      }       
  }
  </code></pre>
  
  <p>}</p>
  
  <p>However the results are not same as doing Group by name, version.
  The results are grouped by name and within each group, they are grouped by version.
  How do I modify the above query to group by name, version tuple and return results in descending order?</p>
  
  <p>Your help is greatly appreciated.</p>
  
  <p>Update:
  What i get is:</p>
  
  <pre><code>{
    "took": 1424,
    "timed_out": false,
    "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
    },
    "hits": {
      "total": 115,
      "max_score": 0,
      "hits": []
    },
    "aggregations": {
      "group_by_name": {
        "doc_count_error_upper_bound": 2,
        "sum_other_doc_count": 115,
        "buckets": [
          {
            "key": "product1",
            "doc_count": 50,
            "group_by_version": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 50,
              "buckets": [
                {
                  "key": "1.0",
                  "doc_count": 40
                },
                {
                  "key": "2.0",
                  "doc_count": 10
                },
              ]
            }
          },
          {
            "key": "product3",
            "doc_count": 35,
            "group_by_version": {
              "doc_count_error_upper_bound": 4,
              "sum_other_doc_count": 35,
              "buckets": [
                {
                  "key": "8.0",
                  "doc_count": 20
                },
                {
                  "key": "9.0",
                  "doc_count": 15
                }
              ]
            }
          },
          {
            "key": "product2",
            "doc_count": 30,
            "group_by_version": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 30,
              "buckets": [
                {
                  "key": "4.0",
                  "doc_count": 25
                },
                {
                  "key": "5.0",
                  "doc_count": 5
                }
              ]
            }
          }
        ]
      }
    }
  }
  </code></pre>
  
  <p>What i want is:</p>
  
  <pre><code>name, version   count
  product1 1.0    40
  product2 4.0    25
  product3 8.0    20
  product3 9.0    15
  product1 2.0    10
  product2 5.0     5
  </code></pre>