简体   繁体   中英

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. 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

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

You have to use 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. 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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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