Why documents are the unit of data in Elasticsearch - Performance Analysis
When working with Elasticsearch, documents are the main pieces of data we store and search.
We want to understand how the time it takes to handle data grows as we add more documents.
Analyze the time complexity of indexing multiple documents.
POST /my_index/_bulk
{ "index": { "_id": "1" } }
{ "name": "Alice", "age": 30 }
{ "index": { "_id": "2" } }
{ "name": "Bob", "age": 25 }
{ "index": { "_id": "3" } }
{ "name": "Carol", "age": 27 }
This code adds several documents to an index in bulk, each document representing one unit of data.
Look at what repeats when adding documents.
- Primary operation: Indexing each document one by one.
- How many times: Once per document added.
As you add more documents, the work grows with the number of documents.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 indexing operations |
| 100 | 100 indexing operations |
| 1000 | 1000 indexing operations |
Pattern observation: The time grows directly with the number of documents added.
Time Complexity: O(n)
This means the time to index data grows in a straight line as you add more documents.
[X] Wrong: "Adding more documents takes the same time no matter how many there are."
[OK] Correct: Each document needs its own processing, so more documents mean more work and more time.
Understanding how data size affects processing time helps you explain how Elasticsearch handles scaling in real projects.
"What if we indexed documents in parallel instead of one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of documents in Elasticsearch
Documents store all related data fields together, making them self-contained units.Step 2: Compare documents to other data units
Unlike tables or metadata, documents hold actual data and can be searched independently.Final Answer:
Because each document holds all related information together in one place -> Option DQuick Check:
Documents = self-contained data units [OK]
- Thinking documents are just metadata
- Confusing documents with tables
- Assuming documents can't be searched alone
Solution
Step 1: Identify how documents are stored
Documents are stored inside indexes and each has a unique ID for identification.Step 2: Eliminate incorrect options
File paths, timestamps, or size are not used as unique identifiers in Elasticsearch.Final Answer:
Using a unique document ID within an index -> Option CQuick Check:
Unique ID = document identifier [OK]
- Confusing file path with document ID
- Using timestamps as unique keys
- Assuming size identifies documents
Solution
Step 1: Understand document structure for books
Each document holds all details about a book, like title, author, and summary.Step 2: Analyze search efficiency
Having all data in one document allows Elasticsearch to quickly find relevant books without joining data.Final Answer:
Because each document contains all book details, searches can target whole books quickly -> Option BQuick Check:
Complete data per document = fast search [OK]
- Thinking documents are separate files
- Believing documents store only keywords
- Assuming manual indexing is needed every time
Solution
Step 1: Understand the role of unique IDs
Unique IDs allow Elasticsearch to find, update, or delete specific documents.Step 2: Identify the impact of missing ID
Without an ID, Elasticsearch cannot uniquely identify the document, causing update or delete failures.Final Answer:
The document cannot be uniquely identified or updated -> Option AQuick Check:
Missing ID = no unique identification [OK]
- Assuming document duplicates automatically
- Thinking document is ignored in search
- Believing document splits into parts
Solution
Step 1: Consider data grouping in Elasticsearch
Each document should hold all related data for easy access and management.Step 2: Evaluate impact on retrieval and updates
One document per profile means fewer queries and simpler updates, improving performance.Step 3: Reject incorrect assumptions
Splitting data into multiple documents complicates queries and slows down updates.Final Answer:
Because storing all profile data in one document makes retrieval and updates simpler and faster -> Option AQuick Check:
One document per profile = simpler management [OK]
- Thinking multiple documents per profile speed search
- Believing Elasticsearch limits one document per user
- Assuming documents must be very small always
Ready to prove it?
Pass the test and this pattern moves unknown
