Retrieving a document by ID in Elasticsearch - Time & Space Complexity
When we get a document by its ID in Elasticsearch, we want to know how fast this action is as the data grows.
We ask: How does the time to find one document change when the database gets bigger?
Analyze the time complexity of the following code snippet.
GET /my_index/_doc/12345
{
"_source": true
}
This code fetches a single document by its unique ID from the index.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Direct lookup of the document by its ID using an internal hash or index.
- How many times: This operation happens once per request, no loops or repeated searches.
Getting a document by ID is like looking up a word in a dictionary by its exact spelling.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1 operation |
| 100 | About 1 operation |
| 1000 | About 1 operation |
Pattern observation: The time stays almost the same no matter how many documents there are.
Time Complexity: O(1)
This means finding a document by ID takes about the same time no matter how big the database is.
[X] Wrong: "Retrieving by ID gets slower as the database grows because it searches through all documents."
[OK] Correct: Elasticsearch uses a special index to jump directly to the document, so it does not scan all documents.
Understanding this helps you explain how databases quickly find exact matches, a key skill in many data-related jobs.
"What if we searched for documents by a field value instead of ID? How would the time complexity change?"