资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

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

创新互联2013年至今,先为涞水等服务建站,涞水等地企业,进行企业商务咨询服务。为涞水企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

在 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 不一致导致写入数据失败的问题了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


网站标题:怎样解决Elasticsearchtype不一致导致写入数据失败的问题
文章分享:http://cdkjz.cn/article/jgihji.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220