有没有办法在 ElasticSearch 批量上传中使用 tqdm(进度条)?

Is there a way to use tqdm (progress bar) with ElasticSearch bulk upload?

如标题所述,我正在寻找一种直观的方式来检查我的 ES 客户端上传

我可以使用:

for i in tqdm(<my_docs>):
    es_client.create(...)

但我想使用推荐的(ES)方式:

helpers.bulk(...) <- how to add tqdm here?

是的,但是您需要使用 streaming_bulk 而不是 bulk。不像bulk,最后只有returns最终结果,streaming_bulk每个动作产生结果。有了这个,我们可以在每次操作后更新 tqdm

代码大致如下所示:

# Setup the client
client = Elasticsearch()

# Set total number of documents
number_of_docs = 100

progress = tqdm.tqdm(unit="docs", total=number_of_docs)
successes = 0

for ok, action in streaming_bulk(
    client=client, index="my-index", actions=<your_generator_here>
):
    progress.update(1)
    successes += ok

print(f"Indexed {successes}/{number_of_docs} documents")