Or Weinberger
Or Weinberger

Reputation: 7472

Elasticsearch Filter by day of week

I have the following document structure:

{
    "@version" : "1",
    "@timestamp" : "2015-05-25T13:31:43.848Z",
    "type" : "myevent",
    "value": 1
}

Is it possible to query all documents that occured on a Sunday?

Upvotes: 2

Views: 7846

Answers (2)

Andrei Stefan
Andrei Stefan

Reputation: 52368

Or, if you don't want to enable dynamic scripting because of this use a script file:

{
  "query": {
    "filtered": {
      "filter": {
        "script": {
          "script_file": "my_date_script",
          "params": {
            "some_day": 7
          }
        }
      }
    }
  }
}

where my_date_script.groovy should be placed under /config/scripts and its content should look like this:

def day = doc['@timestamp'].date.dayOfWeek; day == some_day

Or, as I mentioned in my comment, for faster queries (since scripts are not particularly fast) index the day of the week in the index and directly query that.

Upvotes: 2

Val
Val

Reputation: 217344

You can try to query it with a script filter like this:

curl -XPOST localhost:9200/your_index/_search -d'
{
  "query": {
    "filtered": {
      "filter": {
        "script": {
          "script": "doc['@timestamp'].date.dayOfWeek == 7"
        }
      }
    }
  }
}'

Note that you need to make sure dynamic scripting is enabled by adding this to your elasticsearch.yml configuration

script.disable_dynamic: false

Upvotes: 3

Related Questions