**同步结果摘要**: | 目录 | 同步前 | 同步后 | 官方 | |------|--------|--------|------| | aggregations | 40 | 65 | 64 | | analyzers | 8 | 141 | 140 | | api-reference | 97 | 186 | 174 | | benchmark | 34 | 67 | 53 | | clients | 17 | 18 | 18 | | dashboards | 38 | 149 | 131 | | data-prepper | 77 | 134 | 123 | | im-plugin | 22 | 37 | 37 | | ingest-pipelines | 27 | 52 | 51 | | install-and-configure | 37 | 57 | 53 | | ml-commons-plugin | 81 | 157 | 155 | | observing-your-data | 39 | 73 | 66 | | query-dsl | 40 | 77 | 75 | | search-plugins | 80 | 139 | 95 | | security-analytics | 22 | 47 | 46 | **说明**: - 本地文件数 > 官方:本地有一些官方已删除或重命名的文件 - 新增了约 **740 个英文文档文件** - 已有文件保持不变(未覆盖)
326 lines
7.5 KiB
Markdown
326 lines
7.5 KiB
Markdown
---
|
|
layout: default
|
|
title: Create or update alias
|
|
parent: Alias APIs
|
|
grand_parent: Index APIs
|
|
nav_order: 10
|
|
redirect_from:
|
|
- /api-reference/index-apis/update-alias/
|
|
---
|
|
|
|
# Create Or Update Index Alias API
|
|
**Introduced 1.0**
|
|
{: .label .label-purple }
|
|
|
|
The Create or Update Alias API adds one or more indexes to an index alias or updates the settings for an existing alias. For more information about index aliases, see [Index aliases]({{site.url}}{{site.baseurl}}/im-plugin/index-alias/).
|
|
|
|
The Create or Update Alias API is distinct from the [Manage aliases API]({{site.url}}{{site.baseurl}}/api-reference/alias/aliases-api/), which supports the addition and removal of aliases and the removal of indexes with their aliases. In contrast, the following API only supports adding or updating an alias without updating the index itself. Each API also uses different request body parameters.
|
|
{: .note}
|
|
|
|
## Endpoints
|
|
|
|
```json
|
|
POST /{target}/_alias/{alias-name}
|
|
PUT /{target}/_alias/{alias-name}
|
|
POST /_alias/{alias-name}
|
|
PUT /_alias/{alias-name}
|
|
POST /{target}/_aliases/{alias-name}
|
|
PUT /{target}/_aliases/{alias-name}
|
|
POST /_aliases/{alias-name}
|
|
PUT /_aliases/{alias-name}
|
|
PUT /{target}/_alias
|
|
PUT /{target}/_aliases
|
|
PUT /_alias
|
|
```
|
|
|
|
## Path parameters
|
|
|
|
The following table lists the available path parameters. All path parameters are optional.
|
|
|
|
| Parameter | Type | Description |
|
|
:--- | :--- | :---
|
|
| `target` | String | A comma-delimited list of indexes. Wildcard expressions (`*`) are supported. To target all indexes in a cluster, use `_all` or `*`. Optional. |
|
|
| `alias-name` | String | The alias name to be created or updated. Optional. |
|
|
|
|
## Query parameters
|
|
|
|
The following table lists the available query parameters. All query parameters are optional.
|
|
|
|
Parameter | Type | Description
|
|
:--- | :--- | :---
|
|
`cluster_manager_timeout` | Time | The amount of time to wait for a response from the cluster manager node. Default is `30s`.
|
|
`timeout` | Time | The amount of time to wait for a response from the cluster. Default is `30s`.
|
|
|
|
## Request body
|
|
|
|
The following table lists the available request body fields.
|
|
|
|
Field | Type | Description
|
|
:--- | :--- | :--- | :---
|
|
`index` | String | A comma-delimited list of indexes that you want to associate with the alias. If this field is set, it will override the index name specified in the URL path.
|
|
`alias` | String | The name of the alias. If this field is set, it will override the alias name specified in the URL path.
|
|
`is_write_index` | Boolean | Specifies whether the index should be a write index. An alias can only have one write index at a time. If a write request is submitted to an alias that links to multiple indexes, then OpenSearch runs the request only on the write index.
|
|
`routing` | String | Assigns a custom value to a shard for specific operations.
|
|
`index_routing` | String | Assigns a custom value to a shard only for index operations.
|
|
`search_routing` | String | Assigns a custom value to a shard only for search operations.
|
|
`filter` | Object | A filter to use with the alias so that the alias points to a filtered part of the index.
|
|
|
|
## Example request: Add a simple alias
|
|
|
|
The following request creates a basic alias for an index:
|
|
|
|
<!-- spec_insert_start
|
|
component: example_code
|
|
rest: PUT /products-2024/_alias/current-products
|
|
-->
|
|
{% capture step1_rest %}
|
|
PUT /products-2024/_alias/current-products
|
|
{% endcapture %}
|
|
|
|
{% capture step1_python %}
|
|
|
|
|
|
response = client.indices.put_alias(
|
|
name = "current-products",
|
|
index = "products-2024"
|
|
)
|
|
|
|
{% endcapture %}
|
|
|
|
{% include code-block.html
|
|
rest=step1_rest
|
|
python=step1_python %}
|
|
<!-- spec_insert_end -->
|
|
|
|
## Example request: Add a time-based alias
|
|
|
|
The following request creates an alias `quarterly-2024` for the `sales-q1-2024` index:
|
|
|
|
<!-- spec_insert_start
|
|
component: example_code
|
|
rest: PUT /sales-q1-2024/_alias/quarterly-2024
|
|
-->
|
|
{% capture step1_rest %}
|
|
PUT /sales-q1-2024/_alias/quarterly-2024
|
|
{% endcapture %}
|
|
|
|
{% capture step1_python %}
|
|
|
|
|
|
response = client.indices.put_alias(
|
|
name = "quarterly-2024",
|
|
index = "sales-q1-2024"
|
|
)
|
|
|
|
{% endcapture %}
|
|
|
|
{% include code-block.html
|
|
rest=step1_rest
|
|
python=step1_python %}
|
|
<!-- spec_insert_end -->
|
|
|
|
## Example request: Add a filtered alias with routing
|
|
|
|
First, create an index with appropriate mappings:
|
|
|
|
<!-- spec_insert_start
|
|
component: example_code
|
|
rest: PUT /customer-data
|
|
body: |
|
|
{
|
|
"mappings" : {
|
|
"properties" : {
|
|
"customer_id" : {"type" : "integer"},
|
|
"region" : {"type" : "keyword"}
|
|
}
|
|
}
|
|
}
|
|
-->
|
|
{% capture step1_rest %}
|
|
PUT /customer-data
|
|
{
|
|
"mappings": {
|
|
"properties": {
|
|
"customer_id": {
|
|
"type": "integer"
|
|
},
|
|
"region": {
|
|
"type": "keyword"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
{% endcapture %}
|
|
|
|
{% capture step1_python %}
|
|
|
|
|
|
response = client.indices.create(
|
|
index = "customer-data",
|
|
body = {
|
|
"mappings": {
|
|
"properties": {
|
|
"customer_id": {
|
|
"type": "integer"
|
|
},
|
|
"region": {
|
|
"type": "keyword"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
)
|
|
|
|
{% endcapture %}
|
|
|
|
{% include code-block.html
|
|
rest=step1_rest
|
|
python=step1_python %}
|
|
<!-- spec_insert_end -->
|
|
|
|
Then add the index alias for a specific customer with routing and filtering:
|
|
|
|
<!-- spec_insert_start
|
|
component: example_code
|
|
rest: PUT /customer-data/_alias/customer-123
|
|
body: |
|
|
{
|
|
"routing" : "west",
|
|
"filter" : {
|
|
"term" : {
|
|
"customer_id" : 123
|
|
}
|
|
}
|
|
}
|
|
-->
|
|
{% capture step1_rest %}
|
|
PUT /customer-data/_alias/customer-123
|
|
{
|
|
"routing": "west",
|
|
"filter": {
|
|
"term": {
|
|
"customer_id": 123
|
|
}
|
|
}
|
|
}
|
|
{% endcapture %}
|
|
|
|
{% capture step1_python %}
|
|
|
|
|
|
response = client.indices.put_alias(
|
|
name = "customer-123",
|
|
index = "customer-data",
|
|
body = {
|
|
"routing": "west",
|
|
"filter": {
|
|
"term": {
|
|
"customer_id": 123
|
|
}
|
|
}
|
|
}
|
|
)
|
|
|
|
{% endcapture %}
|
|
|
|
{% include code-block.html
|
|
rest=step1_rest
|
|
python=step1_python %}
|
|
<!-- spec_insert_end -->
|
|
|
|
## Example request: Add an alias during index creation
|
|
|
|
You can add an alias when creating an index using the create index API:
|
|
|
|
<!-- spec_insert_start
|
|
component: example_code
|
|
rest: PUT /inventory-2024
|
|
body: |
|
|
{
|
|
"mappings" : {
|
|
"properties" : {
|
|
"category" : {"type" : "keyword"}
|
|
}
|
|
},
|
|
"aliases" : {
|
|
"current-inventory" : {},
|
|
"electronics" : {
|
|
"filter" : {
|
|
"term" : {"category" : "electronics" }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-->
|
|
{% capture step1_rest %}
|
|
PUT /inventory-2024
|
|
{
|
|
"mappings": {
|
|
"properties": {
|
|
"category": {
|
|
"type": "keyword"
|
|
}
|
|
}
|
|
},
|
|
"aliases": {
|
|
"current-inventory": {},
|
|
"electronics": {
|
|
"filter": {
|
|
"term": {
|
|
"category": "electronics"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
{% endcapture %}
|
|
|
|
{% capture step1_python %}
|
|
|
|
|
|
response = client.indices.create(
|
|
index = "inventory-2024",
|
|
body = {
|
|
"mappings": {
|
|
"properties": {
|
|
"category": {
|
|
"type": "keyword"
|
|
}
|
|
}
|
|
},
|
|
"aliases": {
|
|
"current-inventory": {},
|
|
"electronics": {
|
|
"filter": {
|
|
"term": {
|
|
"category": "electronics"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
)
|
|
|
|
{% endcapture %}
|
|
|
|
{% include code-block.html
|
|
rest=step1_rest
|
|
python=step1_python %}
|
|
<!-- spec_insert_end -->
|
|
|
|
## Example response
|
|
|
|
```json
|
|
{
|
|
"acknowledged": true
|
|
}
|
|
```
|
|
|
|
## Required permissions
|
|
|
|
If you use the Security plugin, make sure you have the appropriate permissions: `indices:admin/aliases`.
|
|
|
|
## Related documentation
|
|
|
|
For more information about index aliases, see [Index aliases]({{site.url}}{{site.baseurl}}/im-plugin/index-alias/).
|