将索引的映射从简单映射更新为对象映射

Update mapping of an index from a simple mapping to an object mapping

大家好,我是 elasticsearch 7.9 更新映射的新手,我遇到了一个用例,我必须将一个简单的字段映射更新为一个对象映射,在我应用查询之前,我创建了一个简单的示例来测试我的目的,这是我的初始索引映射:

{
  "myindex" : {
    "mappings" : {
      "properties" : {
        "flag" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "title" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

我想将字段名称 flag 更新为 tag 并向其添加一个名为 id[ 的嵌套字段=33=] 将包含初始索引 myindex 字段 flag 的值,我执行了以下步骤:

  1. 使用以下映射创建了新索引 mynewindex
PUT mynewindex/_mapping
{
"properties" : {
        "tag" : {
          "properties": {
            "id" : {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword"
                }
              }
            }
          }
        },
        "title" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword"
            }
          }
        }
      }
}
  1. 并使用重建索引 API:
POST _reindex
{
  "source": {
    "index": "myindex"
  },
  "dest": {
    "index": "mynewindex"
  },
  "script": {
    "source": "ctx._source.tag.id = ctx._source.remove(\"flag\")"
  }
}

这给了我这个错误:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "runtime error",
        "script_stack" : [
          "ctx._source.tag.id = ctx._source.remove(\"flag\")",
          "               ^---- HERE"
        ],
        "script" : "ctx._source.tag.id = ctx._source.remove(\"flag\")",
        "lang" : "painless",
        "position" : {
          "offset" : 15,
          "start" : 0,
          "end" : 47
        }
      }
    ],
    "type" : "script_exception",
    "reason" : "runtime error",
    "script_stack" : [
      "ctx._source.tag.id = ctx._source.remove(\"flag\")",
      "               ^---- HERE"
    ],
    "script" : "ctx._source.tag.id = ctx._source.remove(\"flag\")",
    "lang" : "painless",
    "position" : {
      "offset" : 15,
      "start" : 0,
      "end" : 47
    },
    "caused_by" : {
      "type" : "null_pointer_exception",
      "reason" : "Cannot invoke \"Object.getClass()\" because \"callArgs[0]\" is null"
    }
  },
  "status" : 400
}

在 elastic doc 中找到的无痛脚本,正如我预期的那样,点符号可以工作,有任何建议或解决方法可以让它工作!

由于标签为空,它给出了空指针,您必须先创建一个地图。

下面应该可行

POST _reindex
{
  "source": {
    "index": "myindex"
  },
  "dest": {
    "index": "mynewindex"
  },
  "script" : {
    "source": """
        if(ctx._source.tag == null) {
          ctx._source.tag = new HashMap();
        }
        if(ctx._source.flag != null) {
          ctx._source.tag.id = ctx._source.remove("flag")
        }
      """,
    "lang": "painless"
  }
}