CptFoobar
CptFoobar

Reputation: 20

Get records for particular day of the week in ElasticSearch

I have an ES cluster that has some summarized numerical data such that there is exactly 1 record per day. I want to write a query that will return the documents for a specific day of the week. For example, all records for Tuesdays. Currently I am doing this by getting all records for the required date range and then filtering out the ones for the day that I need. Is there a way to do that with a query?

Upvotes: 0

Views: 1324

Answers (1)

Val
Val

Reputation: 217344

You can do it using a script like this:

POST my_index/_search
{
  "query": {
    "script": {
      "script": {
        "source": "doc.my_date.value.dayOfWeek == 2"
      }
    }
  }
}

If you're going to run this query often, you would be probably better off creating another field dayOfWeek in your document that contains the day of the week that you can then easily query using a term query. It would be more efficient than a script.

Upvotes: 2

Related Questions