George
(George)
April 7, 2016, 1:57pm
1
Hello everybody,
I have a use case where I need to search in es for a date field.
However, this is not only a simple range but I wanna ignore the year.
Hence I saw this in the es docu:
{
"range" : {
"born" : {
"gte": "01/01/2012",
"lte": "2013",
"format": "dd/MM/yyyy||yyyy"
}
}
}
and I tried this:
{
"range" : {
"born" : {
"gte": "01/01",
"lt": "02/01",
"format": "dd/MM"
}
}
}
I had a document with a range date field which was set to 01.01.2014 but the above query didn't match my document.
Can anybody point me in some direction? Basically I wanna search for date fields but ignoring the year.
PS. I'm using es 2.X
Many thanks!
dadoonet
(David Pilato)
April 7, 2016, 2:19pm
2
IMO, if you are searching for a month, index a month. If you are searching for a day, index a day.
A date in elasticsearch is indexed as ms
since epoch. So you can not really do that kind of range query.
That being said, you could compute an aggregation using https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-script and then add a sub aggregation https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-aggregations-metrics-top-hits-aggregation.html . It would may be give you something close to what you are looking for but not exactly what you asked for.
My 2 cents
George
(George)
April 7, 2016, 2:35pm
3
Hi David,
Thanks for the info.
I was thinking about the indexing part but I thought it would be easier if es query api already has this functionality to ignore the year.
However the aggregation part looks interesting too, so thanks for point that out.
Regards!