es更新mapping是一个非常规的操作,下面是操作执行脚本:
from elasticsearch import Elasticsearch
from elasticsearch import helpers
esIp = "127.0.0.1"
esPort = "9200"
fromIndex = "old_index"
targetIndex = "new_index"
new_mapping = {"mappings": {"_doc": {"properties": {"id": {"type": "long"}, "title": {"type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}}}, "content": {"type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}}}}}}}
bulk_read = 1000
scroll = "5m"
# 连接elasticsearch
es = Elasticsearch([esIp], http_auth=("my_name", "my_pwd"), port=esPort)
# 创建新索引
es.indices.create(index=targetIndex, body=new_mapping)
helpers.reindex(client=es, source_index=fromIndex, target_index=targetIndex, target_client=es, query={"query": {"match_all": {}}}, chunk_size=bulk_read)
# 删除旧索引
es.indices.delete(fromIndex)
# 索引设置别名
es.indices.put_alias(index=targetIndex, name=fromIndex)


