Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch filter by year from a date field

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.

like image 643
Avinash Avatar asked Aug 06 '15 16:08

Avinash


People also ask

What is elastic search?

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.

What is default date format in Elasticsearch?

SSSZ or yyyy-MM-dd .

How do I get the current date in Logstash?

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.


2 Answers

"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

like image 62
Avinash Avatar answered Sep 30 '22 02:09

Avinash


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"
                }
            }
        }
    }
}
like image 32
Val Avatar answered Sep 30 '22 04:09

Val