由于ElasticSearch没有像数据库一样可以直接字段数据类型的方法,因此需要通过创建中间索引:data_index_1,备份数据到中间索引:data_index_1,然后删除原索引: data_index,重新创建正确数据类型索引:data_index,再把中间索引:data_index_1的数据备份到新创建索引:data_index。语句通过kibana的 dev_tools/console 执行。
索引mapping,可通过到Kibana查看索引的mapping,这是我们需要修改的字段rcvtime的类型
操作系统:CentOS7.4
ES版本:elasticsearch-7.16.3
kibana版本:kibana-7.16.3
操作步骤:
1.创建一个中间索引
2.向中间索引备份源索引的数据(mapping)
3.查询确认数据是否copy过去
4.删除有问题的索引
5.重新创建同名的索引(★字段类型修改正确★)
6.从中间索引还原到源索引的数据
7.删除中间索引
参考脚本,在kibana执行:
# 1. 创建一个中间索引
# 创建索引和设置正确的字段Mapping
PUT mail_center.received_1
{
"settings": {
"index": {
"max_result_window" : 100000000
},
"number_of_shards": 3,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"rcvtime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"fetchtime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"gmcrcvtime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}
# 2. 向中间索引备份源索引的数据
# 重建索引
POST _reindex
{
"source": {
"index": "mail_center.received"
},
"dest": {
"index": "mail_center.received_1"
}
}
# 3.查询确认数据是否copy过去
GET /mail_center.received/_doc/_search
GET /mail_center.received_1/_doc/_search
# 强制执行一次refresh
POST /mail_center.received_1/_refresh
# 4.删除源索引
# 删除源索引
DELETE mail_center.received
# 5.重新创建同名的索引(★字段类型修改正确★)
#创建索引和Mapping
PUT mail_center.received
{
"settings": {
"index": {
"max_result_window" : 100000000
},
"number_of_shards": 3,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"rcvtime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"fetchtime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"gmcrcvtime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}
# 6. 从中间索引还原到源索引的数据
# 重建索引
POST _reindex
{
"source": {
"index": "mail_center.received_1"
},
"dest": {
"index": "mail_center.received"
}
}
# 7. 删除中间索引
DELETE mail_center.received_1
索引文档及数量可以通过kibana的index management查看
http://local.elasticsearch:5601/app/management/data/index_management/indices
如果显示不准确可以在kibana执行强制刷新
POST /mail_center.received_1/_refresh
下图是修改后的字段mapping