Problem: Have an ElasticSearch Dynamic Template that will apply Templates and/or new properties to any new Index or Type created.
Anytime an Index is created with a new type it should
- Have a default DynamicTemplate
- Add the properties "timestamp" and "creation_date".
First use the following resources:
https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-templates.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/default-mapping.html
That only gets you part way.
Solution:
This was not easy to find, But what you will need to is set (Examples below):
- "index_patterns": ["*"] /* this will match all indexes created */
- "mappings"."{type}". This should be set to "_default_"
Note: this does not effect existing Templates.
PUT _template/template_1
{
"index_patterns": ["*"],
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"_default_": {
"_source": {
"enabled": true
},
"dynamic_templates": [
{
"id_integers": {
"match": "*_id",
"match_mapping_type": "long",
"mapping": {
"type": "long"
}
}
},
{
"date_fields": {
"match": "*_date",
"match_mapping_type": "date",
"mapping": {
"type": "date"
}
}
},
{
"json_fields": {
"match": "*_json",
"match_mapping_type": "*",
"mapping": {
"type": "text"
}
}
},
{
"int_template": {
"match": "_*",
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
],
"properties": {
"creation_date": {
"type": "date",
"format": "yyyy-MM-dd"
},
"timestamp": {
"type": "date",
"format": "epoch_millis"
}
}
}
}
}
Test: POST /school/student/1
{
"title": "The quick brown fox",
"price": 5,
"colors": ["red","green","blue"],
"do_something":"hello world"
}
Validation: POST /school/student/_mapping{
"school": {
"mappings": {
"student": {
"dynamic_templates": [
{
"id_integers": {
"match": "*_id",
"match_mapping_type": "long",
"mapping": {
"type": "long"
}
}
},
{
"date_fields": {
"match": "*_date",
"match_mapping_type": "date",
"mapping": {
"type": "date"
}
}
},
{
"json_fields": {
"match": "*_json",
"mapping": {
"type": "text"
}
}
},
{
"int_template": {
"match": "_*",
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
],
"properties": {
"creation_date": {
"type": "date",
"format": "yyyy-MM-dd"
},
"timestamp": {
"type": "date",
"format": "epoch_millis"
}
}
}
}
}
}