原文地址: https://www.elastic.co/guide/en/elasticsearch/reference/7.7/indices-add-alias.html, 原文档版权归 www.elastic.co 所有

Add index alias APIedit

Creates or updates an index alias.

An index alias is a secondary name used to refer to one or more existing indices.

Most Elasticsearch APIs accept an index alias in place of an index name.

PUT /twitter/_alias/alias1

Requestedit

PUT /<index>/_alias/<alias>

POST /<index>/_alias/<alias>

PUT /<index>/_aliases/<alias>

POST /<index>/_aliases/<alias>

Path parametersedit

<index>

(Required, string) Comma-separated list or wildcard expression of index names to add to the alias.

To add all indices in the cluster to the alias, use a value of _all.

<alias>
(Required, string) Name of the index alias to create or update.

Query parametersedit

master_timeout
(Optional, time units) Specifies the period of time to wait for a connection to the master node. If no response is received before the timeout expires, the request fails and returns an error. Defaults to 30s.
timeout
(Optional, time units) Specifies the period of time to wait for a response. If no response is received before the timeout expires, the request fails and returns an error. Defaults to 30s.

Request bodyedit

filter

(Required, query object) Filter query used to limit the index alias.

If specified, the index alias only applies to documents returned by the filter. Filter query used to limit the index alias.

If specified, the index alias only applies to documents returned by the filter.

routing
(Optional, string) Custom routing value used to route operations to a specific shard.

Examplesedit

Add a time-based aliasedit

The following request creates an alias, 2030, for the logs_20302801 index.

PUT /logs_20302801/_alias/2030

Add a user-based aliasedit

First, create an index, users, with a mapping for the user_id field:

PUT /users
{
    "mappings" : {
        "properties" : {
            "user_id" : {"type" : "integer"}
        }
    }
}

Then add the index alias for a specific user, user_12:

PUT /users/_alias/user_12
{
    "routing" : "12",
    "filter" : {
        "term" : {
            "user_id" : 12
        }
    }
}

Add an alias during index creationedit

You can use the create index API to add an index alias during index creation.

PUT /logs_20302801
{
    "mappings" : {
        "properties" : {
            "year" : {"type" : "integer"}
        }
    },
    "aliases" : {
        "current_day" : {},
        "2030" : {
            "filter" : {
                "term" : {"year" : 2030 }
            }
        }
    }
}