Sort on timestamp returns wrong order

Pulling my hair out on this one...

Using ES 2.3

Trying to sort results based on a unix epoch field, but results are being returned in the wrong order... however, they are being sorted (you can reverse the order and get the incorrect results in the reverse order)

E.g, search returns three results

last_action: 1472228572 (181 days ago)
last_action: 1471424370 (190 days ago)
last_action: 1473689679 (164 days ago)

Changing the order to ascending we get

last_action: 1473689679 (164 days ago)
last_action: 1471424370 (190 days ago)
last_action: 1472228572 (181 days ago)

Sort is:

$query['sort'] = [
'last_action' => [
'order' => 'desc',
],
];

Mapping for this field is:

'last_action' => [
'type' => 'date',
'format' => 'epoch_second',
'index' => 'analyzed',
'store' => 'yes'
]

Any idea what could be throwing this off?

There are no exceptions being thrown by the server during storage or mapping...

Marcus

I'm not able to reproduce your issue. Also, I'm not sure where you're getting some of the [ ] syntax from. I'm running Sense from Kibana.

This is what I did:

PUT mypayload
{
    "mappings":{
      "payload_type" : { 
        "properties": {
                  "last_action" : {
"type" : "date",
"format" : "epoch_second",
"index" : "analyzed",
"store" : "yes"
}
        }
}
}
}

Ran the post several times for all your time frames.

POST mypayload/payload_type
{
  "last_action" : 1472228572
}

Then get the output

GET  mypayload/payload_type/_search
{ 
  "sort": [
    {
      "last_action": {
        "order": "desc"
      }
    }
  ]
  }

Output:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": null,
    "hits": [
      {
        "_index": "mypayload",
        "_type": "payload_type",
        "_id": "AVptPpHf-G5KikZv-qhL",
        "_score": null,
        "_source": {
          "last_action": 1487891449
        },
        "sort": [
          1487891449000
        ]
      },
      {
        "_index": "mypayload",
        "_type": "payload_type",
        "_id": "AVptPsb0-G5KikZv-qhM",
        "_score": null,
        "_source": {
          "last_action": 1473689679
        },
        "sort": [
          1473689679000
        ]
      },
      {
        "_index": "mypayload",
        "_type": "payload_type",
        "_id": "AVptP2A4-G5KikZv-qhO",
        "_score": null,
        "_source": {
          "last_action": 1472228572
        },
        "sort": [
          1472228572000
        ]
      },
      {
        "_index": "mypayload",
        "_type": "payload_type",
        "_id": "AVptPyUY-G5KikZv-qhN",
        "_score": null,
        "_source": {
          "last_action": 1471424370
        },
        "sort": [
          1471424370000
        ]
      }
    ]
  }
}

There appears to be no way to close this thread, but I've managed to get to the bottom of this - in summary it was a bug in a third party library, and not elastic search.

I came back to it fresh and found the issue in about 15 minutes. Always the way :confused:

Thanks for your help!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.