Elasticsearch Index aliases

Elasticsearch allows the user to create an alias to the indices.

For example, if we are maintaining a different range of employee details in different indices. And we want to query the employee details of all ranges from our application. We can create an alias for our existing indices.

So that we can query all the needed indices with a single endpoint. If we are frequently adding new indices to our data sources. we don’t need to update every time in the application configuration part if we are using alias concept.

Whenever we are adding new indices to our data source, we can simply add the new index to alias. The endpoint will remain the same, we add our additional index without affecting our application (let’s say we are pointing our website to Elasticsearch for data set).

Index 1: sales-people

Index2: research-people

Index 3: management-people

Alias: our-employees (Sales people, Research people, Management people)

In this case, if we want to query the Index 1 to Index 3, we can simply run our query by pointing to the “our-employees” alias.

Following some snippets of Elasticsearch alias actions

Adding Alias

POST /_aliases
{
   "actions": [
      {
         "add": {
            "index": "sales-people",
            "alias": "our-employees"
         }
      }
   ]
}

Removing Alias

POST /_aliases
{
   "actions": [
      {
         "remove": {
            "index": "sales-people",
            "alias": "our-employees"
         }
      }
   ]
}

Adding and Removing index in alias using the same call

POST /_aliases
{
   "actions": [
      {
         "remove": {
            "index": "sales-people",
            "alias": "our-employees"
         },
         "add": {
            "index": "research-people",
            "alias": "our-employees"
         }
      }
   ]
}

Happy exploring data 🙂

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s