My index has two fields -
- updated which is a date field (eg. "2020-01-04T05:00:06.870000Z")
- numWrites which is a float
I need a query that -
- sorts by date (YYYY-mm-dd) in descending order
- for each day, sorts by numWrites in descending order
Sample data :
"_source": {
"updated": "2020-01-04T05:00:06.870000Z",
"numWrites": 5.0
}
"_source": {
"updated": "2020-01-04T09:00:08.870000Z",
"numWrites": 3.0
}
"_source": {
"updated": "2019-12-04T01:00:06.870000Z",
"numWrites": 15.0
}
"_source": {
"updated": "2019-12-04T04:00:06.870000Z",
"numWrites": 12.0
}
}
The following query sorts by date. But, within the same day, it doesn't sort by numWrites
as expected, as the timestamps within the same day are different. How can I extract date in the from YYYY-mm-dd and then sort by numWrites
within a day ?
Query :
{
"sort":[
{"updated": {"order" : "desc"}},
{"numWrites": {"order" : "desc"}}
]
}
Results :
"_source": {
"updated_time": "2020-01-04T09:00:08.870000Z",
"numWrites": 3.0
}
"_source": {
"updated": "2020-01-04T05:00:06.870000Z",
"numWrites": 5.0
}
"_source": {
"updated_time": "2019-12-04T04:00:06.870000Z",
"numWrites": 12.0
}
"_source": {
"updated_time": "2019-12-04T01:00:06.870000Z",
"numWrites": 15.0
}
If I have understood your question well, you can use a sub aggregation to sort by date first, then by numWrites for each of those days. Here is a suggested solution:
I tried adding two numWrites on the same date. Here is my sample index:
I have two numWrites for the date "2019-12-04T04:00:06.870Z" and the result I obtained is: