Match query in Elasticsearch - Time & Space Complexity
When using a match query in Elasticsearch, it's important to know how the search time changes as the data grows.
We want to understand how the number of documents affects the time it takes to find matches.
Analyze the time complexity of the following Elasticsearch match query.
GET /my_index/_search
{
"query": {
"match": {
"content": "quick brown fox"
}
}
}
This query searches the "content" field for documents matching the words "quick brown fox".
Look for repeated steps that affect performance.
- Primary operation: Checking the inverted index for matching terms in the "content" field.
- How many times: Once per unique term in the query, then merging results.
As the number of documents grows, the search time depends on the number of matching documents and the efficiency of the inverted index.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Depends on matching documents, not all 10 checked |
| 100 | Depends on matching documents, not all 100 checked |
| 1000 | Depends on matching documents, not all 1000 checked |
Pattern observation: The number of operations grows with the number of matching documents, not necessarily all documents.
Time Complexity: O(m), where m is the number of matching documents.
This means the search time grows linearly with the number of matching documents, not the total number of documents.
[X] Wrong: "The match query checks every document, so it runs in linear time with total documents."
[OK] Correct: Elasticsearch uses an inverted index to quickly find matching documents, so it does not scan all documents.
Understanding how search time grows with data size helps you explain performance in real projects and shows you can think about scaling.
"What if we added an index on the 'content' field? How would the time complexity change?"
Practice
match query in Elasticsearch primarily do?Solution
Step 1: Understand the purpose of match query
The match query is designed to find documents that contain text similar to the search words provided.Step 2: Differentiate from other operations
Deleting, updating, or creating indexes are not functions of the match query; it only searches text.Final Answer:
Finds documents with text similar to the search words -> Option BQuick Check:
Match query = text search [OK]
- Confusing match query with delete or update operations
- Thinking match query creates indexes
- Assuming match query returns exact matches only
match query in Elasticsearch?Solution
Step 1: Review correct JSON structure for match query
The match query requires a JSON object with the field name as key and the search text as value inside another object.Step 2: Identify syntax errors in options
{ "match": { "field": "search text" } } uses correct JSON with colon separators and nested object. Options A, B, and C have syntax errors like missing braces, wrong assignment, or array usage.Final Answer:
{ "match": { "field": "search text" } } -> Option AQuick Check:
Correct JSON syntax = { "match": { "field": "search text" } } [OK]
- Using equal sign instead of colon
- Missing curly braces around field and text
- Using array instead of object for field and text
{ "query": { "match": { "title": { "query": "quick brown fox", "operator": "and" } } } }What does the
operator": "and" do in this match query?Solution
Step 1: Understand the operator option in match query
The operator 'and' means all words in the query must be present in the document for a match.Step 2: Compare with other operators
Operator 'or' would return documents with any word, but 'and' requires all words.Final Answer:
Returns documents containing all the words 'quick', 'brown', and 'fox' -> Option AQuick Check:
Operator 'and' = all words required [OK]
- Confusing 'and' with 'or' operator
- Thinking 'and' means exact phrase match
- Ignoring operator option defaults
{ "query": { "match": { "content": { "query": "data science", "operator": "AND" } } } }Solution
Step 1: Check operator value case sensitivity
Elasticsearch expects the operator value to be lowercase 'and' or 'or'. Uppercase 'AND' is invalid.Step 2: Validate other parts of the query
The field name 'content' is valid, query string can be a string, and match query supports operator option.Final Answer:
The operator value should be lowercase 'and', not uppercase 'AND' -> Option CQuick Check:
Operator values are lowercase [OK]
- Using uppercase 'AND' or 'OR' for operator
- Assuming query must be an array
- Thinking operator option is not allowed
description field contains either the word 'fast' or 'quick', but not necessarily both. Which match query is correct?Solution
Step 1: Understand the operator 'or' in match query
Operator 'or' returns documents containing any of the words, so either 'fast' or 'quick' or both.Step 2: Evaluate other options
B requires both words, C searches for documents containing 'fast', 'OR', and 'quick' (treating OR as literal text), D uses invalid operator 'not'.Final Answer:
{ "match": { "description": { "query": "fast quick", "operator": "or" } } } -> Option DQuick Check:
Use operator 'or' for any word match [OK]
- Using 'and' when 'or' is needed
- Using 'OR' in the search text instead of operator
- Using invalid operator like 'not'
Ready to prove it?
Pass the test and this pattern moves unknown
