Fur1at
Fur1at

Reputation: 47

How to get first day of week in kibana using range?

I have date field in ES and i want to get only first day of week.

Tried

{
  "query": {
    "range": {
      "myDate": {
        "from": "now/w",
        "to": "now/w"
      }
    }
  }
}

and

{
  "query": {
    "terms": {
      "myDate": [
        "now/w"
      ]
    }
  }
}

but it always returns results from monday to today.

Upvotes: 0

Views: 819

Answers (1)

Val
Val

Reputation: 217344

Your query goes int he right direction, but since you're rounding on the week, all days of that week will have the same value. What you need to do is to add another filter to only consider documents with dates on the Monday from that week, like this:

{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "myDate": {
              "from": "now/w",
              "to": "now/w"
            }
          }
        },
        {
          "script": {
            "script": {
              "source": "doc['myDate'].value.dayOfWeek == 1"
            }
          }
        }
      ]
    }
  }
}

Instead of using scripting (performance killer), you could also index the day of the week inside another field of your document, so you can run a simple term query on that field.

Upvotes: 2

Related Questions