0
0
Elasticsearchquery~10 mins

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

Choose your learning style9 modes available
Concept Flow - Function score query
Start: Receive Query
Apply Base Query
Apply Functions to Modify Scores
Combine Function Scores with Base Score
Return Documents with Updated Scores
End
The function score query starts with a base query, applies scoring functions to modify document scores, combines them, and returns the results.
Execution Sample
Elasticsearch
{
  "query": {
    "function_score": {
      "query": { "match": { "field": "value" } },
      "functions": [
        { "weight": 2 },
        { "filter": { "term": { "status": "active" } }, "weight": 3 }
      ],
      "score_mode": "sum"
    }
  }
}
This query matches documents where 'field' contains 'value', then boosts scores by weights, adding extra weight if 'status' is 'active'.
Execution Table
StepActionBase Query ResultFunction AppliedScore ChangeCombined Score
1Run base query match field:valueDoc1=1.0, Doc2=1.0, Doc3=0.0None0Doc1=1.0, Doc2=1.0, Doc3=0.0
2Apply function weight=2 to all docsSame as aboveWeight 2+2Doc1=3.0, Doc2=3.0, Doc3=2.0
3Apply function weight=3 filtered by status:activeSame as aboveWeight 3 if status=active+3 to Doc1 onlyDoc1=6.0, Doc2=3.0, Doc3=2.0
4Combine scores using sum modeFinal scoresSum of weightsN/ADoc1=6.0, Doc2=3.0, Doc3=2.0
💡 All functions applied, scores combined, final ranked documents returned.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Doc1 Score01.03.06.06.0
Doc2 Score01.03.03.03.0
Doc3 Score00.02.02.02.0
Key Moments - 3 Insights
Why does Doc3 get a score even though it doesn't match the base query?
Because the weight function applies to all documents by default, Doc3 gets a base weight of 2 even with base score 0, as shown in step 2 of the execution_table.
How does the filter in the second function affect scoring?
The filter limits the weight of 3 to only documents where status is active, so only Doc1 gets this extra weight, as seen in step 3 of the execution_table.
What does score_mode 'sum' do with multiple function scores?
It adds all function weights to the base score, combining them by summing, resulting in the final scores shown in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is Doc2's score after applying the first weight function?
A1.0
B4.0
C3.0
D0.0
💡 Hint
Check 'After Step 2' column for Doc2 Score in variable_tracker.
At which step does the filter for status:active apply its weight?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Function Applied' column in execution_table for when the filter is used.
If the score_mode changed from 'sum' to 'multiply', how would Doc1's final score change?
AIt would be 0.0
BIt would be 1.0 * 2 * 3 = 6.0
CIt would be 6.0 multiplied by 3
DIt would be 6.0
💡 Hint
Multiplying base score and weights means multiplying 1.0 (base) * 2 * 3 for Doc1.
Concept Snapshot
Function score query syntax:
{
  "query": {
    "function_score": {
      "query": { ... },
      "functions": [ ... ],
      "score_mode": "sum"|"multiply"|...
    }
  }
}

Behavior: Modifies base query scores by applying functions with optional filters.
Key rule: Functions can boost or modify scores; score_mode controls combination.
Full Transcript
The function score query starts by running the base query to find matching documents. Then, it applies one or more functions that adjust the scores of these documents. Each function can have a filter to apply only to certain documents and a weight to boost scores. The scores from all functions combine using a mode like sum or multiply. For example, a base query matches documents with 'field' equal to 'value'. A weight function adds 2 to all documents' scores. Another function adds 3 only to documents where 'status' is 'active'. The final scores are the sum of base scores and these weights. This process helps rank documents better based on custom criteria.