ES 插入文档报错:Rejecting mapping update to [testindex] as the final mapping would have more than 1 type

  笔者在用 curl 命令行对 ElasticSearch 进行文档插入时,报了如下错误。

[estestuser@vm-10-201-42-9 ~]$ curl -u elastic -H "Content-Type: application/json" -XPOST 'http://localhost:9200/testindex/5?pretty' -d '{"mchnt_id" : "5"}'
Enter host password for user 'elastic':
{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "Rejecting mapping update to [testindex] as the final mapping would have more than 1 type: [_doc, 5]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "Rejecting mapping update to [testindex] as the final mapping would have more than 1 type: [_doc, 5]"
  },
  "status" : 400
}

  其中,testindex 为 ElasticSearch 的索引,文档 ID 为 5,在插入文档时,报错:Rejecting mapping update to [testindex] as the final mapping would have more than 1 type: [_doc, 5]。测试环境使用的是 7.1.1 版本的 ElasticSearch,经查阅,原因是 Elasticsearch 在 7.x 版本中已经去除 type 的概念。

  type 的版本迭代:

  • 5.x 及以前版本,一个 index 有一个或者多个 type
  • 6.x 版本,一个 index 只有一个 type
  • 7.x 版本移除了 type,type 相关的所有内容全部变成 Deprecated,为了兼容升级和过渡,所有的 7.x 版本 es 数据写入后 type 字段都默认被置为 “_doc”
  • 8.x 版本完全废弃 type

  下图是 ElasticSearch 官网上关于 type 的演进历程。
在这里插入图片描述
  1、为何要去除 type 的概念?

  因为 Elasticsearch 设计初期,是直接参考了关系型数据库的设计模式,存在了 type(数据表)的概念。
  在关系型数据库里,table 是相互独立的,一个 table 里的 column 和另外一个 table 的同名 column 没有关系,互不影响。但在 type 里字段却不是这样的,在一个 Elasticsearch 的 index 里,所有不同 type 的同名 field 内部使用的是同一个 lucene 字段存储。举例来说,teacher 类型的 user_name 字段和 student 类型的 user_name 字段是存储在一个 field 里的,两个 type 里的 user_name 必须有一样的 field 定义。
  这可能导致一些问题,例如你希望同一个索引中 “create_time” 字段在一个类型里是存储日期值,在另外一个类型里存储字符串。另外,按照 Lucene 存储的方式和特点,如果按照此种方式组织数据,会影响到底层的压缩比例和存储效率。
  Lucene 的全文检索功能之所以快,是因为 “倒序索引” 的存在。而这种倒序索引的生成是基于 index 的,而非 type。多个 type 反而会减慢搜索的速度。为了保持 Elasticsearch “一切为了搜索” 的宗旨,适当的做些改变(去除 type)也是无可厚非的,也是值得的。

  2、为何不是在 6.X 版本开始就直接去除 type,而是要逐步去除 type?

  因为历史原因,前期 Elasticsearch 支持一个 index 下存在多个 type,而且有很多项目在使用 Elasticsearch 作为数据库。如果直接去除 type 的概念,不仅使很多应用了 Elasticsearch 的项目将面临业务、功能和代码的大改,而且对于 Elasticsearch 官方来说,也是一个巨大的挑战,很多涉及到 type 的源码都要修改。

  综上所述,本次报错的 ES 插入语句修改如下,即解决问题,成功插入一条文档。

[estestuser@vm-10-201-42-19 ~]$ curl -u elastic -H "Content-Type: application/json" -XPOST 'http://localhost:9200/testindex/_doc/5?pretty' -d '{"mchnt_id": "5"}'
Enter host password for user 'elastic':
{
  "_index" : "testindex",
  "_type" : "_doc",
  "_id" : "5",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 5
}
文章参考:
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
这个错误消息表明 Elasticsearch 的可用处理器数量已经被设置为 8,再次尝试将其设置为 8 时被拒绝了。这是由于 Elasticsearch 在初始化时只允许设置一次可用处理器数量,无法重复设置。 解决这个问题的方法是确保只有一处设置了可用处理器数量的地方,并且值是一致的。您可以按照以下步骤进行排查: 1. 检查您的代码,确保只有一处设置了可用处理器数量。可能是在不同的配置文件或代码段中重复设置了该属性。 2. 确保您的依赖库和 Elasticsearch 版本兼容。某些版本的 Elasticsearch 客户端可能对可用处理器数量的设置有限制或者不兼容。 3. 如果您在代码中使用了其他第三方库或者框架,尝试查找是否有类似于设置可用处理器数量的配置项,可能会与 Elasticsearch 的设置冲突。 为了解决这个问题,您可以尝试以下操作: 1. 将所有设置可用处理器数量的地方都移除,并确保只在一个地方进行设置。如果您使用的是 Spring Boot,可以考虑使用 `spring.data.elasticsearch.properties` 属性来集中配置 Elasticsearch 的相关属性。 2. 如果您没有特殊需求,可以尝试删除手动设置可用处理器数量的代码,让 Elasticsearch 使用默认值。 3. 如果您确实需要手动设置可用处理器数量,尝试将其设置为不同于 8 的值,例如 4 或 16,以避免冲突。 请注意,以上解决方案仅为参考,请根据具体情况进行调整。如果问题仍然存在,建议查阅 Elasticsearch 的官方文档或者咨询 Elasticsearch 社区获取更详细的帮助。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值