Bird
Raised Fist0
Elasticsearchquery~15 mins

Relevance score (_score) in Elasticsearch - Deep Dive

Choose your learning style10 modes available
Overview - Relevance score (_score)
What is it?
Relevance score, shown as _score in Elasticsearch, is a number that tells how well a document matches your search query. It helps rank search results so the most relevant ones appear first. The score is calculated based on how often the search terms appear and how important those terms are in the whole collection. This makes searching smarter and more useful.
Why it matters
Without relevance scores, search results would be random or just sorted by date or name, which might not help you find what you want quickly. Relevance scores solve the problem of ranking results by importance, saving time and improving user experience. This is crucial for search engines, online stores, and any system where finding the best match fast matters.
Where it fits
Before learning about relevance scores, you should understand basic Elasticsearch concepts like documents, fields, and queries. After this, you can explore advanced search features like boosting, custom scoring, and query tuning to improve search quality.
Mental Model
Core Idea
The relevance score (_score) measures how well a document matches your search query, guiding the order of search results.
Think of it like...
Imagine looking for a book in a library. The relevance score is like a librarian who knows which books mention your topic the most and puts those books on top of the pile for you.
┌───────────────┐
│ Search Query  │
└──────┬────────┘
       │
       ▼
┌─────────────────────────────┐
│ Documents in Elasticsearch   │
│ (each with text and fields)  │
└──────┬──────────────────────┘
       │
       ▼
┌─────────────────────────────┐
│ Calculate _score for each    │
│ document based on query      │
└──────┬──────────────────────┘
       │
       ▼
┌─────────────────────────────┐
│ Sort documents by _score     │
│ Highest score on top         │
└─────────────────────────────┘
Build-Up - 7 Steps
1
FoundationWhat is _score in Elasticsearch
🤔
Concept: Introduce the basic idea of _score as a number showing match quality.
When you search in Elasticsearch, each document gets a number called _score. This number shows how well the document matches your search words. Higher means better match. Elasticsearch uses this to sort results so you see the best matches first.
Result
You understand that _score is a number assigned to each document after a search.
Understanding that _score exists helps you see why search results are ordered the way they are.
2
FoundationHow _score affects search results
🤔
Concept: Explain that _score controls the order of results shown to users.
After Elasticsearch calculates _score for each document, it sorts all documents by this score from highest to lowest. This means documents that match your query better appear first. Without _score, results would be unordered or random.
Result
Search results are ranked by relevance, improving user experience.
Knowing _score controls ranking helps you trust and interpret search results better.
3
IntermediateFactors influencing the _score value
🤔Before reading on: do you think every matching word counts the same for _score, or do some words matter more? Commit to your answer.
Concept: Introduce how term frequency, inverse document frequency, and field length affect _score.
Elasticsearch uses a formula called TF-IDF (Term Frequency-Inverse Document Frequency) to calculate _score. It counts how often your search words appear in a document (term frequency). It also checks how rare those words are across all documents (inverse document frequency). Rare words add more to the score. Shorter fields with the term get a higher score too.
Result
Documents with many rare matching words and shorter matching fields get higher _score.
Understanding these factors explains why some documents rank higher even if they have fewer matching words.
4
IntermediateHow query types affect _score calculation
🤔Before reading on: do you think all query types calculate _score the same way, or do some ignore _score? Commit to your answer.
Concept: Explain that some queries calculate _score differently or not at all.
Most queries like match or multi_match calculate _score using TF-IDF or newer models. But some queries like filters or term queries can ignore _score and just include or exclude documents. This means filters speed up search by skipping scoring. Combining queries can mix scored and unscored parts.
Result
You learn that _score depends on query type and can be skipped for performance.
Knowing which queries affect _score helps you design faster and more relevant searches.
5
IntermediateRole of boosting in modifying _score
🤔Before reading on: do you think boosting changes the _score or just the order of results? Commit to your answer.
Concept: Introduce boosting as a way to increase or decrease _score for certain terms or fields.
Boosting lets you tell Elasticsearch to give more importance to some words or fields by multiplying their _score contribution. For example, boosting the title field means matches there count more than matches in the description. You can boost queries or individual terms to fine-tune ranking.
Result
Boosted documents get higher _score and appear earlier in results.
Understanding boosting shows how to control search relevance beyond default scoring.
6
AdvancedCustom scoring with function_score query
🤔Before reading on: do you think you can change how _score is calculated using your own rules? Commit to your answer.
Concept: Explain how to customize _score by adding functions like decay, random, or script scoring.
Elasticsearch lets you modify _score using the function_score query. You can add functions that increase or decrease scores based on document fields, dates, or random values. For example, you can boost recent documents or those with higher ratings. This custom scoring combines with the original _score to produce final ranking.
Result
Search results reflect both text relevance and custom business rules.
Knowing how to customize _score empowers you to build smarter, context-aware search experiences.
7
ExpertHow Elasticsearch scoring evolved with BM25
🤔Before reading on: do you think Elasticsearch always used the same scoring formula? Commit to your answer.
Concept: Describe the transition from TF-IDF to BM25 as the default scoring algorithm.
Originally, Elasticsearch used TF-IDF to calculate _score. Since version 5.0, it switched to BM25, a more advanced formula that better models term frequency saturation and document length normalization. BM25 improves relevance by avoiding overvaluing very frequent terms and better handling long documents. You can still configure which similarity algorithm to use per field.
Result
More accurate and user-friendly search rankings with BM25.
Understanding scoring evolution helps you appreciate modern search quality and how to tune it.
Under the Hood
When you run a search, Elasticsearch breaks down your query into terms and compares them to terms in each document's fields. It calculates a score for each document by combining how often terms appear (term frequency), how rare those terms are across all documents (inverse document frequency), and how long the field is (field length normalization). This calculation uses the BM25 algorithm by default, which balances these factors to produce a relevance score. The scores are then used to sort documents before returning results.
Why designed this way?
The scoring system was designed to mimic how humans judge relevance: terms that appear often in a document but are rare in the whole collection are more important. BM25 replaced TF-IDF to better handle cases where terms appear many times and to normalize for document length, improving search quality. This design balances accuracy and performance, making search fast and relevant.
┌───────────────┐
│ User Query    │
└──────┬────────┘
       │
       ▼
┌─────────────────────────────┐
│ Query Parsing & Analysis     │
└──────┬──────────────────────┘
       │
       ▼
┌─────────────────────────────┐
│ Term Matching in Documents   │
└──────┬──────────────────────┘
       │
       ▼
┌─────────────────────────────┐
│ Calculate TF (term freq)     │
│ Calculate IDF (inverse doc freq) │
│ Apply Field Length Norm      │
│ Use BM25 Formula             │
└──────┬──────────────────────┘
       │
       ▼
┌─────────────────────────────┐
│ Compute _score per Document  │
└──────┬──────────────────────┘
       │
       ▼
┌─────────────────────────────┐
│ Sort Documents by _score     │
└─────────────────────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does a higher _score always mean the document is more useful? Commit yes or no.
Common Belief:A higher _score always means the document is the best and most useful result.
Tap to reveal reality
Reality:A higher _score means better text match, but not necessarily more useful. Other factors like freshness, popularity, or business rules might matter more.
Why it matters:Relying only on _score can lead to showing irrelevant or outdated results first, hurting user satisfaction.
Quick: Do filters affect _score? Commit yes or no.
Common Belief:Filters change the _score of documents by boosting or lowering it.
Tap to reveal reality
Reality:Filters do not affect _score; they only include or exclude documents without scoring.
Why it matters:Confusing filters with scoring can cause unexpected search behavior and performance issues.
Quick: Is _score always between 0 and 1? Commit yes or no.
Common Belief:_score is a normalized value between 0 and 1.
Tap to reveal reality
Reality:_score can be any positive number and is not normalized; it depends on the query and index.
Why it matters:Assuming normalization can cause wrong interpretations or incorrect custom scoring.
Quick: Does boosting a term always guarantee top ranking? Commit yes or no.
Common Belief:Boosting a term guarantees documents with that term appear first.
Tap to reveal reality
Reality:Boosting increases _score contribution but does not guarantee top ranking if other factors outweigh it.
Why it matters:Overusing boosting can lead to unexpected rankings and poor search quality.
Expert Zone
1
BM25 parameters like k1 and b can be tuned per field to adjust term frequency saturation and length normalization, affecting relevance subtly.
2
The _score is a floating-point number that can be influenced by index statistics that change as documents are added or removed, causing score shifts over time.
3
Combining multiple queries with different scoring behaviors (e.g., bool with must and filter) requires understanding how scores are combined or ignored to avoid surprises.
When NOT to use
Relying solely on _score is not ideal when business rules or user context matter more. In such cases, use function_score queries, custom scripts, or external ranking systems like learning-to-rank models.
Production Patterns
In production, _score is often combined with filters for performance, boosted by business priorities, and adjusted with decay functions for freshness. Monitoring score distribution helps detect index changes affecting relevance.
Connections
Information Retrieval
Relevance scoring in Elasticsearch builds on classic information retrieval models like TF-IDF and BM25.
Understanding these models from information retrieval theory helps grasp why _score behaves as it does and how to improve search quality.
Machine Learning Ranking
Custom scoring and boosting in Elasticsearch can be seen as simple ranking models, which relate to more advanced machine learning ranking techniques.
Knowing _score basics prepares you to integrate Elasticsearch with ML-based ranking for smarter search.
Human Decision Making
Relevance scoring mimics how humans judge importance by weighing rare and frequent terms differently.
Recognizing this connection helps appreciate the design of scoring algorithms as attempts to model human judgment.
Common Pitfalls
#1Expecting _score to be consistent across different queries or index states.
Wrong approach:Running the same query multiple times and assuming _score values will not change.
Correct approach:Understand that _score depends on index statistics and can vary; use relative ranking rather than absolute score values.
Root cause:Misunderstanding that _score is dynamic and depends on the current index content and query context.
#2Using filters when you want to boost relevance, expecting _score to change.
Wrong approach:Applying a filter query to boost documents with a certain field value.
Correct approach:Use boosting or function_score queries to modify _score instead of filters.
Root cause:Confusing filtering (which excludes or includes documents) with scoring (which ranks documents).
#3Overusing boosting on multiple fields or terms without testing impact.
Wrong approach:Adding high boosts to many fields hoping to improve relevance.
Correct approach:Carefully test and tune boosts to avoid skewing results and maintain balanced relevance.
Root cause:Lack of understanding of how boosting affects combined _score and ranking.
Key Takeaways
The _score in Elasticsearch measures how well a document matches a search query, guiding result ranking.
It is calculated using algorithms like BM25 that consider term frequency, rarity, and field length.
Not all queries affect _score; filters exclude documents without scoring to improve performance.
Boosting and custom scoring let you adjust _score to reflect business priorities or context.
Understanding _score's dynamic nature and limitations helps build better, more relevant search experiences.

Practice

(1/5)
1. What does the _score field represent in an Elasticsearch search result?
easy
A. The total number of documents in the index
B. The unique identifier of the document
C. The relevance score showing how well a document matches the query
D. The timestamp when the document was indexed

Solution

  1. Step 1: Understand the purpose of _score

    The _score is a numeric value assigned by Elasticsearch to each document based on how well it matches the search query.
  2. Step 2: Differentiate _score from other fields

    Unlike document ID or timestamps, _score specifically measures relevance, not identity or metadata.
  3. Final Answer:

    The relevance score showing how well a document matches the query -> Option C
  4. Quick Check:

    _score = relevance [OK]
Hint: Remember: _score means relevance, not ID or count [OK]
Common Mistakes:
  • Confusing _score with document ID
  • Thinking _score is a timestamp
  • Assuming _score counts documents
2. Which of the following is the correct way to access the _score in an Elasticsearch query response JSON?
easy
A. response.score
B. response._score
C. response.hits._score
D. response.hits.hits[0]._score

Solution

  1. Step 1: Understand Elasticsearch response structure

    Search results are inside hits.hits array, each element representing a document with its _score.
  2. Step 2: Identify correct path to _score

    The _score is a property of each hit object, so it is accessed as hits.hits[0]._score for the first result.
  3. Final Answer:

    response.hits.hits[0]._score -> Option D
  4. Quick Check:

    Access _score inside hits array [OK]
Hint: Remember: _score is inside each hit in hits.hits array [OK]
Common Mistakes:
  • Trying to access _score directly on response
  • Using hits._score which is invalid
  • Using response.score which does not exist
3. Given this Elasticsearch query snippet, which document will have the highest _score?
{
  "query": {
    "match": {
      "title": "apple banana"
    }
  }
}
Documents: 1. {"title": "apple banana fruit"} 2. {"title": "banana apple"} 3. {"title": "apple orange"} 4. {"title": "fruit banana apple"}
medium
A. Document 1
B. Document 2
C. Document 3
D. Document 4

Solution

  1. Step 1: Understand match query scoring

    Elasticsearch scores documents higher when query terms appear closer and in the same order in the field.
  2. Step 2: Compare documents for term order and presence

    Document 2 has "banana apple" which matches both terms but reversed order; Document 1 has "apple banana fruit" matching query order exactly; Document 4 has terms but with extra word; Document 3 misses "banana".
  3. Step 3: Consider term frequency and order

    Exact order and proximity increase score, so Document 1 scores highest because it matches "apple banana" exactly in order and proximity.
  4. Final Answer:

    Document 1 -> Option A
  5. Quick Check:

    Exact term order and proximity = highest _score [OK]
Hint: Exact term order and proximity boost _score [OK]
Common Mistakes:
  • Ignoring term order effect on score
  • Assuming all documents with terms score equally
  • Overlooking missing query terms in documents
4. You wrote this Elasticsearch query but all documents have _score equal to 1.0, which is unexpected:
{
  "query": {
    "match_all": {}
  }
}
What is the likely reason for this behavior?
medium
A. The match_all query assigns a constant _score of 1.0 to all documents
B. The index is empty so _score defaults to 1.0
C. You forgot to specify the field in the query
D. The _score is not returned by default

Solution

  1. Step 1: Understand match_all query behavior

    The match_all query matches every document equally and assigns a default _score of 1.0 to all.
  2. Step 2: Check if this matches the observed behavior

    Since all documents have _score 1.0, it confirms match_all is working as designed.
  3. Final Answer:

    The match_all query assigns a constant _score of 1.0 to all documents -> Option A
  4. Quick Check:

    match_all = constant _score 1.0 [OK]
Hint: match_all always gives _score 1.0 [OK]
Common Mistakes:
  • Thinking _score varies with match_all
  • Assuming missing field causes this
  • Believing _score is not returned by default
5. You want to boost documents that contain the word "premium" in the description field to increase their _score. Which Elasticsearch query correctly applies a boost to achieve this?
{
  "query": {
    "bool": {
      "should": [
        { "match": { "description": "premium" } },
        { "match": { "description": { "query": "premium", "boost": 2 } } }
      ]
    }
  }
}
hard
A. Use two match queries in should without boost
B. Use a single match with boost inside the field object
C. Use match_all with boost parameter
D. Use term query without boost

Solution

  1. Step 1: Understand boosting in Elasticsearch queries

    Boosting is done by adding a boost parameter inside the query clause to increase _score for matching documents.
  2. Step 2: Identify correct syntax for boosting

    The correct way is to use a single match query with boost inside the field object, like { "match": { "description": { "query": "premium", "boost": 2 } } }.
  3. Step 3: Evaluate given options

    Use a single match with boost inside the field object matches this syntax; other options either lack boost or use wrong query types.
  4. Final Answer:

    Use a single match with boost inside the field object -> Option B
  5. Quick Check:

    Boost inside match field object increases _score [OK]
Hint: Put boost inside match field object for scoring [OK]
Common Mistakes:
  • Using multiple matches without boost
  • Applying boost to match_all query
  • Using term query without boost
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