Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timezone of now in elasticsearch

What is the timezone of elasticsearch when I use now ?

{
   "range": {
      "myDateInSomeTimezone": {
         "gt": "now"
      }
   }
},

I am wondering what is the timezone of now:

  • is with my server local timezone?
  • is with same timezone of the field?
  • is with UTC timezone?
like image 475
Alberto Avatar asked Aug 09 '16 16:08

Alberto


People also ask

What timezone does Elasticsearch use?

Tip: Both Elasticsearch and Logstash use the UTC timezone for timestamps. Although this can be changed it is recommended to keep the standard and leave adjustments to Kibana or any other presentation layers you may use.

How do I change timezone in Elasticsearch?

Elasticsearch operates entirely on UTC, you cannot change that.

What is range query in Elasticsearch?

Range Queries in Elasticsearch Combining the greater than ( gt ) and less than ( lt ) range parameters is an effective way to search for documents that contain a certain field value within a range where you know the upper and lower bounds. In this example, we can find all cars that were made in 2016, 2017, and 2018: 1.


2 Answers

now is always resolved to unix timestamp in millisecond. So one would need to either store the dates in UTC or speicify the timezone parameter in the range query.

From the documentation :

Dates can be converted from another timezone to UTC either by specifying the time zone in the date value itself (if the format accepts it), or it can be specified as the time_zone parameter:

now is not affected by the time_zone parameter (dates must be stored as UTC).

like image 141
keety Avatar answered Sep 27 '22 19:09

keety


now by itself will query based on UTC. So querying documents from midnight UTC to now would look like:

         "range" =>  {
            "myDateInSomeTimezone" =>  {
              "gte" =>  "now/d",
              "lte" =>  "now"
            }
          }

If you want to query documents in timezone other than UTC, you can use the time_zone parameter to the range queries like this:

         "range" =>  {
            "myDateInSomeTimezone" =>  {
              "gte" =>  "now/d",
              "lte" =>  "now",
              "time_zone" =>  "America/Los_Angeles"
            }
          }
like image 26
James Boutcher Avatar answered Sep 27 '22 17:09

James Boutcher