Constant score query in Elasticsearch - Time & Space Complexity
We want to understand how the time to run a constant score query changes as the data grows.
Specifically, how does the query's work increase when there are more documents?
Analyze the time complexity of the following code snippet.
{
"constant_score": {
"filter": {
"term": { "status": "active" }
},
"boost": 1.2
}
}
This query finds documents where the status is "active" and assigns a fixed score to each match.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning the index to find documents matching the filter.
- How many times: Once per document or per matching term in the index.
As the number of documents grows, the query checks more entries to find matches.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows roughly in direct proportion to the number of documents.
Time Complexity: O(n)
This means the time to run the query grows linearly as the number of documents increases.
[X] Wrong: "The constant score query runs instantly no matter how big the data is."
[OK] Correct: Even though the score is fixed, the query still needs to check documents to find matches, so time grows with data size.
Understanding how queries scale helps you write efficient searches and explain your choices clearly in real projects.
"What if we replaced the term filter with a match_all filter? How would the time complexity change?"
Practice
constant_score query in Elasticsearch?Solution
Step 1: Understand the role of constant_score query
The constant_score query filters documents and assigns the same score to all matches, ignoring relevance scoring.Step 2: Compare with other query types
Unlike full-text queries that score by relevance, constant_score keeps scores equal for filtered documents.Final Answer:
To filter documents and assign the same score to all matched documents -> Option AQuick Check:
constant_score = equal scoring for filtered docs [OK]
- Thinking it sorts by relevance
- Confusing with full-text scoring
- Assuming it aggregates data
status equal to active?Solution
Step 1: Identify correct key for filtering
The constant_score query uses the key "filter" to specify the filter clause, not "query" or direct term.Step 2: Check filter syntax
The filter should be a term query inside the filter object, correctly written as {"term": {"status": "active"}}.Final Answer:
{"constant_score": {"filter": {"term": {"status": "active"}}}} -> Option DQuick Check:
Use "filter" with term inside constant_score [OK]
- Using "query" instead of "filter"
- Placing term query directly under constant_score
- Using match instead of term for exact filter
{
"constant_score": {
"filter": { "term": { "category": "books" } },
"boost": 3
}
}Solution
Step 1: Understand constant_score with boost
The constant_score query assigns the same score to all matched documents, and the boost multiplies that score.Step 2: Calculate the score
By default, constant_score gives score 1, multiplied by boost 3, resulting in score 3 for all matches.Final Answer:
The score will be 3 for all matched documents -> Option BQuick Check:
constant_score x boost = 3 [OK]
- Assuming scores vary by relevance
- Ignoring the boost value
- Thinking boost is ignored in constant_score
{
"constant_score": {
"filter": {
"terms": {
"tags": ["tech", "science"]
}
},
"boost": "2"
}
}Solution
Step 1: Check boost data type
The boost parameter must be a numeric value, not a string. Here, "2" is a string, which is invalid.Step 2: Validate filter and terms usage
Using "filter" with a "terms" query is valid syntax, so no error there.Final Answer:
The boost value should be a number, not a string -> Option AQuick Check:
boost must be numeric, not string [OK]
- Using string for boost instead of number
- Replacing filter with query incorrectly
- Misunderstanding terms query usage
status is published and boost their score by 5, but keep all matched documents scored equally. Which query correctly achieves this?Solution
Step 1: Use constant_score with filter and boost
To keep scores equal and apply a boost, use constant_score with a filter clause and boost parameter.Step 2: Validate correct syntax
{ "constant_score": { "filter": { "term": { "status": "published" } }, "boost": 5 } } uses "filter" with a term query and numeric boost, which is the correct syntax.Step 3: Check other options
{ "bool": { "must": { "term": { "status": "published" } }, "boost": 5 } } uses bool query which scores by relevance, not constant. { "match": { "status": "published", "boost": 5 } } is a match query with boost but scores vary. { "constant_score": { "query": { "term": { "status": "published" } }, "boost": 5 } } incorrectly uses "query" instead of "filter" inside constant_score.Final Answer:
{ "constant_score": { "filter": { "term": { "status": "published" } }, "boost": 5 } } -> Option CQuick Check:
constant_score + filter + boost = equal boosted scores [OK]
- Using query instead of filter inside constant_score
- Using bool or match queries that score variably
- Setting boost as string instead of number
Ready to prove it?
Pass the test and this pattern moves unknown
