需要Python重新索引Elasticsearch索引。

huangapple go评论66阅读模式
英文:

Need Python to reindex Elasticsearch index

问题

# 我需要使用Python重新索引Elasticsearch中的索引。
# 但在"es.reindex(...)"部分出现错误
# "TypeError: 无法在Elasticsearch API方法中使用位置参数,只能使用关键字参数。"
# 如何修复这个问题?
from datetime import datetime
from elasticsearch import Elasticsearch

nodes = ['http://localhost:9200/']
es = Elasticsearch(nodes)
print(es.info().body)

indexPrefix = "test-index"

res = es.indices.get(
    index=f"{indexPrefix}*"
)
indices = []
for index in res:
    indices.append(index)

no_of_indices = len(indices)
print(indices, no_of_indices)

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)
英文:

I need python to reindex indices in Elasticsarch.
But on the "es.reindex(...)" I get an error
"TypeError: Positional arguments can't be used with Elasticsearch API methods. Instead only use keyword arguments."

How to fix this?

from datetime import datetime
from elasticsearch import Elasticsearch

nodes = ['http://localhost:9200/']
es = Elasticsearch(nodes)
print(es.info().body)

indexPrefix = "test-index"

res = es.indices.get(
    index=f"{indexPrefix}*"
)
indices = []
for index in res:
    indices.append(index)

no_of_indices = len(indices)
print(indices, no_of_indices)

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)

Tried to use internetsearch but could not find a solution how to fix the code

答案1

得分: 0

根据您的错误消息报告,不允许使用位置参数。 您缺少关键字参数 body:

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)
英文:

as reported by your error message, positional arguments

es.reindex({ "source": {"index": source_index}, "dest": {"index": destination_index}}, wait_for_completion=True)

are not allowed. You're missing the keyword argument body:

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)

huangapple
  • 本文由 发表于 2023年3月12日 19:36:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定