Oliver
Oliver

Reputation: 851

Only show matched weekdays

In my index i have a field called available_weekdays. It's an array with weekdays from 1-7.

In my query i pass a start_date with the format 'yyyy-MM-dd'.

Goal: I only want to show records which match the start_date weekday.

Lets say i pass 2017-01-01 to my query which is weekday 7. My query would have to convert my passed date to a weekday. Then it checks for a matching weekday in the available_weekdays field. If weekday is found in available_weekdays return the record.

ES version: 2.4.4

Is this possible by using a script filter?

Upvotes: 2

Views: 424

Answers (1)

Taras Kohut
Taras Kohut

Reputation: 2555

Yes, you can do this using script query

{
  "query":{
    "bool":{
      "filter":{
        "script":{
          "script":{
            "lang":"groovy",
            "inline":"doc['available_weekdays'].values.find{it == DateTime.parse(param1).dayOfWeek().get()}",
            "params":{
              "param1":"2017-01-01"
            }
          }
        }
      }
    }
  }
}

Upvotes: 4

Related Questions