Inverted index data structure in Elasticsearch - Time & Space Complexity
When searching text in Elasticsearch, the inverted index helps find words quickly.
We want to know how the search time changes as the amount of text grows.
Analyze the time complexity of building and searching an inverted index.
{
"mappings": {
"properties": {
"content": { "type": "text" }
}
}
}
GET /my_index/_search
{
"query": {
"match": { "content": "search term" }
}
}
This code defines a text field that Elasticsearch indexes using an inverted index, then searches for documents containing "search term".
Look at what repeats when building and searching the inverted index.
- Primary operation: Scanning each word in all documents to build the index.
- How many times: Once per word per document when indexing; then searching looks up words in the index.
As you add more documents or words, indexing takes longer because it reads all words.
| Input Size (n words) | Approx. Operations |
|---|---|
| 10 | About 10 word scans |
| 100 | About 100 word scans |
| 1000 | About 1000 word scans |
Pattern observation: The work grows directly with the number of words; doubling words doubles work.
Time Complexity: O(n)
This means the time to build the index grows in a straight line with the number of words.
[X] Wrong: "Searching the inverted index takes as long as reading all documents again."
[OK] Correct: The inverted index lets Elasticsearch jump directly to matching words, so search is much faster than scanning all documents.
Understanding how inverted indexes scale helps you explain efficient search systems clearly and confidently.
What if we added a second word to search for? How would the time complexity change?
Practice
inverted index in Elasticsearch?Solution
Step 1: Understand the role of inverted index
An inverted index links each word to the documents containing it, enabling fast search.Step 2: Compare options with definition
Only To map words to the documents where they appear describes this mapping; others describe unrelated functions.Final Answer:
To map words to the documents where they appear -> Option BQuick Check:
Inverted index = word-to-document map [OK]
- Thinking it stores documents in order
- Confusing with data compression
- Assuming it encrypts data
Solution
Step 1: Recall inverted index structure
Inverted index keeps a list of documents for each unique word found in the data.Step 2: Match options to structure
It stores a list of documents for each unique word matches this exactly; others describe different or incorrect storage methods.Final Answer:
It stores a list of documents for each unique word -> Option DQuick Check:
Inverted index = word to document list [OK]
- Confusing inverted index with forward index
- Assuming documents are stored as trees
- Thinking documents are stored as plain text only
Doc1: "apple banana apple"Doc2: "banana orange"What will the inverted index entry for the word
banana contain?Solution
Step 1: Identify documents containing 'banana'
Doc1 contains "banana" once, Doc2 contains "banana" once.Step 2: List documents in inverted index for 'banana'
Both Doc1 and Doc2 should be listed for the word "banana".Final Answer:
[Doc1, Doc2] -> Option AQuick Check:
banana appears in Doc1 and Doc2 [OK]
- Listing only one document
- Confusing word frequency with document list
- Leaving the list empty
orange linked only to Doc1, but Doc2 also contains "orange". What is the likely cause?Solution
Step 1: Verify word presence in documents
Since "orange" is in Doc2, it should appear in the inverted index for that word.Step 2: Identify indexing error
If Doc2 is missing, likely the indexing process did not update the inverted index correctly for Doc2.Final Answer:
The indexing process missed updating the inverted index for Doc2 -> Option AQuick Check:
Missing doc means indexing error [OK]
- Assuming word is not in Doc2
- Thinking Elasticsearch limits docs per word
- Believing inverted index stores only one doc
Solution
Step 1: Understand inverted index purpose
It allows fast lookup of documents by words, speeding up search.Step 2: Evaluate options for search speed
Use the inverted index to quickly find documents containing search words uses the inverted index as intended; others slow search or lose data.Final Answer:
Use the inverted index to quickly find documents containing search words -> Option CQuick Check:
Inverted index speeds search by word lookup [OK]
- Thinking scanning all text is faster
- Disabling inverted index to save space
- Indexing only part of documents
Ready to prove it?
Pass the test and this pattern moves unknown
