Aggregate over an array

I actually don't know about any examples. There are probably some examples on the Internet somewhere if you go look for them though.

If you're asking about scripted metric aggs, I don't think they are possible in Kibana. The JSON input simply lets you add additional parameters to the existing aggregation, and we don't offer scripted metrics as an option.

You might be able to do what you're trying to do with scripted fields though. Scripted fields are different, they are added as fields to your index pattern. Out of the box, the are limited to the Lucene syntax, which only supports numbers. But, if you enable groovy scripting, you can give you field a custom script type. See the script fields, scripts in terms aggs and scripting docs to get started.