在日常开发中,可能会疏忽而未指定字段类型,而es会默认根据插入的数据去映射有可能不符合你期望的类型,从而导致后续的某些问题。由于ElasticSearch不支持直接修改字段类型, 因此需要借助中间索引并搭配_reindex来重新索引数据
操作步骤
- 创建中间索引设置中间索引_mapping_reindex 重新索引数据删除有问题的旧索引重新创建同名新索引(_mapping中字段类型正确)_reindex 中间索引数据至新索引删除中间索引
# 创建中间索引
PUT asr_202203_back/
# 创建Mapping
POST asr_202203_back/asr/_mapping
{
"asr": {
"_source": {
"enabled": true
},
"properties": {
"callId": {
"type": "keyword"
},
"fileName": {
"type": "keyword"
},
"level": {
"type": "integer"
},
"enterpriseId": {
"type": "integer"
},
"tag": {
"type": "keyword"
},
"taskId": {
"type": "keyword"
}
}
}
}
# 向中间索引备份源索引的数据
# 重建索引
POST _reindex
{
"source": {
"index": "asr_202203"
},
"dest": {
"index": "asr_202203_back"
}
}
GET /asr_202203_back/_search
# 删除有问题的索引
DELETE asr_202203
# 重新创建同名的索引
# 创建索引
PUT asr_202203/
# 创建Mapping
POST asr_202203/asr/_mapping
{
"asr": {
"_source": {
"enabled": true
},
"properties": {
"callId": {
"type": "keyword"
},
"fileName": {
"type": "keyword"
},
"level": {
"type": "integer"
},
"enterpriseId": {
"type": "integer"
},
"tag": {
"type": "keyword"
},
"taskId": {
"type": "keyword"
}
}
}
}
# 从中间索引还原到源索引的数据
# 重建索引
POST _reindex
{
"source": {
"index": "asr_202203_back"
},
"dest": {
"index": "asr_202203"
}
}
# 查询验证是否正确
GET asr_202203_alias/_search
GET asr_202203_alias/_mapping
# 删除中间索引
DELETE asr_202203_back



