i have docs with date field of the format yyyy-MM-dd. is there a way to filter only based on year part of the field.
example:
{'name': 'a', 'born': '1984-11-22'},
{'name': 'b', 'born': '1984-12-12'},
{'name': 'c', 'born': '1985-10-22'},
I want to do a term/range filter to find people born on year 1984. Any help is highly appreciated.
Elasticsearch is a distributed search and analytics engine built on Apache Lucene. Since its release in 2010, Elasticsearch has quickly become the most popular search engine and is commonly used for log analytics, full-text search, security intelligence, business analytics, and operational intelligence use cases.
SSSZ or yyyy-MM-dd .
you could do it in logstash with elasticsearch filter. basically you search for documents that has “Today's date” field then update the “Today's date” value using current date value. you can run it with a daily cron at the start of day.
"query": {
"filtered": {
"filter": {
"range": {
"born": {
"gte": "1984||/y",
"lte": "1984||/y",
"format": "yyyy"
}
}
}
}
}
This required rounding the input year argument as well in addition to adding the format. Without rounding by year the range query does not work as intended. Tried it on multiple date formats like dateOptionalTime, date etc.
For details on how to use Date Math see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html
Yes, you can do it using a range
filter and specifying a year format
:
{
"constant_score": {
"filter": {
"range" : {
"born" : {
"gte": "1984",
"lte": "1984",
"format": "yyyy"
}
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With