Elasticsearch更新mapping

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)

易享写作 - 专业在线小说创作工具

🚀 易享写作 - 专业在线小说创作工具

欢迎使用 易享写作 (MakeANovel) —— 一款专为小说创作者设计的在线平台。无论您是网文作家、文学爱好者还是故事创作者,这里都能提供您所需的强大工具。

  • 人物关系图生成器:可视化梳理复杂角色关系,创作更轻松。
  • 在线创作与保存:实时保存,多设备同步,灵感永不丢失。
  • 作品系统管理:清晰管理您的所有小说项目和章节。
  • 便捷分享与反馈:方便获取读者意见,助力作品完善。

0 评论
最新
最旧 最多投票
内联反馈
查看所有评论
滚动至顶部