As you mentioned in the comment, your timezone is GMT +4
. That's why 4hours
are added to your date.
Internally, dates are converted to UTC (if the time-zone is specified) and stored as a long number representing milliseconds-since-the-epoch.
Reference
It's best to keep UTC
formats in the index. And apply timezone settings at the query time.
To query the data:
{
"query": {
"range": {
"initialDate": {
"time_zone": "+04:00",
"gte": "2020-01-01T00:00:00",
//value you are providing as input from your timezone, hence I am adding +04
"lte": "now"
}
}
}
}
Reference
time_zone (Optional, string) Coordinated Universal Time (UTC) offset
or IANA time zone used to convert date values in the query to UTC.
EDIT:
Apart from the internals, you want to see the date that you provide. You can do that by using timezone format in the mapping.
yyyy-MM-dd'T'HH:mm:ss.SSSZ
Mapping: - PUT http://host:port/indexName/_mapping
timestamp1{
"type": "date_nanos",
"ignore_malformed": true,
"format": "yyyy-MM-dd'T'HH:mm:ss'Z'"
}
Post: POST http://host:port/indexName/_doc
{
"timestamp1":"2020-12-31T16:00:00Z",
"key":"1"
}
Search: GET http://host:port/indexName/_search
"_source": {
"timestamp1": "2020-12-31T16:00:00Z",
"key": "1"
}
But internally this date will be stored as milliseconds and retrieval time it uses the string you provided at the time of index.
More format options