如同磁铁吸引四周的铁粉,热情也能吸引周围的人,改变周围的情况。

ES 7.8 能够将 long 类型的字段 reindex 成 keyword 类型的字段么?

Elasticsearch | 作者 hapjin | 发布于2021年03月12日 | 阅读数:1981

face_v1 是旧索引,字段 custom.serial_number是 long 类型。mapping结构参考如下:


      "properties" : {
        "createTime" : {
          "type" : "date",
          "format" : "yyyy-MM-dd HH:mm:ss"
        },
        "custom" : {
          "properties" : {
            "groupID" : {
              "type" : "keyword"
            },
            "projectID" : {
              "type" : "keyword"
            },
            "serial_number" : {
              "type" : "long"
            },
            "upload_time" : {
              "type" : "keyword"
            }
          }
        }


 
我新建了一个索引 face_v2,将 custom.serial_number 设置成了 keyword,然后参考 convert-the-long-datatype-field-to-string,在kibina console 里面执行如下reindex命令:(其中 custom.serial_number是 object 类型)


POST _reindex?pretty
{
  "source": {
    "index": "face_v1"
  },
  "dest": {
    "index": "face_v2"
  },
  "script": {
    "source": "ctx._source.custom.serial_number = String.valueOf(ctx._source.custom.serial_number)"
  }
}


却报错了。错误如下,看错误注释貌似是 script 脚本 不支持 object field type 层级字段的 reindex ?


    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "runtime error",
        "script_stack" : [
          "ctx._source.custom.serial_number = String.valueOf(ctx._source.serial_number)",
          "                  ^---- HERE"
        ],
        "script" : "ctx._source.custom.serial_number = String.valueOf(ctx._source.serial_number)",
        "lang" : "painless",
        "position" : {
          "offset" : 18,
          "start" : 0,
          "end" : 76
        }
      }
    ]


已邀请:

Joshua

赞同来自:

不带script直接reindex试试

zmc - ES PAAS、JuiceFS

赞同来自:

可以的,reindex本质就是重新写,只要定义好mapping,数据能够使用keyword类型就可以reindex~

要回复问题请先登录注册