0
0
Elasticsearchquery~10 mins

Relevance score (_score) in Elasticsearch - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Relevance score (_score)
Query sent to Elasticsearch
Documents matched
Calculate _score for each document
Sort documents by _score descending
Return sorted documents with _score
When you search, Elasticsearch finds matching documents, calculates a relevance score (_score) for each, sorts them by this score, and returns the results.
Execution Sample
Elasticsearch
{
  "query": {
    "match": { "text": "apple" }
  }
}
This query searches for documents containing the word 'apple' and Elasticsearch calculates a _score for each match.
Execution Table
StepActionDocuments Matched_score CalculationResult
1Receive query for 'apple'All documents checkedNo score yetPrepare to score matches
2Match documents containing 'apple'Doc1, Doc2, Doc3Calculate _score based on term frequency and other factorsScores assigned: Doc1=1.5, Doc2=2.3, Doc3=0.9
3Sort documents by _score descendingDoc2, Doc1, Doc3Sorting doneDoc2 highest relevance, Doc3 lowest
4Return sorted documents with _scoreDoc2, Doc1, Doc3Scores includedUser sees results ranked by relevance
💡 All matching documents scored and sorted; results returned to user.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Documents Matched[][Doc1, Doc2, Doc3][Doc1, Doc2, Doc3][Doc2, Doc1, Doc3]
_score{}{Doc1:1.5, Doc2:2.3, Doc3:0.9}{Doc1:1.5, Doc2:2.3, Doc3:0.9}{Doc2:2.3, Doc1:1.5, Doc3:0.9}
Key Moments - 2 Insights
Why does Doc2 have a higher _score than Doc1 even though both contain 'apple'?
Because _score depends on factors like how often 'apple' appears in the document and how rare the term is across all documents, as shown in step 2 of the execution_table.
Does a document with a higher _score always mean it contains more words from the query?
Not necessarily. _score also considers term rarity and field length, so a document with fewer but more important matches can have a higher _score, as seen in the scoring details in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the _score of Doc3 after step 2?
A2.3
B1.5
C0.9
D0
💡 Hint
Check the '_score Calculation' column in row for step 2.
At which step are the documents sorted by their _score?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column describing sorting in the execution_table.
If Doc1 had a higher term frequency for 'apple', how would the _score change in the variable_tracker?
ADoc1's _score would decrease after Step 2
BDoc1's _score would increase after Step 2
CDoc3's _score would increase after Step 2
DNo change in any _score
💡 Hint
Refer to the '_score' values in variable_tracker after Step 2.
Concept Snapshot
Relevance score (_score) in Elasticsearch:
- Calculated for each matching document
- Based on term frequency, rarity, and field length
- Higher _score means more relevant
- Results sorted by _score descending
- Returned with documents for ranking
Full Transcript
When you send a search query to Elasticsearch, it checks all documents to find matches. For each matching document, Elasticsearch calculates a relevance score called _score. This score depends on how often the search terms appear, how rare those terms are, and other factors. After scoring, Elasticsearch sorts the documents by their _score from highest to lowest. Finally, it returns the sorted documents along with their _score values so you see the most relevant results first.