【Elasticsearch】更新 Mapping 的列

只能增加 Mapping 的列,而不能修改已存在的列的属性

脚本:

# -*- coding: utf-8 -*-

import json
import argparse
from elasticsearch import Elasticsearch

def parse():
    parser = argparse.ArgumentParser(description="update es mapping")

    # parser.add_argument('--index', help='input es index name', required=True)

    parser.add_argument('--host', help='input es host', required=True)

    parser.add_argument('--file', help='input es mapping json file', required=True)

    return parser.parse_args()


def create_es_client(hosts):

    es = Elasticsearch(hosts)

    return es


def update_mappings(es, indexs):

    for index in indexs:

        print ('update index : %s ' % index["index"])

        update(es, index["index"], index["mapping"])


def read_json(file_name):

    with open(file_name, 'r') as f:
        data = json.load(f)
        return data

def update(es, index_name, mapping):

    try:

        result = es.indices.put_mapping(index = index_name, doc_type = "doc",  ignore=400, body = mapping)

        if ("status" in result) and  400 == result["status"]:
            print ('error : %s ' % result["error"])

        print ('update index : %s finished' % index_name)

    except BaseException:
        print ('Error! update index : %s error' % index_name)



if __name__ == '__main__':

    args = parse()

    index_mapping_array = read_json(args.file)

    es = create_es_client(args.host)

    update_mappings(es, index_mapping_array)


对应的 mapping.json

[
    {
        "index": "account_index",
        "mapping": {
            "properties": {
                "test_id": { "type": "keyword" }
            }
        }
    }
]

运行:

python update_es_mapping.py --file mapping.json --host 192.168.1.111:9300

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值