使用Python调用Elasticsearch reindex重命名索引时遇400错误
解决Elasticsearch Python客户端reindex时的400错误
错误的核心原因是reindex方法的source和dest参数必须传入字典类型,不能直接传字符串索引名。Elasticsearch的reindex API要求明确通过index键指定目标索引。
修正后的代码如下:
from elasticsearch import Elasticsearch es_host = "https://localhost:9200" password = "PASSWORD_STRING" es_client = Elasticsearch(es_host, basic_auth=('elastic', password), verify_certs=False) # 用字典格式传入source和dest参数,指定对应的索引名 reindex_result = es_client.reindex( source={"index": "transcripts"}, dest={"index": "transcript_sent"} ) print("重索引完成状态:", reindex_result) # 若要实现真正的"重命名"(删除原索引),确认数据复制无误后执行: # es_client.indices.delete(index="transcripts")
补充说明:
source字典中的index字段指定需要复制数据的原索引dest字典中的index字段指定接收数据的目标索引- 原索引
transcripts不会自动删除,若不需要保留,需手动调用删除接口
内容的提问来源于stack exchange,提问作者Varun Pandit
相关产品推荐
相关产品推荐

