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

**URL:** <https://discuss.elastic.co/t/sorting-grouping-result-hits-after-fetch-is-it-possible/38956>\
**Category:** Elasticsearch\
**Created:** [January 12, 2016, 9:18am UTC](https://discuss.elastic.co/t/sorting-grouping-result-hits-after-fetch-is-it-possible/38956 "2016-01-12T09:18:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wkrugiolka](https://avatars.discourse-cdn.com/v4/letter/w/f475e1/32.png) [@wkrugiolka](https://discuss.elastic.co/u/wkrugiolka)\
**Post date:** [January 12, 2016, 9:18am UTC](https://discuss.elastic.co/t/sorting-grouping-result-hits-after-fetch-is-it-possible/38956/1 "2016-01-12T09:18:59Z")

</div>

Hi,

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": { "[user.name](http://user.name)": "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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:25pm UTC](https://discuss.elastic.co/t/sorting-grouping-result-hits-after-fetch-is-it-possible/38956/2 "2017-07-05T23:25:15Z")

</div>


