| endpoint | exists |
|---|---|
| lang | python |
| es_version | 9.3 |
| client | elasticsearch==9.3.0 |
Use client.exists() to check whether a document exists without
fetching its contents. Returns a boolean.
if client.exists(index="products", id="prod-1"):
print("Document exists")
else:
print("Document not found")This issues a HEAD request, so no document body is transferred.
Use exists to guard operations that depend on prior state:
if not client.exists(index="products", id="prod-1"):
client.index(
index="products",
id="prod-1",
document={"name": "Espresso Machine Pro", "price": 899.99},
)For atomic create-if-not-exists, prefer client.create() instead,
which performs the check and write in a single request.