Partial updates in Elasticsearch - Time & Space Complexity
When we update only part of a document in Elasticsearch, it is important to understand how the time to do this update changes as the document size grows.
We want to know how the update operation's cost grows when documents get bigger or more complex.
Analyze the time complexity of the following partial update request.
POST /my_index/_update/1
{
"doc": {
"field": "new value"
}
}
This code updates only the "field" inside the document with ID 1, without replacing the whole document.
In this partial update, Elasticsearch must:
- Primary operation: Read the existing document from storage.
- How many times: Once per update request.
- Then merge the updated fields into the document.
- Finally, write the updated document back to storage.
The dominant work is reading and rewriting the whole document, even if only part changes.
As the document size (n) grows, the time to read and rewrite it grows roughly in proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 KB document | Small read and write time |
| 100 KB document | About 10 times more work than 10 KB |
| 1 MB document | About 100 times more work than 10 KB |
Pattern observation: The time grows roughly linearly with document size because the whole document must be processed.
Time Complexity: O(n)
This means the update time grows in direct proportion to the size of the document being updated.
[X] Wrong: "Partial updates only change the small part, so update time is constant no matter document size."
[OK] Correct: Even if only a small part changes, Elasticsearch reads and rewrites the entire document, so time depends on document size.
Understanding how partial updates work helps you reason about performance in real systems where documents vary in size.
This skill shows you can think about how data size affects operation cost, a key part of building efficient search solutions.
What if Elasticsearch supported in-place partial updates without rewriting the whole document? How would the time complexity change?
Practice
Solution
Step 1: Understand partial update concept
Partial updates allow changing only some fields of a document instead of replacing the entire document.Step 2: Identify the main purpose
This saves bandwidth and time by sending only the changed fields, not the whole document.Final Answer:
To update only specific fields of a document without replacing the whole document -> Option CQuick Check:
Partial update = update some fields only [OK]
- Confusing partial update with document deletion
- Thinking partial update creates new documents
- Believing partial update retrieves documents
Solution
Step 1: Identify correct HTTP method and endpoint
Partial updates use POST method on the _update API with the document ID.Step 2: Check request body format
The update body must include a "doc" key with the fields to update.Final Answer:
POST /products/_update/1 { "doc": { "price": 20 } } -> Option BQuick Check:
Use POST + _update + doc key [OK]
- Using GET instead of POST for update
- Omitting the 'doc' key in request body
- Using PUT on _update endpoint incorrectly
- Updating with _doc endpoint instead of _update
POST /users/_update/2
{
"doc": {
"age": 30
}
}If the original document was {"name": "Alice", "age": 25}, what will be the document after the update?
Solution
Step 1: Understand partial update effect
The partial update changes only the "age" field to 30, keeping other fields unchanged.Step 2: Apply update to original document
Original document {"name": "Alice", "age": 25} becomes {"name": "Alice", "age": 30} after update.Final Answer:
{"name": "Alice", "age": 30} -> Option AQuick Check:
Partial update changes only specified fields [OK]
- Thinking update replaces whole document
- Assuming fields not in doc are removed
- Adding extra fields not in update request
POST /orders/_update/5
{
"doc": {
"status": "shipped"
}
}But you get an error. Which is the most likely cause?
Solution
Step 1: Check common update errors
Partial update errors often happen if the document ID does not exist.Step 2: Analyze given request and error cause
The syntax is correct, so the likely cause is missing document with ID '5'.Final Answer:
The document with ID '5' does not exist in 'orders' index -> Option AQuick Check:
Missing document ID causes update error [OK]
- Confusing HTTP method errors with missing document
- Assuming all fields are immutable
- Omitting 'doc' key causing syntax errors
Solution
Step 1: Understand increment operation in partial update
To increase a numeric field, use a script that adds the value to the existing field.Step 2: Identify correct syntax for scripted update
{ "script": { "source": "ctx._source.stock += params.count", "params": { "count": 10 } } } uses the 'script' key with source code to add 10 to 'stock'.Final Answer:
{ "script": { "source": "ctx._source.stock += params.count", "params": { "count": 10 } } } -> Option DQuick Check:
Use script to increment numeric fields [OK]
- Trying to update with doc key using expression string
- Using 'update' key instead of 'doc' or 'script'
- Assigning value directly instead of incrementing
Ready to prove it?
Pass the test and this pattern moves unknown
