ES Query to sort data on Timestamp

I have a very basic requirement. I am indexing the records based on timestamp.

E.g

{ "temperature": [value], "deviceID":[value] , Timestamp: [ts]}
{ "temperature": [value], "deviceID":[value],Timestamp: [ts] }
{ "temperature": [value], "deviceID":[value],Timestamp: [ts] }

These record/events are added every 1 minute. I would like to query ES and find out the last 10 records ordered by timestamp for a particular deviceID. So, the first record should be t-10, then t-9, then t-8, ...

{

"sort":[  {"Timestamp": { "order" : "asc" }} ],
  "query": {
    "bool": {
      "must": {
        "match": {
          "deviceID": 1
        }
      },
      "filter": {
        "range": {
          "Timestamp": {
            "gte": "now-10m"
          }
        }
      }
    }
},
"_source" : [ "temperature","deviceID","Timestamp"]
}

I realized that every time it gives different results ... ( though it is sorted based on Timestamp ). It is not giving any definitive result if i run it multiple time.

Am i missing something obvious ?

Please advise as i am not an expert in ES query DSL.Preformatted text

1 Like

Hey,

can you be more specific in what way the results are diverting? Do you have results with the same timestamp and thus the sorting is not stable?

--Alex

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