Elasticsearch - how to find exact date type values?
So I am using elasticsearch with spring framework and I am having trouble getting exact date values.
Here's my property mapping:
{
"properties": {
"creationDate": {
"type": "date",
"format": "dd-MM-yyyy HH:mm:ss"
},
...
}
}
Here's the mapping in java class:
@Field(type = Date, format = DateFormat.custom, pattern ="dd-MM-yyyy HH:mm:ss")
public Calendar creationDate;
The problem is when I'm trying to find the exact date:
GET test/searchableSo/_search
{
"query": {
"term": {
"creationDate": {
"value": "14-11-2014 05:55:46"
}
}
}
}
It only returns nothing if I use the long equivalent:
{
"query": {
"term": {
"creationDate": {
"value": "1415987746214"
}
}
}
}
Any insight?
source to share
It is generally safer to use a range filter instead of term / match when working with date fields.
Elasticsearch internally stores the date type as a long value. So I believe that passing 1415987746214 while indexing should eventually keep the value as it is.
Therefore, 1415987746214 is not the same as " 11/14/2014 05:55:46". because of the millisecond part.
Try indexing it without the millisecond part, i.e. " 1415987746000 " or you can use the numeric_resolution in seconds parameter in the collation and specify the timestamp in seconds since the epoch when indexing, i.e. 1415987746
Example:
"properties": { "creationDate": { "type": "date", "format": "dd-MM-yyyy HH:mm:ss" ,"numeric_resolution" : "seconds"},..}
either will work for the request:
{ "query": { "term": { "creationDate": { "value": "14-11-2014 17:55:46" } } } }
remember to use the 24 hour clock.
source to share