0
0
Elasticsearchquery~10 mins

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

Choose your learning style9 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.