Sometimes there is a need to check whether a document exists or not in Elasticsearch. Following the ES documentation and the best practices, you can use the HEAD
method instead of the GET
method.
Prerequisites
- Elasticsearch cluster
Check whether a document exists in Elasticsearch
To check whether a document exists, run:
curl -i -XHEAD 'ES_Endpoint/devcoops/blog/152'
It will return a 200 OK
status code if the document exists:
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 0
Or 404
Not Found if it doesn’t:
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=UTF-8
Content-Length: 0
Conclusion
The document may don’t exist when you checked it, but maybe someone will kick off reindex or index that particular document. So, from this tutorial you can learn whether some document exists or not in Elasticsearch. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.