Python调用Elasticsearch reindex报错:TypeError位置参数不可用的解决方法
解决Elasticsearch Python客户端reindex的TypeError问题
错误原因
你遇到的TypeError是因为新版elasticsearch-py客户端要求所有API方法只能使用关键字参数,禁止使用位置参数。你的代码中把reindex的请求体作为位置参数传递,违反了这个规则。
修复方案
有两种合法的写法可以解决这个问题:
方案1:用body关键字参数传递请求体
将原来的位置参数改为body关键字参数,同时保留wait_for_completion的关键字参数形式:
for source_index in indices: destination_index = "test-neu-2" result = es.reindex( body={ "source": {"index": source_index}, "dest": {"index": destination_index} }, wait_for_completion=True ) print(result)
方案2:直接使用source和dest关键字参数(推荐)
新版客户端支持将请求体中的source和dest直接作为独立的关键字参数传递,写法更直观:
for source_index in indices: destination_index = "test-neu-2" result = es.reindex( source={"index": source_index}, dest={"index": destination_index}, wait_for_completion=True ) print(result)
补充说明
如果你的elasticsearch-py版本是8.x及以上,优先选择方案2,这是官方文档推荐的写法,不仅可读性更强,也更符合客户端的设计规范。
内容的提问来源于stack exchange,提问作者Schlusi
相关产品推荐
相关产品推荐

