Term query in Elasticsearch - Time & Space Complexity
When using a term query in Elasticsearch, it's important to know how the search time changes as data grows.
We want to understand how the query's work increases when there are more documents or terms.
Analyze the time complexity of the following term query.
GET /my_index/_search
{
"query": {
"term": {
"status": {
"value": "active"
}
}
}
}
This query searches for documents where the field "status" exactly matches "active".
Look at what repeats when the query runs.
- Primary operation: Checking the inverted index for the term "active" in the "status" field.
- How many times: Once per query, but the number of documents matching can vary.
As the number of documents grows, the time to find the term's postings grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Checking a small list of documents for "active" |
| 100 | Checking a larger list, more documents to scan |
| 1000 | Checking an even bigger list, more matches to process |
Pattern observation: The work grows roughly in proportion to how many documents contain the term.
Time Complexity: O(k)
This means the time depends on the number of documents that have the term, not the total documents in the index.
[X] Wrong: "The term query checks every document in the index every time."
[OK] Correct: Elasticsearch uses an index that directly points to matching documents, so it only checks those with the term.
Understanding how term queries scale helps you explain search efficiency clearly and confidently in real projects.
What if we changed the term query to a match query? How would the time complexity change?
Practice
term query in Elasticsearch do?Solution
Step 1: Understand the purpose of term query
A term query searches for exact matches without analyzing the search term.Step 2: Compare options with term query behavior
Only Finds exact matches of the given term in the data describes exact matching, which is the core of term query.Final Answer:
Finds exact matches of the given term in the data -> Option BQuick Check:
Term query = exact match [OK]
- Confusing term query with match query that analyzes text
- Thinking term query sorts results
- Assuming term query finds similar or partial matches
Solution
Step 1: Identify correct term query syntax
The term query uses the key "term" with a field and exact value.Step 2: Check each option
{ "term": { "user": "kimchy" } } matches the correct syntax. { "match": { "user": "kimchy" } } is a match query, C is invalid, D is a terms query (plural) for multiple values.Final Answer:
{ "term": { "user": "kimchy" } } -> Option DQuick Check:
Term query syntax = { "term": { "user": "kimchy" } } [OK]
- Using "match" instead of "term" for exact match
- Confusing "term" with "terms" for single value
- Adding extra keys like "term_query" which is invalid
{ "query": { "term": { "status": "active" } } }What documents will this query return?
Solution
Step 1: Understand term query behavior on field
The term query matches documents where the field value exactly equals the given term without analysis.Step 2: Analyze each option
Only Documents where the status field exactly equals "active" correctly describes exact equality. Others describe partial or analyzed matches which term query does not do.Final Answer:
Documents where the status field exactly equals "active" -> Option AQuick Check:
Term query = exact field value match [OK]
- Thinking term query matches partial or analyzed text
- Assuming term query matches prefixes or substrings
- Confusing term query with match query behavior
{ "query": { "term": { "user": "Kimchy" } } }Your data has the user field stored as lowercase "kimchy". What is the problem?
Solution
Step 1: Understand term query case sensitivity
Term query matches exact terms and is case sensitive, so "Kimchy" does not match "kimchy".Step 2: Check other options
Syntax is correct, field exists, and term query does not analyze or change case.Final Answer:
Term query is case sensitive and "Kimchy" does not match "kimchy" -> Option AQuick Check:
Term query = exact case match [OK]
- Assuming term query ignores case
- Thinking syntax error causes no results
- Believing term query analyzes or lowercases terms
tags field contains exactly the term urgent. The tags field is an array of keywords. Which query is best to use?Solution
Step 1: Understand term query on array fields
Term query matches exact terms in arrays, so it works directly on thetagsfield.Step 2: Evaluate other options
Match query analyzes text (not exact). Terms query expects multiple values to match any. Usingtags.keywordis unnecessary iftagsis keyword type.Final Answer:
{ "term": { "tags": "urgent" } } -> Option CQuick Check:
Term query matches exact term in array [OK]
- Using match query which analyzes and may not match exactly
- Using terms query incorrectly for single term
- Confusing field type and using .keyword unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
