es刪除字段
原索引mappings如下,有full_name和short_name兩個字段
{ "audit_demo": { "mappings": { "_doc": { "properties": { "full_name": { "type": "text", "analyzer": "ik_max_word" }, "short_name": { "type": "keyword" } } } } } }
想要刪掉short_name字段,修改后mappings如下
{ "audit_demo_bak": { "mappings": { "_doc": { "properties": { "full_name": { "type": "text", "analyzer": "ik_max_word" } } } } } }
步驟1 刪除原索引中待刪除字段的數據
注意
1.只是刪除數據,不是刪除字段
2.如果不刪除字段數據,后面reindex時依然會把待刪除字段的值帶到新索引,即使設置新索引的dynamic為false
POST {{di}}/audit_demo/_update_by_query
{
"script": {
"lang": "painless",
"inline": "ctx._source.remove(\"short_name\")"
},
"query": {
"match_all": {}
}
}
步驟2 新建一個索引,索引結構在原索引上刪除short_name字段
PUT {{di}}/audit_demo_bak
{
"settings": {
"number_of_shards": "2",
"number_of_replicas": "2",
"max_result_window": 100000,
"analysis": {
"analyzer": {
"ik": {
"tokenizer": "ik_max_word"
}
}
}
},
"mappings": {
"_doc": {
"properties": {
"full_name": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
步驟3 同步數據
POST {{di}}/_reindex
{
"source": {
"index": "audit_demo"
},
"dest": {
"index": "audit_demo_bak"
}
}
步驟4 刪除原索引
DELETE {{di}}/audit_demo
步驟5 新建一個名為原索引名的索引,reindex同步數據,然后刪除步驟2新建的索引
注意事項
步驟4和步驟5,這兩個步驟,耗時較長,在這段時間,索引是不可用的,一般在業務低峰期執行操作沒啥問題。如果想要減少索引不可用的時間,有以下兩個方案
方案1
刪除原索引后,為新索引設置別名為原索引
備注:原索引未刪除時,為新索引設置別名為原索引會報錯
PUT {{di}}/audit_demo_bak/_alias/audit_demo
方案2
步驟1開始之前,就為原索引設置別名,應用程序通過別名訪問索引,步驟4開始之前,刪除原索引與別名的關系,新增新索引與別名的關系,應用程序通過別名可以訪問到新索引。
雖然這兩個步驟的耗時極小,但是還是有可能在這段期間有數據更改,所以還是盡量在業務低峰期操作。