为ES索引新增字段

1、导入并生成ES对象

from elasticsearch import Elasticsearch

es = Elasticsearch(['http://127.0.0.1:9200'], http_auth=('用户名', '密码'))

2、为指定索引添加字段

new_fields = {
    "properties": {
        "日期字段": {"type": "date", "format": "yyyy-MM-dd"},
        "普通字段": {"type": "keyword"}
    }
}

es.indices.put_mapping(index='索引', body=new_fields)

3、查询该索引目前所有的字段

mapping = es.indices.get_mapping(index='索引名')

# 确认新增字段是否添加
print(mapping['索引名']['mappings']['properties'].keys())

posted @ 2025-08-04 09:45  wellplayed  阅读(193)  评论(0)    收藏  举报