简体   繁体   English

计算elasticsearch中索引中的文档数

[英]Counting number of documents in an index in elasticsearch

I'm doing a bulk operation to index 100 documents at once using the python ElasticSearch Client.我正在使用 python ElasticSearch 客户端执行批量操作以一次索引 100 个文档。 I want to count the total number of documents in an index.我想计算索引中的文档总数。 So I do the bulk op and then count the number of documents in an index as below:所以我做了批量操作,然后计算索引中的文档数量,如下所示:

helpers.bulk(es_client, actions);
es_client.count('index').get('count')

However the second line still returns the old count, and I tried to run the second line from a different file, which returns the correct result.然而,第二行仍然返回旧计数,我尝试从不同的文件运行第二行,它返回正确的结果。 I suspect that bulk op is not yet completed.我怀疑批量操作尚未完成。 Please correct me if I'm wrong, and what would be the workaround to do what I want?如果我错了,请纠正我,做我想做的事情的解决方法是什么?

get the index document count in python在python中获取索引文档计数

es.indices.refresh(index_name)
es.cat.count(index_name, params={"format": "json"})

You have to use refresh API:您必须使用refresh API:

POST /index/_refresh

The refresh API allows to explicitly refresh one or more index, making all operations performed since the last refresh available for search.刷新 API 允许显式刷新一个或多个索引,使自上次刷新以来执行的所有操作都可用于搜索。 The (near) real-time capabilities depend on the index engine used. (近)实时功能取决于所使用的索引引擎。 For example, the internal one requires refresh to be called, but by default a refresh is scheduled periodically.例如,内部需要调用刷新,但默认情况下会定期安排刷新。

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

I found this in the examples that I attach below我在下面附加的示例中发现了这一点

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

If it helps you, here are good examples of count with Python:如果它对您有帮助,以下是使用 Python 进行计数的好例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM