更新时间:2024-11-29 GMT+08:00

写入数据和已有type不一致导致写入失败

问题背景与现象

客户端写入数据失败,报错“illegal_argument_exception,final mapping would have more than 1 type”。

curl -XGET --tlsv1.2 --negotiate -k  -u :  "https://ip:httpport/test_index/test_type2/id111?pretty" -H 'Content-Type: application/json' -d' { "gcsj": "2018-10-13 10:20:05","bh": "2fdc8d48-45d5-46c3-8550-de4400a87c1c"}'
{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "Rejecting mapping update to [test_index] as the final mapping would have more than 1 type: [test_type1, test_type2]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "Rejecting mapping update to [test_index] as the final mapping would have more than 1 type: [test_type1, test_type2]"
  },
  "status" : 400
}

原因分析

每一条写入数据 "https://ip:httpport/test_index/test_type2/id111" 中,在Elasticsearch节点信息以外有3个字段,分别是index(如test_index)、type(如test_type)和id(如id111)。Elasticsearch 6.X版本增加内核约束,一个index 对应一个type。该日志的含义是如果写入了这条数据将导致该index的type和已存在的type不一致,因此写入失败。

可以使用命令查看index的mapping情况,确认已有的type名称,以及新写入数据的type和已有type是否一样。

curl -XGET --tlsv1.2 --negotiate -k  -u :  "https://ip:httpport/test_index/_mapping?pretty"
{
  "test_index" : {
    "mappings" : {
      "test_type1" : {
        "properties" : {
          "date" : {
            "type" : "date",
            "format" : "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
          },
          "text" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }
}

解决方法

导入数据时,保证一个index中的type一致。