Ashish Pandey
Ashish Pandey

Reputation: 495

Adding alias to index in elastic search template runtime

I want to add an alias to the index in elasticsearch template, but the alias is not always fixed. Its prepended by a client id. Due to the size of the data, we are creating monthly indexes for each client separately, and I want a single to query the data from all monthly indexes. However, because each client has different indexes, the alias has to prepended by clientId. How can I achieve this?

Eg: my indexes are:

client1_01_18_user_location/user_location, client1_02_18_user_location/user_location

and

client2_01_18_user_location/user_location, client2_02_18_user_location/user_location

The data for the month of January goes to 1st index, and for Feb goes to 2nd index in each client's case. I want an alias client1_ul or client2_ul to be created, based on, for which client the data is being inserted.

How do I achieve this?

Upvotes: 1

Views: 6764

Answers (1)

Dario Balinzo
Dario Balinzo

Reputation: 691

You can solve with a POST like this:

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "client1*", "alias" : "client1_ul" } }
    ]
}

This call will add all the index of the form client1* to the alias client1_ul, that can be used to query all the months.

You will need a POST for each user.

Otherwise, you can use a index template for each client, if not all the indexes are present when you add the alias. The template can provide an alias:

PUT _template/template_1
{
    "index_patterns" : ["client1*"],
    "mappings" : {
        ... optional
    },
    "aliases" : {
        "client1_ul" : {}
    }
}

In this way the alias will be added to each index at the index creation, automatically by elasticsearch.

Upvotes: 4

Related Questions