elasticsearch es 索引重建

ElasticSearch索引重建

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

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

  {
    "error" : {
      "root_cause" : [
        {
          "type" : "illegal_argument_exception",
          "reason" : "mapper [age] cannot be changed from type [integer] to [long]"
        }
      ],
      "type" : "illegal_argument_exception",
     "reason" : "mapper [age] cannot be changed from type [integer] to [long]"
   },
   "status" : 400
 }

因此,如果项目中有需求需要修改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 可以删也可以不删

原文地址:https://www.cnblogs.com/-mrl/p/15293111.html