Updating documents in Elasticsearch - Time & Space Complexity
When updating documents in Elasticsearch, it is important to understand how the time taken grows as the number of documents increases.
We want to know how the update operation scales when more documents are involved.
Analyze the time complexity of the following code snippet.
POST /my_index/_update_by_query
{
"script": {
"source": "ctx._source.counter += params.count",
"params": {"count": 1}
},
"query": {
"term": {"status": "active"}
}
}
This code updates all documents with status "active" by increasing their counter field by 1.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The update script runs once for each matching document.
- How many times: Equal to the number of documents matching the query.
As the number of matching documents grows, the total updates grow proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 update script executions |
| 100 | 100 update script executions |
| 1000 | 1000 update script executions |
Pattern observation: The work grows directly with the number of documents to update.
Time Complexity: O(n)
This means the time to update grows linearly with the number of documents matched.
[X] Wrong: "Updating documents is always a quick single operation regardless of how many documents match."
[OK] Correct: Each matching document must be updated individually, so more documents mean more work and longer time.
Understanding how update operations scale helps you explain performance considerations clearly and shows you grasp how Elasticsearch handles data changes.
"What if we changed the update to only affect a fixed number of documents regardless of total matches? How would the time complexity change?"