只能增加 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