29 April 2014 Engineering

Aliases for the Win

By Luca Cavanna

Index aliases are supported in elasticsearch for some time now. Using aliases you can easily abstract your physical indices away, so that only logical indices are exposed to the users and a lot of interesting things become possible. For instance you can change your mapping without downtime, or even scale out without reindexing your data when your index has reached its full capacity. Aliases can be created, modified and deleted through proper aliases api; elasticsearch 1.1.0 makes it even easier to create them though. The idea is that aliases should be used in every situation, right from the beginning of every project, which is why we made it possible to specify them while creating an index, as well as when creating index templates.

Aliases upon index creation

As of 1.1.0, aliases can be provided as part of the create index api. Here is an example:

curl -XPUT localhost:9200/logs-2014-04-14 -d '
{
  "aliases" : {
    "april_2014" : {},
    "year_2014" : {}
  },  
  "settings" : {
    "number_of_shards" : 3,
    "number_of_replicas" : 0 
  } 
}
'

The above request creates a new index called logs-2014-04-14 and associates the aliases april_2014 and year_2014 with it.

Aliases support in index templates

From 1.1.0 it is also possible to specify aliases when registering index templates. This comes in very handy especially when dealing with time based data, for instance daily indices. You can just create a couple of index templates as follows:

curl -XPUT localhost:9200/_template/template_2014 -d '
{
  "template" : "logs-2014-*",
  "aliases" : {
    "logs-2014" : {}
  } 
}
'
curl -XPUT localhost:9200/_template/template_2014_04 -d '
{
  "template" : "logs-2014-04-*",
  "aliases" : {
    "logs-2014-04" : {}
  }
}
'

As a result, whenever you create an index for year 2014, it will get automatically added to the logs-2014 alias. Also, whenever an index for april 2014 is created, it will get added to the existing logs-2014-04 alias that holds all indices for april 2014.

The above examples contain simple aliases but let’s not forget that an alias can hold routing keys and a filter too. Also, because index templates allow you to associate an alias with an index whose name is not known yet, it is possible to use the {index} placeholder as part of the alias name, which will get automatically replaced with the index the template is getting applied to at index creation time.

curl -XPUT localhost:9200/_template/template_2014 -d '
{
  "template" : "logs-2014-*",
  "aliases" : {
    "{index}-alias" : {}
  }
}
'

Note that it is only possible to use the whole index name as part of the alias name. Although it would be nice to be able to use a portion of the index name and do much more complex things, we decided to keep it simple for now, but there’s already a github issue to address this. A big thank you to James Brook for his contribution on these improvements! We look forward to your feedback on the mailing list or Twitter!