Sometime you’ll want to use templates with Elasticsearch for things such as defining the field types to be used in the document mapping. This has changed a bit in recent versions and caught me out.

If you copy and paste template definitions that you’ve found lying around on t’internet such as this one:

curl -XPUT "http://localhost:9200/_template/kafkaconnect/" -H 'Content-Type: application/json' -d ' { "template": "*", "settings": { "number_of_shards": 1, "number_of_replicas": 0 }, "mappings": { "_default_": { "dynamic_templates": [ { "dates": { "match": "*_TS", "mapping": { "type": "date" } } } ] } } }'

You’ll now get this error, which is deliberate:

{ "error" : { "root_cause" : [ { "type" : "mapper_parsing_exception" , "reason" : "Root mapping definition has unsupported parameters: [_default_ : {dynamic_templates=[{dates={mapping={type=date}, match=*_TS}}]}]" } ] , "type" : "mapper_parsing_exception" , "reason" : "Failed to parse mapping [_doc]: Root mapping definition has unsupported parameters: [_default_ : {dynamic_templates=[{dates={mapping={type=date}, match=*_TS}}]}]" , "caused_by" : { "type" : "mapper_parsing_exception" , "reason" : "Root mapping definition has unsupported parameters: [_default_ : {dynamic_templates=[{dates={mapping={type=date}, match=*_TS}}]}]" } } , "status" : 400 }

To get this to work just remove the type name ( _default_ ) from the mappings element entirely:

curl -XPUT "http://localhost:9200/_template/kafkaconnect/" -H 'Content-Type: application/json' -d ' { "template": "*", "settings": { "number_of_shards": 1, "number_of_replicas": 0 }, "mappings": { "dynamic_templates": [ { "dates": { "match": "*_TS", "mapping": { "type": "date" } } } ] } }'

HOWEVER this only works for Elasticsearch 7; on Elasticsearch 6 and earlier you will get Malformed [mappings] section for type [dynamic_templates], should include an inner object describing the mapping"}] .