# Dynamic alias creation via template

**URL:** <https://discuss.elastic.co/t/dynamic-alias-creation-via-template/203547>\
**Category:** Elasticsearch\
**Created:** [October 15, 2019, 2:29am UTC](https://discuss.elastic.co/t/dynamic-alias-creation-via-template/203547 "2019-10-15T02:29:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ennui](https://avatars.discourse-cdn.com/v4/letter/e/df705f/32.png) [@Ennui](https://discuss.elastic.co/u/Ennui)\
**Post date:** [October 15, 2019, 2:29am UTC](https://discuss.elastic.co/t/dynamic-alias-creation-via-template/203547/1 "2019-10-15T02:29:06Z")

</div>

Hi,

I have indices created by users, where the index name always matches the pattern "xyz.helloworld" (the variable part is xyz)

Is there a way to use a template to automatically create an alias xyz for "xyz.helloworld"?

For example,

abc.helloworld should have alias abc  
test.helloworld should have alias test  
anothertest.helloworld should have alias anothertest

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [October 15, 2019, 8:32am UTC](https://discuss.elastic.co/t/dynamic-alias-creation-via-template/203547/2 "2019-10-15T08:32:59Z")

</div>

Hi @Ennui,

After a quick search I found this issue on github that maybe match with what you looking for:

> <https://github.com/elastic/elasticsearch/issues/5359>
>
> This is a proposal to extend the functionality to support aliases within templat…es #5180, #1825, #2739 
> 
> One use case that the current implementation does not cover is the ability to handle an index per day scenario. In this case it is nice to be able to present a single view of the indices which for the sake of argument look like "product\_elasticsearch\_20140306" as a unified alias which look like "product\_elasticsearch".
> 
> The idea is to allow alias names to be formatted similarly to how python handles format strings http://docs.python.org/2/library/string.html#formatstrings
> 
> If we have a script define the value of the alias's format string parameters then regexes are not required(although still an option) to get a substring of the index name.
> 
> Here is an example:
> 
> PUT \_template/template\_1
> 
> \`\`\` json
> {
> "template" : "product\_\*",
> "settings" : {
> "number\_of\_shards" : 1
> },
> "aliases": {
> "alias1" : {},
> "alias2" : {
> "filter" : {
> "term" : {"user" : "kimchy" }
> },
> "routing" : "kimchy"
> },
> "{index}-alias-for-{gender}": {
> "filter" : {
> "term" : {"product" : "Elasticsearch" }
> },
> "routing" : "Elasticsearch",
> "format": {
> "index": {
> "script": "\_index\['name'\].value",
> },
> "gender": {
> "script": "doc\['isFemale'\].value ? f : m",
> "params": {
> "m": "males",
> "f": "females"
> }
> }
> }
> }
> }
> }
> \`\`\`

But the issue is closed.

One solution is when you create the index you can use the "\_seq\_no" field from the elastic response to understand that the index is created and assign your alias.

> if \_seq\_no = 0:  
> set\_alias()

---

<div class="post-metadata">

**Author:** ![Ennui](https://avatars.discourse-cdn.com/v4/letter/e/df705f/32.png) [@Ennui](https://discuss.elastic.co/u/Ennui)\
**Post date:** [October 15, 2019, 10:31pm UTC](https://discuss.elastic.co/t/dynamic-alias-creation-via-template/203547/3 "2019-10-15T22:31:45Z")

</div>

Thanks @gabriel_tessier for the alternative solution. Yes I saw that github thread but was hoping that there would be some updates since it was closed a long while ago.

---

<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:** [November 12, 2019, 10:31pm UTC](https://discuss.elastic.co/t/dynamic-alias-creation-via-template/203547/4 "2019-11-12T22:31:49Z")

</div>

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