First search query in Elasticsearch - Time & Space Complexity
When we run a search query in Elasticsearch, we want to know how the time it takes changes as we add more data.
We ask: How does the search time grow when the number of documents increases?
Analyze the time complexity of the following code snippet.
GET /my-index/_search
{
"query": {
"match": {
"title": "elasticsearch"
}
}
}
This query searches for documents in "my-index" where the "title" field contains the word "elasticsearch".
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Elasticsearch scans the inverted index entries for the word "elasticsearch".
- How many times: It processes postings for each document containing that word, which depends on how many documents match.
As the number of documents grows, Elasticsearch looks up the word in its index and checks matching documents.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Checks a few postings for "elasticsearch" |
| 100 | Checks more postings, roughly 10 times more |
| 1000 | Checks many postings, about 100 times more than 10 docs |
Pattern observation: The work grows roughly with the number of documents that contain the search word.
Time Complexity: O(k)
This means the search time grows in proportion to the number of documents matching the query term.
[X] Wrong: "The search time depends on the total number of documents in the index."
[OK] Correct: Elasticsearch uses an index to jump directly to matching documents, so it does not scan all documents, only those containing the search term.
Understanding how search time grows helps you explain how Elasticsearch handles queries efficiently, a useful skill for real-world search applications.
"What if we changed the query to search for multiple words combined with AND? How would the time complexity change?"
Practice
What does a match query do in an Elasticsearch search?
Solution
Step 1: Understand the purpose of
Thematchquerymatchquery is used to find documents where a field contains the given text.Step 2: Differentiate from other operations
Deleting, updating, or creating indexes are different Elasticsearch operations, not done bymatch.Final Answer:
It finds documents where the specified field contains the search text. -> Option AQuick Check:
matchquery = find documents [OK]
- Confusing search with delete or update operations
- Thinking match creates or modifies indexes
- Assuming match returns all documents regardless of content
Which of the following is the correct syntax for a basic Elasticsearch match query to search for "apple" in the field "fruit"?
{
"query": {
"match": {
"fruit": "apple"
}
}
}Solution
Step 1: Identify the correct JSON structure for a match query
The correct structure placesmatchinsidequery, with the field name as key and search text as value.Step 2: Compare options to this structure
{ "query": { "match": { "fruit": "apple" } } } matches the correct syntax exactly. Others misplace keys or swap field and text.Final Answer:
{ "query": { "match": { "fruit": "apple" } } } -> Option AQuick Check:
Correct JSON structure = { "query": { "match": { "fruit": "apple" } } } [OK]
- Swapping field name and search text
- Placing match outside query
- Using wrong nesting of JSON keys
Given this Elasticsearch query, what will it search for?
{
"query": {
"match": {
"title": "quick brown fox"
}
}
}Solution
Step 1: Understand how
Thematchquery works with multiple wordsmatchquery analyzes the text and searches for documents containing any of the words, not necessarily the exact phrase.Step 2: Compare options with this behavior
Documents where the title field contains any of the words "quick", "brown", or "fox". correctly states it searches for any of the words. Documents where the title field contains the exact phrase "quick brown fox". is phrase match, which requires a different query type.Final Answer:
Documents where the title field contains any of the words "quick", "brown", or "fox". -> Option DQuick Check:
match= any word match [OK]
- Thinking match does exact phrase search
- Assuming match finds only one word
- Confusing match with match_phrase query
Identify the error in this Elasticsearch query:
{
"query": {
"match": {
"content": apple
}
}
}Solution
Step 1: Check JSON syntax for values
In JSON, string values must be enclosed in double quotes. Here, apple is unquoted.Step 2: Verify other parts of the query
The field name and braces are correctly quoted and closed. The match query is correctly inside query.Final Answer:
The search text apple should be in quotes. -> Option CQuick Check:
Strings need quotes in JSON [OK]
- Leaving string values unquoted
- Misplacing match outside query
- Forgetting closing braces
You want to find documents where the field "description" contains the word "fresh" but only if the field "category" equals "fruit". Which Elasticsearch query correctly combines these conditions?
Solution
Step 1: Understand combining multiple conditions in Elasticsearch
To require both conditions, use aboolquery withmustarray containing eachmatchquery.Step 2: Evaluate each option's structure
{ "query": { "bool": { "must": [ { "match": { "description": "fresh" } }, { "match": { "category": "fruit" } } ] } } } correctly usesboolwithmustfor both matches. { "query": { "match": { "description": "fresh fruit" } } } searches combined text in one field. Options A and C have invalid JSON or incorrect query structure.Final Answer:
{ "query": { "bool": { "must": [ { "match": { "description": "fresh" } }, { "match": { "category": "fruit" } } ] } } } -> Option BQuick Check:
Use bool must to combine queries [OK]
- Combining terms incorrectly in one match
- Using invalid JSON with repeated keys
- Not using bool query for multiple conditions
Ready to prove it?
Pass the test and this pattern moves unknown
