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"
            }
        }
    }
}

对应 python 方法

from threading import BoundedSemaphore
from elasticsearch import Elasticsearch


# elasticsearch集群服务器的地址
# 创建elasticsearch客户端
es = Elasticsearch(
    "127.0.0.1:9200",
    # # 启动前嗅探es集群服务器
    # sniff_on_start=True,
    # # es集群服务器结点连接异常时是否刷新es节点信息
    # sniff_on_connection_fail=True,
    # # 每60秒刷新节点信息
    # sniffer_timeout=60
)

def add_field(index, body):
    """将现有 index 增加一个字段

    Args:
        index (str): 索引名称
        body (dict): 查询体同上 PUT /zq_test/_mapping
    """
    return es.indices.put_mapping(index=index, body=body)

ES 删除字段

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

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

body = {
        "script": f"ctx._source.remove('{field}')",
        "query": {
            "bool": {
                "must": [
                    {
                        "exists": {
                            "field": field
                        }
                    }
                ]
            }
        }
    }
es.update_by_query(index, body)
posted @ 2022-02-21 11:25  小片清风  阅读(26195)  评论(0编辑  收藏  举报