title: Grafana & Elastic - How to count sub array length
body: <p>So I have a document that has two nested arrays i.e.</p>
  
  <p><code>foo.bars[].baz[]</code></p>
  
  <p>I am trying to figure out how I can use graphana to group by bars and give me a count of bar's for each bar. So it would look something like:</p>
  
  <pre><code>| bars.id| count| 
  | 1      | 10   |
  | 2      | 15   |
  | 3      | 20   |
  </code></pre>
  
  <p>What I have tried is the following:</p>
  
  <ol>
  <li>Group by <code>bars.id</code> </li>
  <li>Add a Sum metric for <code>bars.baz.id</code></li>
  <li>Override the script value to return 1</li>
  </ol>
  
  <p>While this does give me the count of the bars, it does so for all bars in the document and not grouped by the bars.id i.e.</p>
  
  <pre><code>| bars.id| count| 
  | 1      | 45   |
  | 2      | 45   |
  | 3      | 45   |
  </code></pre>
  
  <p>Any help to achieve this would be very helpful. </p>
  
  <p>Now if this can be done I have another more complex problem.  I have another collection let's call it <code>bobs</code> that is a child of the root document. Now <code>bobs</code> isn't nested under the <code>bars</code> array but it has a <code>bar.id</code> field. I would also like to sum this based on that i.e.</p>
  
  <pre><code>{
    bobs: [
      {bar_id: 1},
      {bar_id: 2},
    ],
    bars: [
     {id: 1, bazes: []},
     {id: 2, bazes: []}
    ]
  }
  </code></pre>
  
  <p>In this case I would also like in the table:</p>
  
  <pre><code>| bars.id| bobs.count| 
  | 1      | 1         |
  | 2      | 1         |
  | 3      | 0         |
  </code></pre>
  
  <p>Is this possible?</p>