背景

在对外提供服务的线上环境中,发现 Elasticsearch 集群中核心业务涉及的索引设计不合理,需要做数据迁移,但不允许重启服务。

方案

使用 Alias 别名对外提供服务。

新建索引并设定好 Mapping,然后进行数据 reindex 迁移操作。

例如,设置索引 my_index 的 Alias 别名为 my_index_alias

json
1
2
3
4
5
6
7
8
9
10
11
PUT my_index
{
"aliases": {
"my_index_alias": {}
},
"settings": {
"refresh_interval": "30s",
"number_of_shards": 1,
"number_of_replicas": 0
}
}

创建新索引 my_index_v2,根据需要调整 mapping 配置。

json
1
2
3
4
5
6
7
8
9
PUT my_index_v2
{
"aliases": {},
"settings": {
"refresh_interval": "30s",
"number_of_shards": 3,
"number_of_replicas": 0
}
}

将数据从旧索引 my_index 重索引到新索引 my_index_v2

json
1
2
3
4
5
6
7
8
9
POST _reindex
{
"source": {
"index": "my_index"
},
"dest": {
"index": "my_index_v2"
}
}

当确认新索引已经准备就绪,并且所有数据都已经成功迁移后,更新别名 my_index_alias 以指向新索引 my_index_v2

json
1
2
3
4
5
6
7
POST _aliases
{
"actions": [
{ "remove": { "index": "my_index", "alias": "my_index_alias" } },
{ "add": { "index": "my_index_v2", "alias": "my_index_alias" } }
]
}

reindex 迁移索引到 aliases 更换别名指向的期间,如果有业务往旧索引 my_index写入数据,可能会导致数据不一致,建议将 POST _reindexPOST _aliases 放在一起执行,并在低峰期执行。