GET testusage/_search?size=0
{
"query": {
"bool": {
"filter": {"range": {
"str_login_time": {
"gte": "2017-03-16",
"lte": "2018-03-17"
}
}}
}
},
"aggs": {
"By Time": {
"terms": {
"field": "str_time.keyword",
"size": 24
}
}
}
}
The output:
"aggregations": {
"By Time": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "12:00AM-1:00PM",
"doc_count": 67222
},
{
"key": "11:00AM-12:00AM",
"doc_count": 60576
},
{
"key": "1:00PM-2:00PM",
"doc_count": 55583
},
{
"key": "4:00PM-5:00PM",
"doc_count": 49822
},
{
"key": "5:00PM-6:00PM",
"doc_count": 49182
},
{
"key": "6:00PM-7:00PM",
"doc_count": 36849
},
{
"key": "3:00PM-4:00PM",
"doc_count": 33488
},
{
"key": "10:00AM-11:00AM",
"doc_count": 33424
},
{
"key": "2:00PM-3:00PM",
"doc_count": 25477
},
{
"key": "7:00PM-8:00PM",
"doc_count": 24724
},
{
"key": "8:00PM-9:00PM",
"doc_count": 16351
},
{
"key": "9:00AM-10:00AM",
"doc_count": 14356
},
{
"key": "9:00PM-10:00PM",
"doc_count": 10088
},
{
"key": "8:00AM-9:00AM",
"doc_count": 9313
},
{
"key": "10:00PM-11:00PM",
"doc_count": 5788
},
{
"key": "7:00AM-8:00AM",
"doc_count": 5738
},
{
"key": "11:00PM-12:00PM",
"doc_count": 3669
},
{
"key": "6:00AM-7:00AM",
"doc_count": 2671
},
{
"key": "0:00AM-1:00AM",
"doc_count": 1763
},
{
"key": "5:00AM-6:00AM",
"doc_count": 1242
},
{
"key": "1:00AM-2:00AM",
"doc_count": 828
},
{
"key": "2:00AM-3:00AM",
"doc_count": 481
},
{
"key": "4:00AM-5:00AM",
"doc_count": 468
},
{
"key": "3:00AM-4:00AM",
"doc_count": 383
}
]
}
}
I need to order the result by key
as
1:00AM-2:00AM
2:00AM-3:00AM
3:00AM-4:00AM
4:00AM-5:00AM
5:00AM-6:00AM
6:00AM-7:00AM
7:00AM-8:00AM
8:00AM-9:00AM
9:00AM-10:00AM
10:00AM-11:00AM
11:00AM-12:00AM
12:00AM-1:00PM
1:00PM-2:00PM
2:00PM-3:00PM
3:00PM-4:00PM
4:00PM-5:00PM
5:00PM-6:00PM
6:00PM-7:00PM
7:00PM-8:00PM
8:00PM-9:00PM
9:00PM-10:00PM
10:00PM-11:00PM
11:00PM-12:00PM
0:00AM-1:00AM