ES中索引的字段类型是不可修改的,只能是重新创建一个索引并设置好mapping,然后再将老索引的数据复制过去
- 查看老索引mapping
GET /twitter/_mappings
- 创建new索引,并指定mapping
PUT /twitter410
{"mappings": {"properties": {"author_id": {"type": "long"},"author_name": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"created_at": {"type": "keyword"},"message": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}}}}
}
- 将老的索引中的数据复制到新的索引中
POST _reindex
{"source":{"index":"twitter" //老索引名},"dest":{"index":"twitter410" //新索引名}
}
- 删除老索引twitter
# curl -XDELETE -u elastic:elasticpasswd http://localhost:9200/index_name
{"acknowledged":true}
- 修改new索引名 为老索引名
法1,通过起别名方式 (建议使用这种方式,不需要多次reindex)
POST _aliases
{"actions": [{"add": {"index": "twitter410","alias": "twitter"}}]
}
法2,再建一个索引叫twitter,然后把twitter410 进行 reindex到twitter;
即执行PUT创建后,再执行POST-reindex
PUT /twitter
{"mappings": {"properties": {"author_id": {"type": "long"},"author_name": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"created_at": {"type": "keyword"},"message": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}}}}
}POST _reindex
{"source": {"index": "twitter410"},"dest": {"index": "twitter"}
}
当新索引结构里字段名 和老索引结构里的不一样时,改怎么迁移数据
场景:twitter2023140这个新索引的信息内容字段是msg_content ,而老索引里是message ,执行数据迁移时需要特殊指定reindex的迁移字段转换逻辑。
POST _reindex
{"source": {"index": "twitter"},"dest": {"index": "twitter2023140"},"script":{"source":"ctx._source.msg_content=ctx._source.remove(\"message\")"}
}