Reindex 操作
本章定位:Reindex 是 ES 运维的"救命稻草"——当需要修改 Mapping、调整分片数或迁移数据时,Reindex 是唯一的方案。
定义与作用
Reindex 是将一个索引的数据复制到另一个索引的过程。源索引的文档不变,目标索引可以有不同的 Settings 和 Mappings。
解决的痛点:ES 的主分片数和已有字段的 Mapping 类型创建后不可修改。需要重新设计时,只能通过 Reindex 把数据迁移到新索引。
完整示例:飞翔科技的索引迁移
场景一:杨英的 Mapping 变更
杨英管理的 courses 索引需要新增一个 enrollment_count 字段为 integer 类型:
# 步骤 1:创建新索引
PUT /courses_v2
{
"settings": { "number_of_shards": 5 },
"mappings": {
"properties": {
"title": { "type": "text", "analyzer": "ik_max_word" },
"teacher": { "type": "keyword" },
"category": { "type": "keyword" },
"price": { "type": "float" },
"enrollment_count": { "type": "integer" },
"publish_date": { "type": "date" }
}
}
}
# 步骤 2:Reindex
POST /_reindex
{
"source": { "index": "courses_v1" },
"dest": { "index": "courses_v2" }
}
小结
Reindex 三步走:创建目标索引(正确 Mapping)→ 执行 _reindex → 验证数据量 → 别名切换。对于生产环境大数据量(百万级+),建议用 wait_for_completion=false 异步执行并监控进度。