How to Create Index Alias in Elasticsearch Quickly
To create an index alias in Elasticsearch, use the
PUT /_aliases API with a JSON body specifying the alias name and the target index. This lets you refer to one or more indices by a single alias name for easier querying and management.Syntax
The basic syntax to create an alias uses the PUT /_aliases endpoint with a JSON body. You specify the index and the alias name under the actions array.
- actions: List of operations to perform.
- add: Operation to add an alias.
- index: The name of the existing index.
- alias: The name of the alias to create.
json
PUT /_aliases
{
"actions": [
{
"add": {
"index": "my_index",
"alias": "my_alias"
}
}
]
}Example
This example creates an alias named logs_alias for the index logs_2024. After this, you can query logs_alias instead of the full index name.
json
PUT /_aliases
{
"actions": [
{
"add": {
"index": "logs_2024",
"alias": "logs_alias"
}
}
]
}Output
{
"acknowledged": true
}
Common Pitfalls
Common mistakes when creating aliases include:
- Using
PUT /{alias}instead ofPUT /_aliaseswhich is incorrect for alias creation. - Trying to create an alias for a non-existing index.
- Not using the
actionsarray format, which is required.
Always check the index exists before adding an alias.
json
### Wrong way (missing actions array):
PUT /_aliases
{
"add": {
"index": "my_index",
"alias": "my_alias"
}
}
### Right way:
PUT /_aliases
{
"actions": [
{
"add": {
"index": "my_index",
"alias": "my_alias"
}
}
]
}Quick Reference
| Operation | Description | Example |
|---|---|---|
| Add alias | Create an alias for an index | PUT /_aliases {"actions":[{"add":{"index":"idx","alias":"alias"}}]} |
| Remove alias | Delete an alias from an index | POST /_aliases {"actions":[{"remove":{"index":"idx","alias":"alias"}}]} |
| Check alias | Get indices for an alias | GET /alias_name |
Key Takeaways
Use the PUT /_aliases API with an actions array to create index aliases.
Aliases let you query one or more indices with a simple name.
Always ensure the target index exists before adding an alias.
The JSON body must include the add action inside an actions array.
Check your alias with GET /alias_name to confirm creation.