Elastic Search

Delete Alias in Elasticsearch

“Aliases are an essential feature in Elasticsearch. They allow developers and applications to use alternative names to access various resources such as indices and data streams. We can also use aliases to perform reindexing operations without taking down an index, resulting in downtime.

Whether you are just starting with Elasticsearch or seasoned developers, learning the fundamentals of working with the alias API can be hugely beneficial.”

This post aims to give you the fundamentals of working with aliases in Elasticsearch by showing you how to create and delete an alias for a given Elasticsearch resource.

Let’s jump in and learn.

Elasticsearch Create Alias

To create an alias for a given resource in Elasticsearch, we use the alias API. The syntax below shows the request for adding a new alias.

POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "",
        "alias": ""
      }
    }
  ]
}

We use the action ADD to create a new alias for an index or data stream.

NOTE: The add operation requires the target user to have to manage privileges on the target data stream or index.

Example. Suppose we want to give an alias to the “kibana_sample_data_logs” index. We can run the request as shown:

curl -XPOST "localhost:9200/_aliases" -H "kbn-xsrf: reporting" -H "Content-Type: application/json" -d'
{
  "actions": [
    {
      "add": {
        "index": "kibana_sample_data_logs",
        "alias": "logs"
      }
    }
    ]
}'

Executing the request above should return an output:

{
  "acknowledged": true
}

Once we have created the alias for the index, we can use the alias with various endpoints just like we would with the regular index name.

Elasticsearch Delete Alias

There are two methods of deleting an existing alias. The first is to use the alias API and use the REMOVE action.

The request syntax is as shown below:

POST _aliases
{
  "actions": [
    {
      "remove": {
        "index": "<target_resource?",
        "alias": ""
      }
    }
  ]
}

For example, suppose we want to remove the “log” alias from the “kibana_sample_data_logs” index. Then, we can run the query as:

curl -XPOST "http://localhost:9200/_aliases" -H "kbn-xsrf: reporting" -H "Content-Type: application/json" -d'
{
  "actions": [
    {
      "remove": {
        "index": "kibana_sample_data_logs",
        "alias": "logs"
      }
    }
  ]
}'

In the example above, we use the remove action in the alias API to delete the alias “logs” from the specified index.

If successful, the request should return an acknowledgement message.

{
  "acknowledged": true
}

The delete alias API is the second method we can use to delete an existing alias. This allows us to use the DELETE HTTP method followed by the index or data stream and the alias we wish to remove,

The request syntax is as shown:

DELETE <target_resource>/_alias/<target_alias>

For example, to remove the logs alias, we can run:

curl -XDELETE "http://localhost:9200/kibana_sample_data_logs/_alias/logs" -H "kbn-xsrf: reporting"

Ensure the target alias exists in the cluster. Otherwise, Elasticsearch will return an “alias not found” exception.

Conclusion

This article taught you the basics of creating an alias on an Elasticsearch index or data stream. You also learned two main methods of deleting an existing alias.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list