背景
需要删除索引下满足指定条件的文档数据,并将删除的数据进行备份。
操作步骤
新建索引
该索引结构与映射关系与原索引一致
查看原索引设置
java
GET /tb/_settings
结果:
{
"tb" : {
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "tb",
"creation_date" : "1719220554972",
"analysis" : {
"normalizer" : {
"CustomNormalizer" : {
"filter" : [
"lowercase",
"asciifolding"
],
"type" : "custom"
}
},
"analyzer" : {
"optimizeIK" : {
"type" : "custom",
"tokenizer" : "ik_max_word"
}
}
},
"number_of_replicas" : "1",
"uuid" : "ovmNbNg7T6W98WcpqYfQwQ",
"version" : {
"created" : "7170799"
}
}
}
}
}
查看映射关系
java
GET /tb/_mapping
结果:
{
"tb" : {
"mappings" : {
"dynamic" : "true",
"properties" : {
"createTime" : {
"type" : "long",
"coerce" : false
},
"creator" : {
"type" : "keyword"
},
"dbId" : {
"type" : "keyword"
},
"fileName" : {
"type" : "keyword"
},
"id" : {
"type" : "keyword"
},
"isApproved" : {
"type" : "keyword"
},
"modifiedTime" : {
"type" : "long",
"coerce" : false
},
"modifier" : {
"type" : "keyword"
},
"note" : {
"type" : "keyword"
},
"original" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
},
"normalizer" : {
"type" : "keyword",
"normalizer" : "CustomNormalizer"
},
"pattern" : {
"type" : "text",
"norms" : false,
"analyzer" : "pattern"
},
"standard" : {
"type" : "text",
"norms" : false,
"analyzer" : "standard"
}
},
"norms" : false,
"analyzer" : "optimizeIK"
},
"originalLang" : {
"type" : "keyword"
},
"remark" : {
"type" : "keyword"
},
"reviewer" : {
"type" : "keyword"
},
"translation" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
},
"normalizer" : {
"type" : "keyword",
"normalizer" : "CustomNormalizer"
},
"pattern" : {
"type" : "text",
"norms" : false,
"analyzer" : "pattern"
},
"standard" : {
"type" : "text",
"norms" : false,
"analyzer" : "standard"
}
},
"norms" : false,
"analyzer" : "optimizeIK"
},
"translationLang" : {
"type" : "keyword"
}
}
}
}
}
新建索引
java
PUT /tb81901
{
"settings" : {
"index" : {
"analysis" : {
"normalizer" : {
"CustomNormalizer" : {
"filter" : [
"lowercase",
"asciifolding"
],
"type" : "custom"
}
},
"analyzer" : {
"optimizeIK" : {
"type" : "custom",
"tokenizer" : "ik_max_word"
}
}
},
"number_of_shards" : "1",
"number_of_replicas" : "1"
}
},
"mappings": {
"dynamic" : "true",
"properties" : {
"createTime" : {
"type" : "long",
"coerce" : false
},
"creator" : {
"type" : "keyword"
},
"dbId" : {
"type" : "keyword"
},
"fileName" : {
"type" : "keyword"
},
"id" : {
"type" : "keyword"
},
"isApproved" : {
"type" : "keyword"
},
"modifiedTime" : {
"type" : "long",
"coerce" : false
},
"modifier" : {
"type" : "keyword"
},
"note" : {
"type" : "keyword"
},
"original" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
},
"normalizer" : {
"type" : "keyword",
"normalizer" : "CustomNormalizer"
},
"pattern" : {
"type" : "text",
"norms" : false,
"analyzer" : "pattern"
},
"standard" : {
"type" : "text",
"norms" : false,
"analyzer" : "standard"
}
},
"norms" : false,
"analyzer" : "optimizeIK"
},
"originalLang" : {
"type" : "keyword"
},
"remark" : {
"type" : "keyword"
},
"reviewer" : {
"type" : "keyword"
},
"translation" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
},
"normalizer" : {
"type" : "keyword",
"normalizer" : "CustomNormalizer"
},
"pattern" : {
"type" : "text",
"norms" : false,
"analyzer" : "pattern"
},
"standard" : {
"type" : "text",
"norms" : false,
"analyzer" : "standard"
}
},
"norms" : false,
"analyzer" : "optimizeIK"
},
"translationLang" : {
"type" : "keyword"
}
}
}
}
迁移数据
迁移原索引中满足删除条件的文档数据
java
POST /_reindex
{
"source": {
"index": "tb",
"query": {
"bool": {
"must": [
{
"term": {
"dbId": {
"value": "270057"
}
}
}
]
}
}
},
"dest": {
"index": "tb81901"
}
}
删除数据
删除原索引中满足删除条件的文档数据
java
POST /tb/_delete_by_query
{
"query": {
"bool": {
"must": [
{
"terms": {
"dbId": [
"270057"
]
}
}
]
}
}
}