Home > database >  Query by timestamp in ElasticSearch
Query by timestamp in ElasticSearch

Time:07-14

I would like to make a query in elastic search such as I only get the last 40 data I have on my database. For the moment my query is such :

  {
    "size": 40,
    "query" : {
      "exists": {
      "field": "transaction.domain"
      }
      "range": {
      "@timestamp" : {
          "from": "now-30mn",
          "to": "now"
      }
    }
    }
  }

Thanks for your help. Victoire

CodePudding user response:

You are almost there, you just need to add sorting on your timestamp field.

below query should work for you.

{
    "size": 40,
    "query": {
        "exists": {
            "field": "transaction.domain"
        },
        "range": {
            "@timestamp": {
                "from": "now-30mn",
                "to": "now"
            }
        }
    },
    "sort": [
        {
            "@timestamp": "desc"
        }
    ]
}
  • Related