ElasticSearch equivalent to MongoDB .count()
The ElasticSearch equivalent to MongoDB’s count()
is also called count
. It can be used in a similar way
When you have an ElasticSearch query like (example in Python)
result = es.search(index="my_index", body={
"query": {
"match": {
"my_field": "my_value"
}
}
})
result_docs = [hit["_source"] for hit in result["hits"]["hits"]]
you can easily change it to a count-only query by replacing search
with count
:
result = es.count(index="my_index", body={
"query": {
"match": {
"my_field": "my_value"
}
}
})
result_count = result["count"] # e.g. 200