首页 > 解决方案 > 有没有办法将 tqdm(进度条)与 ElasticSearch 批量上传一起使用?

问题描述

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

我可以使用:

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

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

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

标签: pythonelasticsearchtqdm

解决方案


是的,但是bulk您需要使用 ,而不是使用streaming_bulk。不像bulk,它最终只返回最终结果,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")

推荐阅读