ElasticSearch date rangequery affected by TimeZone


(Sosoda) #1

I tried for several times,but still confused.
At first the mapping of date field is
{
"createdAt" : {
"type" : "date",
"format" : "yyyy-MM-dd HH:mm:ss"
}
}
when I query with java, I use SimpleDateFormat like

SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format = smf.format(date);

the result is affected by TimeZone.
And I try store the date field with format like "yyyy-MM-dd hh:mm:ss Z" and without format.In this time , I get the correct result. So what happend in the format thing?


(Christoph) #2

Hi,
which version of Elasticsearch are you using? That said, dates are internally stored as long
values representing milliseconds-since-the-epoch in UTC. So if your date are not UTC, you can either convert them in your application or specify the time zone in your date format string.

The Range Query also supports a time_zone parameter, but this will only affect the bounds you apply in the query itself, not the stored values.


(Sosoda) #3

Thank you for reply.:D
I'm using version 1.2, quite old one.
I think I know what happened before.
The date value I stored in ES when I'm using Java Client And save with Java.util.Date. It is timestamp acturally.
When I'm doing the query, I'm using the data value like 2016-03-03 00:00:00 .When ES convert this into timeStamp,
it automaticly add "+00:00". I intent to search with a different timezone.
Is this right?


(Christoph) #4

Hi Sosoda,

ES doesn't add anything to your dates, it rather assumes it is UTC when you don't specify any other timezone. I'm not exactly sure, but it seems the time_zone option has been added to the range query in ES 1.4.


(system) #5