Thank you very much for the quick response. The scripted_metric works perfectly!
I needed to also remove duplicate entries (which existed in the data) so my final aggregation was:
"values": {
"scripted_metric": {
"init_script": "state.docs = []",
"map_script": "def values = params['_source']['values'] != null ? new ArrayList(params['_source']['values']) : new ArrayList(); for (v in values) { state.docs.add(new HashMap(v)); }",
"combine_script": "return state.docs",
"reduce_script": "def dedupe = new HashSet(); def docs = []; for (s in states) {for (d in s) { if (!dedupe.contains(d)) { dedupe.add(d); docs.add(d); } } } return docs"
}
}
Cheers