在ES中无法直接增删索引中的字段,只能通过新建索引的方式迁移数据
具体流程
- 创建新索引
index_temp
- 将旧索引
index
数据迁移至index_temp
post _reindex?wait_for_completion=false { "source":{ "index":"index", "query":{ "range": { "dt":{ "gte": 10 } } }, "size":20000 }, "dest":{ "index":"index_temp" } }
说明:
如果数据量过大,会响应超时,默认超时时间为30s
{"statusCode":502,"error":"Bad Gateway","message":"Client request timeout"}
因此需要加入wait_for_completion=false后台执行
在同步时需要保证被删除的字段为空,否则index_temp会自动创建字段
{
"query": {
"bool": {
"must_not": {
"exists": {
"field": "my_field"
}
}
}
}
}
- 删除
index
并重新创建
DELETE index/
PUT /my_index
{
"mappings": {
"properties": {
"dataId": {
"type": "long"
},
"dataTime": {
"type": "date"
},
"dataValue": {
"type": "float"
},
"dataName": {
"type": "keyword"
},
"deviceName": {
"type": "keyword"
},
"dataUnit": {
"type": "keyword"
},
"projectId": {
"type": "long"
}
}
}
}
- 再将
index_temp
重新导入index
即可