Reputation: 769
I'm learning Elasticsearch and have an issue with date range queries. My queries compare only days, not the whole dates. Here's a short example:
DELETE test
PUT /test
{
"mappings": {
"properties": {
"date" : {
"type": "date",
"format": "dd MM YYYY"
}
}
}
}
POST /_bulk
{"index" : {"_index": "test"}}
{"date": "01 01 2001"}
POST /test/_search
{
"query": {
"range": {
"date": {
"lt": "01 01 2016",
"gt": "01 01 2000"
}
}
}
}
POST /test/_search
{
"query": {
"range": {
"date": {
"lte": "01 01 2000",
"gte": "01 01 2016"
}
}
}
}
From my understanding the first search query should return the document - the date 2001 is < 2016 and > 2000. The second query should return nothing because 2001 is not <= 2000 and >=2016. But it works completely opposite.
Could you please point me what I'm missing?
Upvotes: 2
Views: 280
Reputation: 7566
The reason it is failing is because ES is not formatting your date appropriately. The date formatting behavior changed between 6.x and 7.0 and joda based date formatters are replaced with java ones. If you change your format to "format": "dd MM yyyy"
(Note the lower case yyyy
), you will find your search works.
The reason for the format not working, is that YYYY
signifies date week year. You almost always want to have a lower case yyyy
format to signify your year in your date format.
Upvotes: 2