Sorting (grouping) result hits after fetch - is it possible?


What i want is to get first few results from ES and after that i want to group them by some scripted field or type.
Is it possible?

I have documents:
{"user": {"id": "1", "name": "John Smith1"}} index: index1, type: type1
{"user": {"id": "2", "name": "John Smith2"}} index: index1, type: type2
{"user": {"id": "3", "name": "John Smith3"}} index: index1, type: type1
{"user": {"id": "4", "name": "John Smith4"}} index: index1, type: type2

and a query:
"size" : 2,
"query": {
"bool": {
"must": [{ "match": { "": "John"}}]

and that query gives me first two documents

what i want is to sort those 2 documents by type or some scripted field i will add
how to do this?

Regards, Wojtas