Rowls
Rowls

Reputation: 1

elasticsearch date range to get a date range between today and yesterday

I'm using elasticsearch and I'm trying to get a report from ANY SECOND PAST 10:am the previous day to 10:am the present-day. Please assist, thanks.

I have tried this :

  "@timestamp":{
    "gte": "now-1d/d+10h",
    "lt": "now/d+10h"

Upvotes: 0

Views: 7182

Answers (1)

Bhavya
Bhavya

Reputation: 16172

You need to set the mapping of the @timestamp field according to the format of data you are using. You can refer to this official documentation to know more about date field type.

Adding a working example with index data, mapping, search query and search result

Index Mapping:

{
  "mappings": {
    "properties": {
      "@timestamp": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      }
    }
  }
}

Index Data:

{
  "@timestamp": "2021-05-04 10:11:51"
}
{
  "@timestamp": "2021-05-04 09:11:51"
}
{
  "@timestamp": "2021-05-05 09:11:51"
}
{
  "@timestamp": "2022-05-05 09:11:51"
}

Search Query:

{
  "query": {
    "range": {
      "@timestamp": {
        "gte": "now-1d/d+10h",
        "lt": "now/d+10h"
      }
    }
  }
}

Search Result:

 "hits": [
      {
        "_index": "67392888",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.0,
        "_source": {
          "@timestamp": "2021-05-05 09:11:51"
        }
      },
      {
        "_index": "67392888",
        "_type": "_doc",
        "_id": "4",
        "_score": 1.0,
        "_source": {
          "@timestamp": "2021-05-04 10:11:51"
        }
      }
    ]

Upvotes: 3

Related Questions