# Sort (or group) results from query based on a field

**URL:** <https://discuss.elastic.co/t/sort-or-group-results-from-query-based-on-a-field/18745>\
**Category:** Elasticsearch\
**Created:** [July 19, 2014, 12:24am UTC](https://discuss.elastic.co/t/sort-or-group-results-from-query-based-on-a-field/18745 "2014-07-19T00:24:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Liang](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Michael\_Liang](https://discuss.elastic.co/u/Michael_Liang)\
**Post date:** [July 19, 2014, 12:24am UTC](https://discuss.elastic.co/t/sort-or-group-results-from-query-based-on-a-field/18745/1 "2014-07-19T00:24:57Z")

</div>

I have a bunch of entries In my elasticsearch db, each with keys  
'hostname', 'time\_recorded', 'status', whose values are of type string,  
int, string.

I want to create a query that prints out each entry such that it will be  
"sorted" or "grouped by" hostname. I.e.  
host1  
host1  
host1  
host2  
host3  
host3  
...  
etc

How would I do this? I tried using sort:  
query = {  
'fields': ['time\_recorded', 'hostname', 'status', 'dbg\_str'],  
'query': {  
'bool': {  
'minimum\_should\_match': 3,  
'should': [  
{  
'term': {  
'\_type': 'host\_history'  
}  
},  
{  
'range': {  
'time\_recorded': {  
'gte': 1405628341.904379,  
'lte': 1405700341.904379  
}  
}  
}  
]  
},  
},  
'size': 20  
'sort': [  
'hostname',  
{ 'time\_recorded': 'asc'},  
]  
}

The only important part is the 'sort' part of my query. I look at the  
results but it is not sorted based on hostname first, only on time recorded.  
I tried {'hostname':'asc'} but it is still only sorted based on time. Can  
someone help me?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/9c4635f0-5a1e-4a51-b265-5a2d01a05a13%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9c4635f0-5a1e-4a51-b265-5a2d01a05a13%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Michael\_Liang](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Michael\_Liang](https://discuss.elastic.co/u/Michael_Liang)\
**Post date:** [July 29, 2014, 11:46pm UTC](https://discuss.elastic.co/t/sort-or-group-results-from-query-based-on-a-field/18745/2 "2014-07-29T23:46:20Z")

</div>

Resolved this issue, i had to set mappings for the field 'hostname' to be  
not\_analyzed.

On Friday, July 18, 2014 5:24:57 PM UTC-7, Michael Liang wrote:

> I have a bunch of entries In my elasticsearch db, each with keys  
> 'hostname', 'time\_recorded', 'status', whose values are of type string,  
> int, string.
> 
> I want to create a query that prints out each entry such that it will be  
> "sorted" or "grouped by" hostname. I.e.  
> host1  
> host1  
> host1  
> host2  
> host3  
> host3  
> ...  
> etc
> 
> How would I do this? I tried using sort:  
> query = {  
> 'fields': ['time\_recorded', 'hostname', 'status', 'dbg\_str'],  
> 'query': {  
> 'bool': {  
> 'minimum\_should\_match': 3,  
> 'should': [  
> {  
> 'term': {  
> '\_type': 'host\_history'  
> }  
> },  
> {  
> 'range': {  
> 'time\_recorded': {  
> 'gte': 1405628341.904379,  
> 'lte': 1405700341.904379  
> }  
> }  
> }  
> ]  
> },  
> },  
> 'size': 20  
> 'sort': [  
> 'hostname',  
> { 'time\_recorded': 'asc'},  
> ]  
> }
> 
> The only important part is the 'sort' part of my query. I look at the  
> results but it is not sorted based on hostname first, only on time recorded.  
> I tried {'hostname':'asc'} but it is still only sorted based on time. Can  
> someone help me?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/84fac1d5-14f8-4f59-9448-72d7e673e1f6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/84fac1d5-14f8-4f59-9448-72d7e673e1f6%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:12am UTC](https://discuss.elastic.co/t/sort-or-group-results-from-query-based-on-a-field/18745/3 "2017-07-06T01:12:22Z")

</div>


