Skip to content

Latest commit

 

History

History
61 lines (50 loc) · 1.29 KB

File metadata and controls

61 lines (50 loc) · 1.29 KB
endpoint update
lang python
es_version 9.3
client elasticsearch==9.3.0

Elasticsearch 9.3 update endpoint (Python example)

Use client.update() to modify specific fields of an existing document without replacing the entire document.

response = client.update(
    index="products",
    id="prod-1",
    doc={"price": 799.99, "in_stock": False},
)
print(f"{response['result']} document {response['_id']}")

Only the fields in doc are merged into the existing document. All other fields remain unchanged.

Script updates

Use a script for updates that depend on the current document state:

response = client.update(
    index="products",
    id="prod-1",
    script={
        "source": "ctx._source.price *= params.discount",
        "params": {"discount": 0.9},
    },
)

Upserts

Supply upsert to create the document if it does not already exist:

response = client.update(
    index="products",
    id="prod-3",
    doc={"price": 599.00},
    upsert={
        "name": "Ergonomic Standing Desk",
        "brand": "DeskCraft",
        "price": 599.00,
        "category": "furniture",
        "in_stock": True,
        "rating": 4.8,
    },
)

If prod-3 exists, only price is updated. If it does not exist, the full upsert body is indexed instead.