| endpoint | update_by_query |
|---|---|
| lang | python |
| es_version | 9.3 |
| client | elasticsearch==9.3.0 |
Use client.update_by_query() to update all documents matching a
query using a script.
response = client.update_by_query(
index="products",
query={"term": {"category": "electronics"}},
script={
"source": "ctx._source.price *= params.discount",
"params": {"discount": 0.9},
},
)
print(f"Updated {response['updated']} documents")By default, version conflicts abort the operation. Set
conflicts="proceed" to skip conflicting documents and continue:
response = client.update_by_query(
index="products",
query={"term": {"in_stock": True}},
script={"source": "ctx._source.in_stock = false"},
conflicts="proceed",
)Use max_docs to cap the number of updates, and scroll_size to
control batch size:
response = client.update_by_query(
index="products",
query={"match_all": {}},
script={"source": "ctx._source.rating += 0.1"},
max_docs=500,
scroll_size=100,
)