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


(Michael Liang) #1

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/9c4635f0-5a1e-4a51-b265-5a2d01a05a13%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Michael Liang) #2

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/84fac1d5-14f8-4f59-9448-72d7e673e1f6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #3