0
0
Elasticsearchquery~10 mins

Why relevance scoring ranks results in Elasticsearch - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why relevance scoring ranks results
User Query
Search Engine
Match Documents
Calculate Relevance Scores
Sort Documents by Score
Return Ranked Results
The search engine takes a user query, finds matching documents, calculates a relevance score for each, sorts them by score, and returns the ranked list.
Execution Sample
Elasticsearch
GET /my_index/_search
{
  "query": {
    "match": { "content": "apple" }
  }
}
This query searches for documents containing 'apple' and ranks them by relevance score.
Execution Table
StepActionDocuments MatchedScore CalculationSorted Order
1Receive query 'apple'---
2Find documents containing 'apple'Doc1, Doc2, Doc3--
3Calculate score for Doc1Doc1TF=3, IDF=1.5, Score=4.5-
4Calculate score for Doc2Doc2TF=1, IDF=1.5, Score=1.5-
5Calculate score for Doc3Doc3TF=2, IDF=1.5, Score=3.0-
6Sort documents by scoreDoc1, Doc3, Doc2-Doc1 > Doc3 > Doc2
7Return ranked resultsDoc1, Doc3, Doc2--
💡 All matched documents scored and sorted; results returned in descending order of relevance.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
Doc1_Score04.54.54.54.5
Doc2_Score001.51.51.5
Doc3_Score0003.03.0
Key Moments - 3 Insights
Why does Doc1 have a higher score than Doc3 even though both contain 'apple'?
Doc1 has a higher term frequency (TF=3) compared to Doc3 (TF=2), so its score is higher as shown in steps 3 and 5 of the execution_table.
What does the IDF value represent in the score calculation?
IDF (Inverse Document Frequency) measures how rare the term 'apple' is across all documents, boosting scores for rarer terms; it is constant (1.5) for all docs here as shown in score calculations.
Why are documents sorted after scoring?
Sorting by score ensures the most relevant documents appear first in results, as shown in step 6 where Doc1 > Doc3 > Doc2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the score of Doc3 after step 5?
A4.5
B1.5
C3.0
D0
💡 Hint
Check the 'Score Calculation' column at step 5 in the execution_table.
At which step are the documents sorted by their relevance score?
AStep 3
BStep 6
CStep 2
DStep 7
💡 Hint
Look for the 'Sort documents by score' action in the execution_table.
If Doc2 had TF=4 instead of 1, how would its score change after step 4?
AScore would be 6.0
BScore would be 1.5
CScore would be 4.5
DScore would be 3.0
💡 Hint
Score = TF * IDF; IDF is 1.5 as shown in the execution_table.
Concept Snapshot
Relevance scoring ranks search results by calculating a score for each document.
Score depends on term frequency (TF) and inverse document frequency (IDF).
Higher scores mean more relevant documents.
Results are sorted by score before returning.
This helps users see the best matches first.
Full Transcript
When a user searches, Elasticsearch finds documents containing the query terms. Each document gets a relevance score based on how often the term appears (TF) and how rare the term is across all documents (IDF). These scores are calculated step-by-step for each document. Then, documents are sorted by their scores so the most relevant appear first. For example, Doc1 with TF=3 scores higher than Doc3 with TF=2. This ranking helps users find the best matches quickly.