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?"
Practice
Solution
Step 1: Understand retrieval methods
Retrieving by ID directly targets one document, making it the fastest method.Step 2: Compare with other methods
Search queries and scroll APIs scan multiple documents, which is slower than direct ID retrieval.Final Answer:
Retrieve the document by its ID using the GET API -> Option AQuick Check:
Fastest retrieval = GET by ID [OK]
- Thinking search queries are faster than direct ID retrieval
- Using scroll API for single document retrieval
- Confusing aggregation with document retrieval
Solution
Step 1: Recall the GET by ID syntax
The correct syntax uses GET with the index and document ID: GET /index/_doc/123Step 2: Identify incorrect options
GET /index/_search {"query": {"term": {"_id": "123"}}} uses a search query, not direct GET by ID. POST /index/_doc/123/_get uses POST and an invalid path. GET /index/_search/123 has an invalid URL structure.Final Answer:
GET /index/_doc/123 -> Option AQuick Check:
Correct syntax = GET /index/_doc/ID [OK]
- Using search API instead of GET by ID
- Wrong HTTP method like POST
- Incorrect URL path structure
GET /products/_doc/abc123
Solution
Step 1: Understand the GET by ID response
If the document exists, Elasticsearch returns found: true with the document source.Step 2: Match the response format
{"_index":"products","_id":"abc123","found":true,"_source":{"name":"Laptop","price":1200}} shows found: true and the document data, which matches expected output.Final Answer:
{"_index":"products","_id":"abc123","found":true,"_source":{"name":"Laptop","price":1200}} -> Option CQuick Check:
Document exists = found: true with _source [OK]
- Expecting HTTP 404 instead of found: false
- Confusing error message with standard response
- Ignoring the _source field in response
GET /store/_docWhat is the problem and how to fix it?
Solution
Step 1: Identify the request format
The GET by ID request requires the document ID after _doc in the URL.Step 2: Fix the URL
Add the document ID after _doc, e.g., GET /store/_doc/123Final Answer:
Missing document ID in URL; add the ID after _doc -> Option DQuick Check:
GET by ID needs document ID in URL [OK]
- Omitting document ID in GET request URL
- Using wrong HTTP method
- Misnaming the index with underscore
Solution
Step 1: Understand GET by ID response behavior
GET by ID returns a JSON with 'found': false if the document is missing, no error thrown.Step 2: Compare with other methods
Search queries return hits but are slower; catching HTTP 404 is unnecessary; scroll API is for multiple docs.Final Answer:
Use GET /index/_doc/id and check if 'found' is true in the response -> Option BQuick Check:
Check 'found' field to detect missing document [OK]
- Expecting HTTP 404 error for missing documents
- Using search queries instead of GET by ID
- Using scroll API for single document retrieval
Ready to prove it?
Pass the test and this pattern moves unknown
