Bulk API for batch operations in Elasticsearch - Time & Space Complexity
When using Elasticsearch's Bulk API, we send many operations at once. It's important to know how the time to process these operations grows as we add more.
We want to understand how the number of operations affects the total work Elasticsearch does.
Analyze the time complexity of the following Bulk API request.
POST /_bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "index" : { "_index" : "test", "_id" : "2" } }
{ "field1" : "value2" }
{ "delete" : { "_index" : "test", "_id" : "3" } }
This code sends multiple index and delete operations in one batch to Elasticsearch.
Look for repeated work inside the bulk request.
- Primary operation: Processing each individual action (index or delete) in the batch.
- How many times: Once for each operation in the bulk request.
As you add more operations to the bulk request, Elasticsearch processes each one separately.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 operations processed |
| 100 | About 100 operations processed |
| 1000 | About 1000 operations processed |
Pattern observation: The work grows directly with the number of operations you send.
Time Complexity: O(n)
This means the time to complete the bulk request grows in a straight line with the number of operations.
[X] Wrong: "Sending more operations in bulk will take the same time as sending just one."
[OK] Correct: Each operation still needs to be processed, so more operations mean more total work and more time.
Understanding how batch sizes affect processing time helps you design efficient data loading and updating strategies in Elasticsearch.
"What if we split one large bulk request into many smaller ones? How would that affect the overall time complexity?"
Practice
Solution
Step 1: Understand Bulk API functionality
The Bulk API is designed to handle many document operations like add, update, or delete in one request to improve efficiency.Step 2: Compare options with Bulk API purpose
Creating indexes, searching, or deleting clusters are not the Bulk API's main tasks.Final Answer:
To perform multiple document operations in a single request -> Option AQuick Check:
Bulk API = batch operations [OK]
- Confusing Bulk API with search API
- Thinking Bulk API creates indexes
- Assuming Bulk API deletes clusters
Solution
Step 1: Recall Bulk API action format
Each action in Bulk API consists of two JSON lines: first metadata (like index and id), then the document data.Step 2: Check options for correct order and format
Combining metadata and data in one JSON object is invalid. The reversed order with data line before metadata line is wrong. Using a JSON array is invalid. {"index": {"_index": "test", "_id": "1"}} {"field": "value"} correctly shows metadata line followed by data line.Final Answer:
{"index": {"_index": "test", "_id": "1"}} {"field": "value"} -> Option BQuick Check:
Metadata line then data line = correct Bulk format [OK]
- Swapping metadata and data lines
- Using JSON arrays instead of lines
- Combining metadata and data in one JSON object
{"index": {"_index": "products", "_id": "10"}}
{"name": "Laptop", "price": 1200}
{"delete": {"_index": "products", "_id": "5"}}
{"index": {"_index": "products", "_id": "11"}}
{"name": "Mouse", "price": 25}Solution
Step 1: Analyze each Bulk API action
First action: index document with id 10 (add Laptop). Second action: delete document with id 5 (no data line needed). Third action: index document with id 11 (add Mouse).Step 2: Confirm correct syntax and operations
Delete action does not require a data line, so no syntax error. Two documents are added, one deleted.Final Answer:
Adds two documents and deletes one document -> Option AQuick Check:
Index + delete + index = add 2, delete 1 [OK]
- Expecting data line after delete action
- Counting delete as add
- Confusing action order
{"update": {"_index": "users", "_id": "3"}}
{"name": "Alice"}Solution
Step 1: Understand update action format
Update action requires the data line to contain a "doc" field wrapping the fields to update.Step 2: Check the given data line
The data line only has {"name": "Alice"} without "doc", so it is invalid syntax for update.Final Answer:
Missing "doc" field inside the data line for update -> Option CQuick Check:
Update data needs "doc" wrapper [OK]
- Omitting "doc" in update data
- Thinking update needs empty data line
- Confusing update with index action
Solution
Step 1: Understand conditional updates in Bulk API
Bulk API update actions can include scripts that check document fields before applying changes.Step 2: Evaluate options for conditional update
Use Bulk API with update actions including a script that checks "status" before updating uses update with scripts to check "status" = "pending" before updating, which is efficient. Use Bulk API with index actions to overwrite all documents regardless of status overwrites all documents ignoring condition. Use Bulk API with delete actions for all documents and re-index updated ones deletes unnecessarily. Use Bulk API with search queries to filter documents before updating is invalid because Bulk API does not support search queries.Final Answer:
Use Bulk API with update actions including a script that checks "status" before updating -> Option DQuick Check:
Conditional update = update + script in Bulk API [OK]
- Using index to overwrite without condition
- Deleting documents unnecessarily
- Trying to filter with search inside Bulk API
Ready to prove it?
Pass the test and this pattern moves unknown
