Docs/ELK/es学习记录10-索引别名与零停机.md
2022-10-18 16:59:37 +08:00

64 lines
No EOL
2.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

索引 别名 就像一个快捷方式或软连接可以指向一个或多个索引也可以给任何一个需要索引名的API来使用。别名 带给我们极大的灵活性,允许我们做下面这些:
在运行的集群中可以无缝的从一个索引切换到另一个索引
给多个索引分组 (例如, last_three_months)
给索引的一个子集创建 视图
有两种方式管理别名: _alias 用于单个操作, _aliases 用于执行多个原子级操作。
在本章中,我们假设你的应用有一个叫 my_index 的索引。事实上, my_index 是一个指向当前真实索引的别名。真实索引包含一个版本号: my_index_v1 my_index_v2 等等。
首先,创建索引 my_index_v1 ,然后将别名 my_index 指向它:
```
PUT /my_index_v1 ## 创建索引 my_index_v1
PUT /my_index_v1/_alias/my_index ## 设置别名 my_index 指向 my_index_v1
```
你可以检测这个别名指向哪一个索引
```
GET /*/_alias/my_index
```
或哪些别名指向这个索引:
```
GET /my_index_v1/_alias/*
```
两者都会返回下面的结果
```
{
"my_index_v1" : {
"aliases" : {
"my_index" : { }
}
}
}
```
然后,我们决定修改索引中一个字段的映射。当然,我们不能修改现存的映射,所以我们必须重新索引数据。 首先, 我们用新映射创建索引 my_index_v2
```
PUT /my_index_v2
{
"mappings": {
"my_type": {
"properties": {
"tags": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
```
然后我们将数据从 my_index_v1 索引到 my_index_v2 ,下面的过程在 重新索引你的数据 中已经描述过。一旦我们确定文档已经被正确地重索引了,我们就将别名指向新的索引。
一个别名可以指向多个索引,所以我们在添加别名到新索引的同时必须从旧的索引中删除它。这个操作需要原子化,这意味着我们需要使用 _aliases 操作
```
POST /_aliases
{
"actions": [
{ "remove": { "index": "my_index_v1", "alias": "my_index" }},
{ "add": { "index": "my_index_v2", "alias": "my_index" }}
]
}
```
你的应用已经在零停机的情况下从旧索引迁移到新索引了。