ES 新增字段和删除字段

176

前言

在ES项目实战中,由于前期考虑不充分,难免有字段设计出现缺漏的情况。对于数据量小的情况下,可以做代码迁移。如果数据量过大,那么对于索引的修改就显得很有必要了。

ES 指定索引增加字段

首先创建一个索引

PUT /indexname  # indexname = zq_test

{
    "mappings": {
        "properties": {
            "name": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "age": {
                "type": "long",
                "index": true
            },
            "gender": {
                "type": "keyword"
            }
        }
    }
}

查看创建好的索引

GET /zq_test

{
    "zq_test": {
        "aliases": {},
        "mappings": {
            "properties": {
                "addr": {
                    "type": "integer"
                },
                "age": {
                    "type": "long"
                },
                "event_engine_count": {
                    "type": "integer"
                },
                "gender": {
                    "type": "keyword"
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1623333220789",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "zWmXJeCSQsaI83JrJ3wJLA",
                "version": {
                    "created": "7060099"
                },
                "provided_name": "zq_test"
            }
        }
    }
}

增加新的字段

PUT /zq_test/_mapping

{
    "properties": {
        "hight": {
            "type": "integer"
        }
    }
}

再次查看索引

GET /zq_test

{
    "zq_test": {
        "aliases": {},
        "mappings": {
            "properties": {
                "addr": {
                    "type": "integer"
                },
                "age": {
                    "type": "long"
                },
                "event_engine_count": {
                    "type": "integer"
                },
                "gender": {
                    "type": "keyword"
                },
                "hight": {
                    "type": "integer"
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1623333220789",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "zWmXJeCSQsaI83JrJ3wJLA",
                "version": {
                    "created": "7060099"
                },
                "provided_name": "zq_test"
            }
        }
    }
}

嵌套节点下面新增字段

PUT /zq_test/_mapping

{
    "properties": {
        "school": {
            "type": "keyword",
            "norms": false,
            "doc_values": false
        },
        "teacher": {
            "type": "nested",
            "properties": {
                "rightStatus": {
                    "type": "keyword",
                    "norms": false,
                    "doc_values": false
                },
                "rightCurrTime": {
                    "type": "keyword",
                    "norms": false,
                    "doc_values": false
                }
            }
        }
    }
}

对应新的索引信息

{
    "zq_test": {
        "aliases": {},
        "mappings": {
            "properties": {
                "addr": {
                    "type": "integer"
                },
                "age": {
                    "type": "long"
                },
                "event_engine_count": {
                    "type": "integer"
                },
                "gender": {
                    "type": "keyword"
                },
                "hight": {
                    "type": "integer"
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "school": {
                    "type": "keyword",
                    "doc_values": false
                },
                "teacher": {
                    "type": "nested",
                    "properties": {
                        "rightCurrTime": {
                            "type": "keyword",
                            "doc_values": false
                        },
                        "rightStatus": {
                            "type": "keyword",
                            "doc_values": false
                        }
                    }
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1623333220789",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "zWmXJeCSQsaI83JrJ3wJLA",
                "version": {
                    "created": "7060099"
                },
                "provided_name": "zq_test"
            }
        }
    }
}

ES 删除字段

ES 已经建立好的索引数据,无法直接删除一个字段。除非新建索引。但是我们通过文档删除字段。通过脚本更新的方式,删除设计文档中的字段内容,达到该字段数据为 None 的形式

删除指定文档中所有的某一个字段数据

body = {
        "script": f"ctx._source.remove('{field}')",
        "query": {
            "bool": {
                "must": [
                    {
                        "exists": {
                            "field": field
                        }
                    }
                ]
            }
        }
    }
es.update_by_query(index, body)

后记

文章参考,原文地址 www.cnblogs.com