Skip to content

Latest commit

 

History

History
36 lines (28 loc) · 860 Bytes

File metadata and controls

36 lines (28 loc) · 860 Bytes
endpoint exists
lang python
es_version 9.3
client elasticsearch==9.3.0

Elasticsearch 9.3 exists endpoint (Python example)

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.

Conditional logic

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.