# Matching patterns from existing older templates ... will take precedence during new index creation

**URL:** <https://discuss.elastic.co/t/matching-patterns-from-existing-older-templates-will-take-precedence-during-new-index-creation/273764>\
**Category:** Elasticsearch\
**Tags:** ilm-index-lifecycle-management, reindex\
**Created:** [May 23, 2021, 4:17pm UTC](https://discuss.elastic.co/t/matching-patterns-from-existing-older-templates-will-take-precedence-during-new-index-creation/273764 "2021-05-23T16:17:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![6NMgfDwZ3](https://avatars.discourse-cdn.com/v4/letter/6/ac91a4/32.png) [@6NMgfDwZ3](https://discuss.elastic.co/u/6NMgfDwZ3)\
**Post date:** [May 23, 2021, 4:17pm UTC](https://discuss.elastic.co/t/matching-patterns-from-existing-older-templates-will-take-precedence-during-new-index-creation/273764/1 "2021-05-23T16:17:51Z")

</div>

Hello,

Environment:

- Elasticsearch 7.12.1 (Basic License)
- Kibana 7.12.1 (Basic License)
- Filebeat 7.12.1 (Basic License)
- Ubuntu 21.04

I have an index template named `myapp` the index pattern `myapp-*`.  
This index template created by Filebeat. (see details: [Legacy index templates](https://discuss.elastic.co/t/legacy-index-templates/273763))

I'd like to apply 1 ILM policy and 3 templates to every newly created index:

```auto
###################################

# https://www.elastic.co/guide/en/elasticsearch/reference/current/set-up-lifecycle-policy.html

curl -X DELETE "http://*MYELKIP*:9200/_ilm/policy/myapp_cleaner_policy"

curl -X PUT "http://*MYELKIP*:9200/_ilm/policy/myapp_cleaner_policy" -H 'Content-Type: application/json' -d'
{
  "policy": {
    "phases": {
      "hot": {
        "min_age": "0ms",
        "actions": {
          "set_priority": {
            "priority": 100
          }
        }
      },
      "delete": {
        "min_age": "5d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}
' | python3 -mjson.tool

curl -X GET "http://*MYELKIP*:9200/_ilm/policy/myapp_cleaner_policy" | python3 -mjson.tool

###################################

# https://www.elastic.co/guide/en/elasticsearch/reference/current/set-up-lifecycle-policy.html#apply-policy-template

curl -X DELETE "http://*MYELKIP*:9200/_index_template/myapp_cleaner_policy_template"

curl -X PUT "http://*MYELKIP*:9200/_index_template/myapp_cleaner_policy_template" -H 'Content-Type: application/json' -d'
{
	"index_patterns": ["myapp-*"],
	"priority": 1,
	"template": {
		"settings": {
			"index": {
				"lifecycle": {
					"name": "myapp_cleaner_policy",
					"rollover_alias": ""
				}
			}
		}
	}
}
' | python3 -mjson.tool

curl -X GET "http://*MYELKIP*:9200/_index_template/myapp_cleaner_policy_template" | python3 -mjson.tool

###################################

curl -X DELETE "http://*MYELKIP*:9200/_index_template/myapp_total_fields_policy_template"

curl -X PUT "http://*MYELKIP*:9200/_index_template/myapp_total_fields_policy_template" -H 'Content-Type: application/json' -d'
{
	"priority": 2,
	"index_patterns": ["myapp-*"],
	"template": {
		"settings": {
			"index": {
				"mapping": {
					"total_fields": {
						"limit": "5432"
					}
				}
			}
		}
	}
}
' | python3 -mjson.tool

curl -X GET "http://*MYELKIP*:9200/_index_template/myapp_total_fields_policy_template" | python3 -mjson.tool

###################################

curl -X DELETE "http://*MYELKIP*:9200/_index_template/myapp_date_nano_template"

curl -X PUT "http://*MYELKIP*:9200/_index_template/myapp_date_nano_template" -H 'Content-Type: application/json' -d'
{
	"priority": 3,
	"index_patterns": ["myapp-*"],
	"template": {
		"mappings": {
			"properties": {
				"@timestamp": {
					"type": "date_nanos"
				},
				"mytimestamp": {
					"type": "date_nanos"
				}
			}
		}
	}
}
' | python3 -mjson.tool

curl -X GET "http://*MYELKIP*:9200/_index_template/myapp_date_nano_template" | python3 -mjson.tool

```

Short explain:

- `myapp_cleaner_policy`: delete an index after 5 days
- `myapp_cleaner_policy_template`: apply `myapp_cleaner_policy` to every newly created index which is match with "myapp-\*"
- `myapp_total_fields_policy_template`: set the total mapped limit to 5432 to every newly created index which is match with "myapp-\*"
- `myapp_date_nano_template`: set preconfigured mapping to every newly created index which is match with "myapp-\*"

So after I run that script and check the Elasticsearch log, I got this:

```auto
[2021-05-23T15:59:41,524][INFO][o.e.x.i.a.TransportPutLifecycleAction] [x] adding index lifecycle policy [myapp_cleaner_policy]
[2021-05-23T15:59:41,819][INFO][o.e.c.m.MetadataIndexTemplateService] [x] removing index template [myapp_cleaner_policy_template]
[2021-05-23T15:59:42,092][WARN][o.e.c.m.MetadataIndexTemplateService] [x] index template [myapp_cleaner_policy_template] has index patterns [myapp-*] matching patterns from existing older templates [myapp] with patterns (myapp => [myapp-*]); this template [myapp_cleaner_policy_template] will take precedence during new index creation
[2021-05-23T15:59:42,096][INFO][o.e.c.m.MetadataIndexTemplateService] [x] adding index template [myapp_cleaner_policy_template] for index patterns [myapp-*]
[2021-05-23T15:59:42,314][INFO][o.e.c.m.MetadataIndexTemplateService] [x] removing index template [myapp_total_fields_policy_template]
[2021-05-23T15:59:42,442][WARN][o.e.c.m.MetadataIndexTemplateService] [x] index template [myapp_total_fields_policy_template] has index patterns [myapp-*] matching patterns from existing older templates [myapp] with patterns (myapp => [myapp-*]); this template [myapp_total_fields_policy_template] will take precedence during new index creation
[2021-05-23T15:59:42,446][INFO][o.e.c.m.MetadataIndexTemplateService] [x] adding index template [myapp_total_fields_policy_template] for index patterns [myapp-*]
[2021-05-23T15:59:42,665][INFO][o.e.c.m.MetadataIndexTemplateService] [x] removing index template [myapp_date_nano_template]
[2021-05-23T15:59:42,771][WARN][o.e.c.m.MetadataIndexTemplateService] [x] index template [myapp_date_nano_template] has index patterns [myapp-*] matching patterns from existing older templates [myapp] with patterns (myapp => [myapp-*]); this template [myapp_date_nano_template] will take precedence during new index creation
[2021-05-23T15:59:42,775][INFO][o.e.c.m.MetadataIndexTemplateService] [x] adding index template [myapp_date_nano_template] for index patterns [myapp-*]

```

if a new index created this is in the Elasticsearch log:

```auto
[2021-05-23T16:02:17,854][INFO][o.e.c.m.MetadataDeleteIndexService] [x] [myapp-7.12.1-2021.05.23/1wx3yd-fQ3mNRgojqEN6_w] deleting index
[2021-05-23T16:02:18,183][INFO][o.e.c.m.MetadataCreateIndexService] [x] [myapp-7.12.1-2021.05.23] creating index, cause [auto(bulk api)], templates [myapp_date_nano_template], shards [1]/[1]
[2021-05-23T16:02:18,533][INFO][o.e.c.m.MetadataMappingService] [x] [myapp-7.12.1-2021.05.23/xtI4jkMjSluzhOBjrD5ciA] update_mapping [_doc]

```

So Elasticsearch **only** applies the last (the largest priority number -\> myapp\_date\_nano\_template`) template of the above script.

I expected that all template will be applied to a newly created index.

I've tried:

- reorder the templates (different priority order) ... but always the latest template win (with largest priority number)
- "merge" the content of the three templates into one template ... **it worked!** All the three "rules" applied to the newly created index

Why only the last template has applied to the newly created index?  
Is there some kind of limitations?  
Is it related to my "legacy index template" question ([Legacy index templates](https://discuss.elastic.co/t/legacy-index-templates/273763)) ?

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [May 26, 2021, 11:01pm UTC](https://discuss.elastic.co/t/matching-patterns-from-existing-older-templates-will-take-precedence-during-new-index-creation/273764/2 "2021-05-26T23:01:06Z")

</div>

Is there a reason you don't want all 3 configs in the same template?

---

<div class="post-metadata">

**Author:** ![6NMgfDwZ3](https://avatars.discourse-cdn.com/v4/letter/6/ac91a4/32.png) [@6NMgfDwZ3](https://discuss.elastic.co/u/6NMgfDwZ3)\
**Post date:** [May 28, 2021, 4:38pm UTC](https://discuss.elastic.co/t/matching-patterns-from-existing-older-templates-will-take-precedence-during-new-index-creation/273764/3 "2021-05-28T16:38:27Z")

</div>

Of course, that would be a reasonable response for this situation but I'd like to understand if I missed something or not done on a proper way or it's just an ES bug

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 25, 2021, 4:38pm UTC](https://discuss.elastic.co/t/matching-patterns-from-existing-older-templates-will-take-precedence-during-new-index-creation/273764/4 "2021-06-25T16:38:29Z")

</div>

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