| endpoint | update |
|---|---|
| lang | python |
| es_version | 9.3 |
| client | elasticsearch==9.3.0 |
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.
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},
},
)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.