0
0
ElasticsearchHow-ToBeginner · 3 min read

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 of PUT /_aliases which is incorrect for alias creation.
  • Trying to create an alias for a non-existing index.
  • Not using the actions array 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

OperationDescriptionExample
Add aliasCreate an alias for an indexPUT /_aliases {"actions":[{"add":{"index":"idx","alias":"alias"}}]}
Remove aliasDelete an alias from an indexPOST /_aliases {"actions":[{"remove":{"index":"idx","alias":"alias"}}]}
Check aliasGet indices for an aliasGET /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.