Skip to content

Date math in query_string caches now() #2808

@clintongormley

Description

@clintongormley

When using "now" in date math in the query string, the value for now is cached:

curl -XPUT 'http://127.0.0.1:9200/test/?pretty=1'  -d '
{
   "mappings" : {
      "test" : {
         "properties" : {
            "date" : {
               "type" : "date"
            }
         }
      }
   }
}
'

curl -XPOST 'http://127.0.0.1:9200/test/test?pretty=1'  -d '
{
   "date" : "2020-01-01"
}
'

Run this query multiple times:

curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1'  -d '
{
   "query" : {
      "query_string" : {
         "query" : "date:[now TO *]"
      }
   },
   "explain" : 1
}
'

You will see that the description line "ConstantScore(date:[1363953493782 TO *])" reuses the same start date

# {
#    "hits" : {
#       "hits" : [
#          {
#             "_source" : {
#                "date" : "2020-01-01"
#             },
#             "_score" : 1,
#             "_index" : "test",
#             "_shard" : 4,
#             "_id" : "iOPrtwzdQFGixdfwaAAlcQ",
#             "_node" : "gx3hk4y7S0Khhx8IQ4uYQQ",
#             "_type" : "test",
#             "_explanation" : {
#                "value" : 1,
#                "details" : [
#                   {
#                      "value" : 1,
#                      "description" : "boost"
#                   },
#                   {
#                      "value" : 1,
#                      "description" : "queryNorm"
#                   }
#                ],
#                "description" : "ConstantScore(date:[1363953493782 TO *]), product of:"
#             }
#          }
#       ],
#       "max_score" : 1,
#       "total" : 1
#    },
#    "timed_out" : false,
#    "_shards" : {
#       "failed" : 0,
#       "successful" : 5,
#       "total" : 5
#    },
#    "took" : 5
# }

A range query on the other hand, updates the start date on each run:

curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1'  -d '
{
   "query" : {
      "range" : {
         "date" : {
            "gt" : "now"
         }
      }
   },
   "explain" : 1
}
'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions