怎样解决Elasticsearch type 不一致导致写入数据失败的问题

发布时间:2021-12-06 15:01:18 作者:柒染
来源:亿速云 阅读:248

这期内容当中小编将会给大家带来有关怎样解决Elasticsearch type 不一致导致写入数据失败的问题,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

在 Elasticsearch 7.x 以前的版本中,当ES Client写数据的时候报了如下错误:

2020-03-13 10:00:41.076 ERROR 9 --- [Report ES Thread 0] .g.c.c.AbstractElasticsearchReportClient : item: 2634ef87-ec48-4d38-899f-508ba8b69b9c, errorReason: Rejecting mapping update to [journal_test] as the final mapping would have more than 1 type: [default, doc]

意思就是说你写入的数据的中的type 与你创建索引时指定type不一致。比如我创建时 "_type":"_doc",而写入时为 "_type":"default"。因此,有两种办法,要么修改写入数据时的 type ,要么修改当前索引的 type 。

不过在最新的 7.x 没有该问题了,因为 官方已经把 type 功能移除了。

考虑到更改写入时候的 type 就得重启应用,会影响用户使用。所以这里采用修改当前索引的方法。

提供一种最简单的解决思路:通过 Elasticsearch 的 reindex 功能将当前索引 journal_test 备份至 journal_test_back 索引并删除 journal_test,让 ES 根据写入数据时自动生成 type 为 default 的索引。随后将备份数据导出到文件,修改文件中 type 为 default。再写入原来新的 journal_test_2 索引。

 

将 journal_test_2 索引数据备份至 journal_test_2_bak索引:

curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
{
 "source": {
   "index": "journal_test"
 },
 "dest": {
   "index": "journal_test_back"
 }
}
'
   

删除 journal_test 索引,让你的应用再次写入数据时 ES 自己创建索引:

curl -X DELETE "http://localhost:9200/journal_test"
   

journal_test_back 数据导出到 my_index_mapping.json 文件中

这里借助工具:https://github.com/taskrabbit/elasticsearch-dump,将 备份数据导出到我本地 Desktop 目录下:

docker run --rm -ti -v ~/Desktop:/tmp taskrabbit/elasticsearch-dump \
--input=http://localhost:9200/journal_test_back \
--output=/tmp/my_index_mapping.json \
--type=data
 

编辑 my_index_mapping.json 文件,将 type 修改为 default。注意,为了不影响原来的备份数据,我将修改后的数据写入到新的文件(my_index_mapping_default.json)中:

cat my_index_mapping.json| sed s/\"_type\":\"doc\"/\"_type\":\"default\"/ > my_index_mapping_default.json
   

最后一步,将正确的数据导入正确的 journal_test 索引中:

docker run --rm -ti -v ~/Desktop:/tmp taskrabbit/elasticsearch-dump \
--output=http://localhost:9200/journal_test \ # 注意索引名
--input=/tmp/my_index_mapping_default.json \ #注意这里使用的文件
--type=data
   

上述就是小编为大家分享的怎样解决Elasticsearch type 不一致导致写入数据失败的问题了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 一、Elasticsearch原理与基本使用
  2. 贷前系统ElasticSearch实践总结

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

elasticsearch type

上一篇:ASP.NET5 MVC6中的新特性是什么

下一篇:如何使用SecureCRT连接ubuntu16.04

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》