Elasticsearch Elasticsearch Index Alias

By Opster Team

Updated: Aug 28, 2023

| 2 min read

Quick links

Introduction

Elasticsearch index aliases serve as a crucial tool in managing and accessing data efficiently. They provide a level of abstraction, allowing users to interact with the data without needing to directly reference specific indices. This article delves into the intricacies of Elasticsearch index aliases, their use cases, and how to effectively implement them.

An index alias in Elasticsearch is essentially a secondary name for an index or a set thereof. It can point to one or more indices, enabling operations to be performed on multiple indices simultaneously. This functionality is particularly useful in scenarios where indices are frequently created or deleted, such as time-series data or log data management.

Use cases for index aliases

1. Index rotation and data aging: Index aliases are instrumental in managing time-series data, such as logs or metrics. For instance, an alias can point to the current index where data is being written, and as new indices are created (daily, weekly, or monthly), the alias can be updated to point to the new index. Older indices can then be deleted or archived without affecting the write operations.

2. Search across multiple indices: An alias can point to multiple indices, allowing a single query to search across all associated indices. This is beneficial when dealing with related data spread across multiple indices.

3. Zero downtime reindexing: During reindexing, an alias can be used to switch between the old and new index seamlessly, ensuring continuous data availability.

Creating and managing index aliases: A step-by-step guide

Creating an index alias involves the use of the _alias and _aliases APIs. 

Step 1: Creating an alias

To create an alias named ‘alias1’ for an index named ‘index1’, use the following command:

bash
PUT /index1/_alias/alias1

Step 2: Adding multiple indices to an alias 

To add multiple indices to an alias, use the following command:

bash
POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "index1", "alias" : "alias1" } },
        { "add" : { "index" : "index2", "alias" : "alias1" } }
    ]
}

It is also possible to leverage the _alias API to achieve the same result:

bash
PUT /index1,index2/_alias/alias1

Step 3: Removing an index from an alias 

To remove an index from an alias, use the following command:

bash
POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "index1", "alias" : "alias1" } }
    ]
}

It is also possible to leverage the _alias API to achieve the same result:

bash
DELETE /index1/_alias/alias1

Step 4: Listing aliases

To list all aliases in the cluster, you can use either of the following commands:

bash
GET /_aliasesGET /_cat/aliases

Filtering with index aliases

Index aliases also support filtering, which means that an alias can be set up with a predefined filter. When a search is performed using the alias, the filter is automatically applied. Here’s an example:

bash
POST /_aliases
{
    "actions" : [
        {
            "add" : {
                "index" : "index1",
                "alias" : "alias2",
                "filter" : { "term" : { "user" : "kimchy" } }
            }
        }
    ]
}

In this example, a filter is added to the alias ‘alias2’. Any search request that uses this alias will only return documents where the ‘user’ field is ‘kimchy’.

Routing with index aliases

Index aliases can also be associated with a routing value. This is particularly useful when the alias points to multiple indices, but the search or write operation should only be routed to specific shards. Here’s an example:

bash
POST /_aliases
{
    "actions" : [
        {
            "add" : {
                "index" : "index1",
                "alias" : "alias3",
                "routing" : "1"
            }
        }
    ]
}

In this example, any search or write request that uses the alias ‘alias3’ will be routed to the shard with the routing value ‘1’.

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?