elastic / elasticsearch

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

Mapping parameter validation is based on properties set instead of resulting value #96052

Open jbaiera opened 1 year ago

jbaiera commented 1 year ago

Mapping parameters are sometimes mutually exclusive on a field. For instance, when mapping a number field with a script parameter, if you set ignore_malformed to true the mapping fails to create. However, ignore_malformed is a special parameter which can obtain its default from the index.mapping.ignore_malformed setting. Setting the property here does not cause a validation error. If the mapping then attempts to reset the value back to its normal default value, the validation trips and says that the mapping is invalid:

PUT _component_template/test
{
  "template": {
    "settings": {
      "index.mapping.ignore_malformed": true // Bypassing the regular default with a different value
    },
    "mappings": {
      "properties": {
        "@timestamp": {
          "type": "long",
          "ignore_malformed": false, // Trying to revert the value back to the normal default is not allowed
          "script": {
            "source": "emit(1L)"
          }
        }
      }
    }
  }
}

fails with the following response:

{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "Failed to parse mapping: Field [ignore_malformed] cannot be set in conjunction with field [script]"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "Failed to parse mapping: Field [ignore_malformed] cannot be set in conjunction with field [script]",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "Field [ignore_malformed] cannot be set in conjunction with field [script]"
    }
  },
  "status": 400
}

I'm not sure if there are other cases where parameters preclude other parameters that have dynamic default values. It seems that the parameter validation is more concerned about the parameter (1) existing on the mapping and (2) differing in value from the default, but not (3) that the resulting value is actually compatible.

elasticsearchmachine commented 1 year ago

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

romseygeek commented 1 year ago

The reason you can't set ignore_malformed on a script field is that scripts are typed, so you can't emit a badly-formed value. I suppose we could change this to just be a warning rather than an error though?

elasticsearchmachine commented 5 months ago

Pinging @elastic/es-storage-engine (Team:StorageEngine)