Skip to content

range filter on date_range datatype should use the format defined in the mapping #29282

@dadoonet

Description

@dadoonet

Tested on 6.2.3:

This is a Range query on a date field:

DELETE test
PUT test
{
  "mappings": {
    "_doc": {
      "properties": {
        "mydate": {
          "type": "date", 
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        }
      }
    }
  }
}
PUT test/_doc/1
{
  "mydate" : "2015-10-31 12:00:00"
}
GET test/_search
{
  "query": {
    "range": {
      "mydate": {
        "gte": "2015-10-31 12:00:00"
      }
    }
  }
}

This works well.

This is a Range query on a date_range field:

DELETE range_index
PUT range_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "time_frame": {
          "type": "date_range", 
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        }
      }
    }
  }
}
PUT range_index/_doc/1
{
  "time_frame" : { 
    "gte" : "2015-10-31 12:00:00", 
    "lte" : "2015-11-01"
  }
}
GET range_index/_search
{
  "query" : {
    "range" : {
      "time_frame" : {
        "gte" : "2015-10-31 12:00:00",
        "lte" : "2015-11-01",
        "relation" : "within" 
      }
    }
  }
}

This is failing with:

      {
        "type": "parse_exception",
        "reason": "failed to parse date field [2015-10-31 12:00:00] with format [strict_date_optional_time||epoch_millis]"
      }

We can see that the format defined in the mapping is not used here. It should use yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis but it uses strict_date_optional_time||epoch_millis.

If we manually force the format to be the same as the mapping, then it works:

GET range_index/_search
{
  "query" : {
    "range" : {
      "time_frame" : {
        "gte" : "2015-10-31 12:00:00",
        "lte" : "2015-11-01",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy", 
        "relation" : "within" 
      }
    }
  }
}

I suggest that we try first to use the mapping defined for the field and fallback to the default one if needed.

cc @melvynator

Metadata

Metadata

Assignees

No one assigned

    Labels

    :Search/SearchSearch-related issues that do not fall into other categories>bughelp wantedadoptme

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions