Niko Gamulin
Niko Gamulin

Reputation: 66565

Elasticsearch sort by dates is incorrect

In the Elasticsearch documents, there is a field createdAt defined as follows:

"createdAt" : {
            "type" : "date",
            "format" : "YYYY-MM-DD HH:mm:ss"
          }

When trying to sort the records by date, I get the following order:

  1. 2019-10-01 07:46:22
  2. 2019-10-01 09:06:28
  3. 2019-10-01 10:39:43
  4. 2019-10-01 11:35:00
  5. 2019-10-02 12:27:40
  6. 2019-10-04 00:50:00
  7. 2019-07-05 09:57:15
  8. 2019-11-05 22:58:00
  9. 2019-06-06 12:39:49
  10. 2019-09-06 19:46:00

Does anyone know why the dates are not sorted correctly?

Upvotes: 2

Views: 319

Answers (1)

Val
Val

Reputation: 217304

Your format is wrong, it should be:

"createdAt" : {
        "type" : "date",
        "format" : "yyyy-MM-dd HH:mm:ss"
      }

Lowercase y and d

Upvotes: 4

Related Questions