【ElasticSearch】索引重建


ElasticSearch索引重建

ElasticSearch索引一旦建立,便不可修改索引字段类型(允许增加或者删除该字段)

例如从Integer类型修改为long类型,这是不被允许的,错误信息如下:

 1 {
 2   "error" : {
 3     "root_cause" : [
 4       {
 5         "type" : "illegal_argument_exception",
 6         "reason" : "mapper [age] cannot be changed from type [integer] to [long]"
 7       }
 8     ],
 9     "type" : "illegal_argument_exception",
10     "reason" : "mapper [age] cannot be changed from type [integer] to [long]"
11   },
12   "status" : 400
13 }

因此,如果项目中有需求需要修改ElasticSearch的索引中字段的类型,则需要重建索引

以下介绍通过别名_aliase的方式实现不停机的重建ElasticSearch索引

前提:项目中使用的索引的别名,例如索引是index_user ,使用的是别名index_user_latest

 

索引重建的步骤:

step1: 创建新索引index_user_20210123并初始化映射关系,映射也就是包含了修改后的字段类型;

step2: 使用 _reindex 将原索引index_user中的数据,导入到新索引index_user_20210123;

step3: aliases actions修改别名: 将index_user的别名index_user_latest移除,index_user_20210123新增别名index_user_latest

以上便完成了修改字段类型的数据迁移

 

备注:

1.旧的索引index_user 可以删也可以不删

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM