Templating with metricbeat

This was a tricky question. Might be possible to do it with a script field (something like this) but I’m not sure.