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.
{
"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:
{
"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:
{
"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:
{
"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:
For example, to remove the logs alias, we can run:
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.