Strict Dynamic Mappings With Dynamic Templates

Hi
Is it possible to apply strict dynamic mappings using dynamic templates with patterns?
The goal is to allow structured strict dynamic mappings.
Example:

PUT _template/test-template
{
    "index_patterns": [
        "test.*"
    ],
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas":0
    },
    "mappings": {
        "dynamic": "strict",
        "dynamic_templates":[
            {
                "other_data":{
                    "match_mapping_type":"object",
                    "match":"*_data",
                    "mapping":{
                        "type":"object",
                        "dynamic":true
                    }
                }
            }
        ],
        "properties": {
            "timestamp": {
	        "type": "date"
	    },
	    "log_level": {
	        "type": "keyword"
	    },
	    "message": {
	        "type": "text"
	    }
	 }
    }
}

and try index a document, example:

POST test.index/_doc
{ 
   "timestamp":"2019-12-12T10:10:57.910",
   "log_level":"error",
   "message":"failed miserably",
   "service_data":{
     "foo":"bar",
     "baz":2354
   }
}

this will get the following response:

{
  "error": {
    "root_cause": [
      {
        "type": "strict_dynamic_mapping_exception",
        "reason": "mapping set to strict, dynamic introduction of [service_data] within [_doc] is not allowed"
      }
    ],
    "type": "strict_dynamic_mapping_exception",
    "reason": "mapping set to strict, dynamic introduction of [service_data] within [_doc] is not allowed"
  },
  "status": 400
}

Is it even possible to create such mapping? If it is what am i missing?
I would expect this behavior when trying to index something like the following

POST test.index/_doc
{ 
   "timestamp":"2019-12-12T10:10:57.910",
   "log_level":"error",
   "message":"failed miserably",
   "arbitrary_field":"test"
}

No, you cannot dynamically map fields if you set dynamic to strict. You'd have keep dynamic to its default value of true to use dynamic templates.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.