# Automatically create an index based on an alias

**URL:** <https://discuss.elastic.co/t/automatically-create-an-index-based-on-an-alias/191941>\
**Category:** Elasticsearch\
**Created:** [July 24, 2019, 4:30am UTC](https://discuss.elastic.co/t/automatically-create-an-index-based-on-an-alias/191941 "2019-07-24T04:30:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vladimir\_Jaric](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vladimir_jaric/32/50781_2.png) [@Vladimir\_Jaric](https://discuss.elastic.co/u/Vladimir_Jaric)\
**Post date:** [July 24, 2019, 4:30am UTC](https://discuss.elastic.co/t/automatically-create-an-index-based-on-an-alias/191941/1 "2019-07-24T04:30:48Z")

</div>

Hi,

I am trying to automatically create an index that does not already exist by applying an index template with an alias definition.

Template looks like this:

```
PUT _template/my_template
{
    "index_patterns" : ["*"],
    "aliases" : {
        "{index}_alias" : {} 
    }
}

```

If I then index the first document:

```
POST /index1/_doc/
{
  "hello": "world"
}

```

It works as expected with both index and alias automatically created:

```
GET /_aliases
{
    "index1" : {
        "aliases" : {
            "index1_alias" : { }
        }
    }
}

```

My problem is that I don't know an index name until I get a document to index and I have been relying on automatic index creation so far. I would now like to only use index aliases in all API calls to future proof my application.

So, if I index the first document with an alias like this:

```
POST /index2_alias/_doc/
{
    "hello": "world"
}

```

I would like to get this:

```
GET /_aliases
{
    "index2" : {
        "aliases" : {
            "index2_alias" : { }
        }
    }
}

```

Is that possible?

Thank you,  
Vlad

---

<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:** [August 21, 2019, 4:30am UTC](https://discuss.elastic.co/t/automatically-create-an-index-based-on-an-alias/191941/2 "2019-08-21T04:30:50Z")

</div>

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