I have Post model with lastRepliedTime and createdTime attributes. How can I sort this Post by lastRepliedTime, but when its value is null, I use createdTime as sort value?
For example, I have this data:
ID  createdTime          lastRepliedTime
1   2015-05-27 13:48:10  2015-06-04 16:34:43
2   2015-06-09 05:23:38  2015-06-24 00:30:06
3   2015-06-09 17:34:56  NULL
If I sort by ascending order, I want the sort result to be like this:
ID  createdTime          lastRepliedTime
1   2015-05-27 13:48:10  2015-06-04 16:34:43
3   2015-06-09 17:34:56  NULL
2   2015-06-09 05:23:38  2015-06-24 00:30:06
I've found the solution using MySQL in here. But is there any way to sort it using Elasticsearch?
Have you try ?
{
    "sort": [
        { "lastRepliedTime": { "order": "asc" }},
        { "createdTime": { "order": "asc" }}
    ],
    "query" : {
       "match_all" : { }
    }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With