Skip to content

Latest commit

 

History

History
52 lines (43 loc) · 1.15 KB

File metadata and controls

52 lines (43 loc) · 1.15 KB
endpoint update_by_query
lang python
es_version 9.3
client elasticsearch==9.3.0

Elasticsearch 9.3 update_by_query endpoint (Python example)

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")

Handling conflicts

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",
)

Limiting scope

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,
)