title: Group by term and get list of array keys and their count
body: <p>in elasticsearch i got several hundred thousand documents with roughly this kind of structure:</p>
  
  <pre><code>{
    "script": "/index.html",
    "query": {
      "ab": "hello",
      "cd": "world",
      "ef": "123"
  }
  </code></pre>
  
  <p>The url "<a href="http://localhost/index.html?ab=hello&amp;cd=world&amp;ef=123" rel="nofollow">http://localhost/index.html?ab=hello&amp;cd=world&amp;ef=123</a>" is parsed into it. "script" only contains the path and the target script - no query at all.
  The query array does not contain the same list of keys and of course different values, which doesn't matter at the moment at all.</p>
  
  <p>I know, i am able to get a distinct list of "script" with:</p>
  
  <pre><code>{
    "aggregations": {
      "my_agg": {
        "terms": {
          "field": "script.raw"
        }
      }
    }
  }
  </code></pre>
  
  <p>which results into multiple buckets like</p>
  
  <pre><code>"buckets": [
  {
      "key": "/index.html",
      "doc_count": 123456
  },
  {
      "key": "/hello.html",
      "doc_count": 1456
  },
  ...
  </code></pre>
  
  <p>My question: Is there a way to get additionally a list and count of all query <strong>keys</strong>, which are occurring in the different urls?</p>
  
  <p>Something like:</p>
  
  <pre><code>"buckets": [
  {
      "key": "/index.html",
      "doc_count": 123456,
      "query_key_count": {
        "ab": 33456,
        "cd": 3456,
        "ef": 456,
        "gh": 56,
        "ij": 6
      }
  },
  {
      "key": "/hello.html",
      "doc_count": 1456,
      "query_key_count": {
        "zy": 156,
        "gh": 6
      }
  },
  ...
  </code></pre>
  
  <p>Thanks alot!!</p>