Grafana & Elastic - How to count sub array length

Viewed 392

So I have a document that has two nested arrays i.e.

foo.bars[].baz[]

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:

| bars.id| count| 
| 1      | 10   |
| 2      | 15   |
| 3      | 20   |

What I have tried is the following:

  1. Group by bars.id
  2. Add a Sum metric for bars.baz.id
  3. Override the script value to return 1

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.

| bars.id| count| 
| 1      | 45   |
| 2      | 45   |
| 3      | 45   |

Any help to achieve this would be very helpful.

Now if this can be done I have another more complex problem. I have another collection let's call it bobs that is a child of the root document. Now bobs isn't nested under the bars array but it has a bar.id field. I would also like to sum this based on that i.e.

{
  bobs: [
    {bar_id: 1},
    {bar_id: 2},
  ],
  bars: [
   {id: 1, bazes: []},
   {id: 2, bazes: []}
  ]
}

In this case I would also like in the table:

| bars.id| bobs.count| 
| 1      | 1         |
| 2      | 1         |
| 3      | 0         |

Is this possible?

0 Answers
Related