Bird
Raised Fist0
Elasticsearchquery~10 mins

Constant score query in Elasticsearch - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Constant score query
Start Query
↓
Apply Filter
↓
Wrap Filter in Constant Score
↓
Assign Fixed Score
↓
Return Results with Constant Score
↓
End
The constant score query wraps a filter and assigns a fixed score to all matching documents, ignoring relevance scoring.
Execution Sample
Elasticsearch
{
  "constant_score": {
    "filter": { "term": { "status": "active" } },
    "boost": 1.5
  }
}
This query finds documents where status is 'active' and assigns them a fixed score of 1.5.
Execution Table
StepActionFilter AppliedScore AssignedResult
1Start query executionNoneNoneQuery begins
2Apply filterstatus = 'active'NoneDocuments filtered by status
3Wrap filter in constant_scorestatus = 'active'Fixed score 1.5Score set to 1.5 for all matches
4Return resultsstatus = 'active'1.5Documents returned with constant score
5EndN/AN/AQuery execution complete
💡 All documents matching the filter receive the fixed score; no relevance scoring applied.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Filtered DocumentsNoneDocs with status='active'Docs with status='active'Docs with status='active'
ScoreNoneNone1.5 (constant)1.5 (constant)
Key Moments - 2 Insights
Why does the constant score query ignore relevance scoring?
Because the filter is wrapped in a constant_score query which assigns a fixed score (see execution_table step 3), so relevance scores from matching terms are not calculated.
What happens if the filter matches no documents?
No documents are returned because the filter excludes all (see execution_table step 2), so no scores are assigned or results returned.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what score is assigned to matching documents?
AScore of 0
B1.5 fixed score
CRelevance score based on term frequency
DNo score assigned
💡 Hint
Check the 'Score Assigned' column at step 3 in the execution_table.
At which step are documents filtered by the condition 'status = active'?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Filter Applied' column in the execution_table.
If the boost value changes from 1.5 to 2.0, what changes in the execution?
AThe fixed score assigned changes to 2.0
BThe filter changes to status='2.0'
CThe documents filtered change
DNo change in score
💡 Hint
The 'Score Assigned' column in execution_table step 3 shows the fixed score from boost.
Concept Snapshot
Constant score query syntax:
{
  "constant_score": {
    "filter": { ... },
    "boost": fixed_score
  }
}

Behavior:
- Filters documents by condition
- Assigns fixed score to all matches
- Ignores relevance scoring

Use when you want uniform scoring for filtered results.
Full Transcript
The constant score query in Elasticsearch wraps a filter and assigns a fixed score to all documents matching that filter. First, the query applies the filter condition to select documents. Then, it wraps this filter in a constant_score query which sets the score for all matched documents to a fixed value, ignoring the usual relevance scoring. This is useful when you want to treat all filtered documents equally in scoring. For example, filtering documents where status is 'active' and assigning them a score of 1.5 means all active documents have the same score regardless of term frequency or other factors. The execution steps show filtering first, then assigning the constant score, and finally returning the results. If no documents match the filter, no results are returned. Changing the boost value changes the fixed score assigned. This approach simplifies scoring when relevance is not important.

Practice

(1/5)
1. What is the main purpose of a constant_score query in Elasticsearch?
easy
A. To filter documents and assign the same score to all matched documents
B. To sort documents by their relevance score
C. To perform a full-text search with varying scores
D. To aggregate documents by a field value

Solution

  1. 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.
  2. Step 2: Compare with other query types

    Unlike full-text queries that score by relevance, constant_score keeps scores equal for filtered documents.
  3. Final Answer:

    To filter documents and assign the same score to all matched documents -> Option A
  4. Quick Check:

    constant_score = equal scoring for filtered docs [OK]
Hint: Constant score means all matched docs get the same score [OK]
Common Mistakes:
  • Thinking it sorts by relevance
  • Confusing with full-text scoring
  • Assuming it aggregates data
2. Which of the following is the correct syntax to create a constant score query with a filter on field status equal to active?
easy
A. {"constant_score": {"filter": {"match": {"status": "active"}}}}
B. {"constant_score": {"query": {"term": {"status": "active"}}}}
C. {"constant_score": {"term": {"status": "active"}}}
D. {"constant_score": {"filter": {"term": {"status": "active"}}}}

Solution

  1. 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.
  2. Step 2: Check filter syntax

    The filter should be a term query inside the filter object, correctly written as {"term": {"status": "active"}}.
  3. Final Answer:

    {"constant_score": {"filter": {"term": {"status": "active"}}}} -> Option D
  4. Quick Check:

    Use "filter" with term inside constant_score [OK]
Hint: Use "filter" key with term query inside constant_score [OK]
Common Mistakes:
  • Using "query" instead of "filter"
  • Placing term query directly under constant_score
  • Using match instead of term for exact filter
3. Given this Elasticsearch query snippet, what will be the score of each matched document?
{
  "constant_score": {
    "filter": { "term": { "category": "books" } },
    "boost": 3
  }
}
medium
A. The score will vary based on relevance
B. The score will be 3 for all matched documents
C. The score will be 1 for all matched documents
D. The score will be 0 because boost is ignored

Solution

  1. 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.
  2. Step 2: Calculate the score

    By default, constant_score gives score 1, multiplied by boost 3, resulting in score 3 for all matches.
  3. Final Answer:

    The score will be 3 for all matched documents -> Option B
  4. Quick Check:

    constant_score x boost = 3 [OK]
Hint: Boost multiplies constant score, so score = boost value [OK]
Common Mistakes:
  • Assuming scores vary by relevance
  • Ignoring the boost value
  • Thinking boost is ignored in constant_score
4. Identify the error in this constant score query:
{
  "constant_score": {
    "filter": {
      "terms": {
        "tags": ["tech", "science"]
      }
    },
    "boost": "2"
  }
}
medium
A. The boost value should be a number, not a string
B. The filter key should be replaced with query
C. The terms query cannot be used inside constant_score
D. The tags field should be inside a match query

Solution

  1. 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.
  2. Step 2: Validate filter and terms usage

    Using "filter" with a "terms" query is valid syntax, so no error there.
  3. Final Answer:

    The boost value should be a number, not a string -> Option A
  4. Quick Check:

    boost must be numeric, not string [OK]
Hint: Boost must be a number, not quoted string [OK]
Common Mistakes:
  • Using string for boost instead of number
  • Replacing filter with query incorrectly
  • Misunderstanding terms query usage
5. You want to find all documents where status is published and boost their score by 5, but keep all matched documents scored equally. Which query correctly achieves this?
hard
A. { "match": { "status": "published", "boost": 5 } }
B. { "bool": { "must": { "term": { "status": "published" } }, "boost": 5 } }
C. { "constant_score": { "filter": { "term": { "status": "published" } }, "boost": 5 } }
D. { "constant_score": { "query": { "term": { "status": "published" } }, "boost": 5 } }

Solution

  1. 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.
  2. 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.
  3. 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.
  4. Final Answer:

    { "constant_score": { "filter": { "term": { "status": "published" } }, "boost": 5 } } -> Option C
  5. Quick Check:

    constant_score + filter + boost = equal boosted scores [OK]
Hint: Use constant_score with filter and numeric boost for equal boosted scores [OK]
Common Mistakes:
  • Using query instead of filter inside constant_score
  • Using bool or match queries that score variably
  • Setting boost as string instead of number
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard