elastic / elasticsearch

Free and Open Source, Distributed, RESTful Search Engine
https://www.elastic.co/products/elasticsearch
Other
1.21k stars 24.84k forks source link

Time Zone Behaviour in Query String Query #85178

Open stevejgordon opened 2 years ago

stevejgordon commented 2 years ago

It was recently noted that when specifying a time_zone for a query string query, it is not always applied to the value being searched. This issue is intended to review this behaviour and discuss if it is as historically designed. This applies to 7.x and 8.x versions.

The supplied example is:

PUT test
{
  "mappings": {
    "dynamic": true
  }
}

PUT test/_doc/1
{
  "testDate": "2022-02-24T15:00:00.000-0000"
}

# working as expected
POST test/_search
{
  "query": {
    "range": {
      "testDate": {
        "gte": "2020-02-24",
        "lte": "2022-02-25T00:00:00",
        "time_zone": "Asia/Tokyo"
      }
    }
  }
}

# NOT working 
POST test/_search
{
  "query": {
    "query_string": {
      "query": "2022-02-25",
      "time_zone": "Asia/Tokyo"
    }
  }
}

After investigating further, I noted that the query string query works as expected (applying the time zone to the query value) if the field is specified using either of these approaches. This doesn't seem to be explicitly called out in the documentation.

POST test/_search
{
  "query": {
    "query_string": {
      "query": "testDate:2022-02-25",
      "time_zone": "Asia/Tokyo"
    }
  }
}
POST test/_search
{
  "query": {
    "query_string": {
      "query": "2022-02-25",
      "time_zone": "Asia/Tokyo",
      "default_field": "testDate"
    }
  }
}

@cbuescher also took a look at the QueryStringQueryParser code governing this behaviour and confirmed that when the date field is specifically targetted or the default field, the timezone should get applied.

elasticmachine commented 2 years ago

Pinging @elastic/es-search (Team:Search)

benwtrent commented 4 months ago

I have confirmed this still exists and honestly, I don't know if we can or will fix it. The tricky part is that I don't think we want to apply the timezone if the query string is querying any field other than a particular dateField. If we do, that will change the behavior pretty significantly.

I suggest we close this as "wont fix" @javanna @cbuescher . Though we should better document how timezone plays with query string.

elasticsearchmachine commented 4 months ago

Pinging @elastic/es-docs (Team:Docs)

elasticsearchmachine commented 4 months ago

Pinging @elastic/es-search-relevance (Team:Search Relevance)