How to show the average of one entry in a multivalued Elasticsearch field?

Hi All,

I’m trying to get a graph (Table) of a set of entries in an Elasticsearch index, where each entry has a number of sub entries, which in turn again have a number of entries. For those second level entries I am trying to get an average on a couple of the field values, where the name has a specific value. To try and make it clearer with an example entry set, please consider the following:

        [{
        "id": "1",
        "entryName": "entry1",
        "modules": [
            {"module1": {"executions": [
                {
                    "name": "execution1",
                    "duration": 10
                },
                {
                    "name": "execution2",
                    "duration": 5
                },
                {
                    "name": "execution3",
                    "duration": 51
                }
            ]}},
            {"module2": {"executions": [
                {
                    "name": "execution1",
                    "duration": 3
                },
                {
                    "name": "execution2",
                    "duration": 88
                },
                {
                    "name": "execution3",
                    "duration": 11
                }
            ]}}
        ]
    },
    {
        "id": "2",
        "entryName": "entry1",
        "modules": [
            {"module1": {"executions": [
                {
                    "name": "execution1",
                    "duration": 44
                },
                {
                    "name": "execution2",
                    "duration": 2
                },
                {
                    "name": "execution3",
                    "duration": 91
                }
            ]}},
            {"module2": {"executions": [
                {
                    "name": "execution1",
                    "duration": 9
                },
                {
                    "name": "execution2",
                    "duration": 110
                }
                ,
                {
                    "name": "execution3",
                    "duration": 513
                }
            ]}}
        ]
    }]

From this set, let’s say I need to calculate the average of the duration for one execution, namely execution2 and somehow get that into a graph and then group it by the entryName or module entries.

I have tried to use bucket scripts for this, but I could not get it working. Also, because I couldn’t really verify whether the calculated averages are actually correct. Also - I’m not really sure if Elastic’s aggregation functionality can actually work?

I’m using Grafana 6.0.0 on Elasticsearch 6.7.0.