背景
项目中ES写入和部分更新使用JSON表示文档,序列化工具FastJSON将实体中以get开头的方法序列化了,但实际上该字段并不存在。导致Mapping和文档污染,需要重建索引。
文档删除字段
mapping 不能删除字段,只能通过重建。
文档删除字段,通过脚本更新,利用ctx._source.remove(‘fieldName’)删除。
POST waybill_exception_report/_doc/_update_by_query
{
"script": "ctx._source.remove('targetOperationLocation')",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "targetOperationLocation"
}
}
]
}
}
}
也可以利用ctx._source.containsKey(‘fieldName’)来判度文档是都包含指定字段,再进行删除。
POST waybill_exception_report/_doc/_update_by_query
{
"script": "if(ctx._sou