在Elasticsearch中计算索引中的文档数

Pav*_*uni 5 python python-2.7 python-3.x elasticsearch

我正在使用python ElasticSearch Client一次批量索引100个文档的索引。我想计算索引中文档的总数。因此,我执行批量操作,然后计算索引中的文档数量,如下所示:

helpers.bulk(es_client, actions);
es_client.count('index').get('count')
Run Code Online (Sandbox Code Playgroud)

但是第二行仍然返回旧计数,因此我尝试从其他文件运行第二行,该文件返回正确的结果。我怀疑批量操作尚未完成。如果我错了,请纠正我,怎么做才能实现我想要的?

小智 11

在python中获取索引文档计数

es.indices.refresh(index_name)
es.cat.count(index_name, params={"format": "json"})
Run Code Online (Sandbox Code Playgroud)


pca*_*res 8

我在下面附加的示例中发现了这一点

es=Elasticsearch([{'host':'url','port':'9200','timeout':60}]) 
res = es.count(index='your index', doc_type='your doc_type', body={'query': your query })["count"]
Run Code Online (Sandbox Code Playgroud)

如果对你有帮助,这里有一些使用 Python 进行计数的好例子:

https://python.hotexamples.com/examples/elasticsearch/Elasticsearch/count/python-elasticsearch-count-method-examples.html


LON*_*MAN 5

您必须使用refreshAPI:

POST /index/_refresh

刷新 API 允许显式刷新一个或多个索引,使自上次刷新以来执行的所有操作都可用于搜索。(近)实时功能取决于所使用的索引引擎。例如,内部需要调用刷新,但默认情况下会定期安排刷新。

有关更多信息,查看https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html