Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 718
Pavan Bahuguni Avatar asked Apr 06 '18 11:04

Pavan Bahuguni


People also ask

What is Docs count in Kibana?

The doc count is summed up among all shards, including replicas. Since you have one replica per primary shard, this can inflate the doc count. Also note that there might also be a discrepancy in the case that you have nested mappings, which create implicity documents. Kibana not showing all documents in index.

How many documents can Elasticsearch hold?

You could have one document per product or one document per order. There is no limit to how many documents you can store in a particular index. Data in documents is defined with fields comprised of keys and values.

How do I get all documents in Elasticsearch?

You can use cURL in a UNIX terminal or Windows command prompt, the Kibana Console UI, or any one of the various low-level clients available to make an API call to get all of the documents in an Elasticsearch index. All of these methods use a variation of the GET request to search the index.

How do I get total hits in Elasticsearch?

The track_total_hits parameter allows you to control how the total number of hits should be tracked. Given that it is often enough to have a lower bound of the number of hits, such as "there are at least 10000 hits", the default is set to 10,000 .


1 Answers

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

like image 61
pcaceres Avatar answered Sep 18 '22 17:09

pcaceres