Callum Linington
Callum Linington

Reputation: 14417

ElasticSearch Date Math find out what it's calculating

So I would like to know what now-1y is producing as a result. So is it:

  1. now - 365days
  2. now - 365.25days
  3. now with the year part subtracted, so 2014-12-23T08:46:00

I've read this documentation but can't find any more information.

Upvotes: 2

Views: 127

Answers (2)

curiousity
curiousity

Reputation: 179

To answer the general question asked above: use the validate API.

indexname/_validate/query?rewrite=true

{
  "query": {
    "range": {
      "modifiedDate": {
        "gte": "now-1y/y",
        "lte": "now/y"
      }
    }
  }
}

And you get:

{
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "valid": true,
  "explanations": [
    {
      "index": "indexname",
      "valid": true,
      "explanation": "+modifiedDate:[1640995200000 TO 1704067199999] #FieldExistsQuery [field=_primary_term]"
    }
  ]
}

This lets you see exactly what value is being used for a given query.

Upvotes: 0

ulric260
ulric260

Reputation: 374

After some testing it appears that the third proposition is correct (ie "now with the year part subtracted, so 2014-12-23T08:46:00"

If 2 was correct the following example should have return 2 results. example1

As 2012 is a leap year. The following example should return 0 result if 1 was correct. example2

Note: example only worked as expected when I wrote this answer, in order to make it work for you, you have to update the date field of the indexed document.

Upvotes: 1

Related Questions