Reputation: 1193
I need a query that would return data from the last year, grouped by days. So far I have written a query that returns data for the entire year (I hope its correct), but I dont know how to group the data by day.
"query" : {
"range" : {
"timestamp" : {
"gt" : "2017-01-01 00:00:00",
"lt" : "2018-01-01 00:00:00"
}
}
}
Any help would be much appreciated.
I am using Elasticsearch 6.2.2.
Upvotes: 3
Views: 396
Reputation: 1844
You can check date_histogram aggregation
POST my_index/my_type/_search
{
"size": 0,
"aggs": {
"bucketName": {
"date_histogram": {
"field": "timestamp",
"interval": "day",
"min_doc_count": 1,
"format": "yyyy-MM-dd",
"order": {"_key": "desc"}
}
}
}
}
It will return you something like this
{
"took": 23,
"timed_out": false,
"_shards": {
"total": 6,
"successful": 6,
"failed": 0
},
"hits": {
"total": 112233,
"max_score": 0,
"hits": []
},
"aggregations": {
"bucketName": {
"buckets": [
{
"key_as_string": "2018-03-07",
"key": 1520380800000,
"doc_count": 1
},
{
"key_as_string": "2018-03-06",
"key": 1520294400000,
"doc_count": 93
},
{
"key_as_string": "2018-03-05",
"key": 1520208000000,
"doc_count": 99
},
{
"key_as_string": "2018-03-04",
"key": 1520121600000,
"doc_count": 33
},
{
"key_as_string": "2018-03-03",
"key": 1520035200000,
"doc_count": 29
}
]
}
}
}
Upvotes: 3