Bird
Raised Fist0
Elasticsearchquery~20 mins

Relevance score (_score) in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Elasticsearch Relevance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the _score of a match query with exact term match?
Given the following Elasticsearch query, what will be the _score of the matched document relative to a document that does not contain the term?
Elasticsearch
{
  "query": {
    "match": {
      "title": "Elasticsearch"
    }
  }
}
ANull because _score is not returned by default
B-1 because the term is matched negatively
CA positive float value indicating relevance, higher than zero
DAlways zero because match queries do not calculate _score
Attempts:
2 left
💡 Hint
Think about how Elasticsearch scores documents based on term frequency and inverse document frequency.
🧠 Conceptual
intermediate
2:00remaining
How does Elasticsearch calculate the _score for a multi-match query?
In Elasticsearch, when you use a multi-match query across multiple fields, how is the _score for each document computed?
AIt ignores field matches and assigns a fixed _score
BIt sums the individual _scores from each field where the term matches
CIt averages the _scores from all fields regardless of matches
DIt takes the maximum _score from any single field match
Attempts:
2 left
💡 Hint
Consider how relevance from multiple fields contributes to the overall score.
🔧 Debug
advanced
2:00remaining
Why does this bool query always return zero _score?
Examine the following Elasticsearch query. Why does it always return documents with _score equal to zero?
Elasticsearch
{
  "query": {
    "bool": {
      "filter": [
        { "term": { "status": "active" } },
        { "term": { "category": "books" } }
      ]
    }
  }
}
ABecause filters do not contribute to _score, so all scores are zero
BBecause term queries inside filter always return zero hits
CBecause bool queries require must clauses to calculate _score
DBecause the index is empty, so no scores are calculated
Attempts:
2 left
💡 Hint
Think about the difference between filter and must clauses in bool queries.
📝 Syntax
advanced
2:00remaining
Which query syntax correctly boosts the _score of documents matching 'python'?
Select the Elasticsearch query syntax that correctly boosts the _score of documents containing the term 'python' in the 'description' field.
A{ "match": { "description": { "boost": 2.0, "query": "python" } } }
B{ "match": { "description": "python", "boost": 2.0 } }
C{ "match": { "description": { "query": "python" }, "boost": 2.0 } }
D{ "match": { "description": { "query": "python", "boost": 2.0 } } }
Attempts:
2 left
💡 Hint
Check the correct placement of the boost parameter inside the match query object.
🚀 Application
expert
3:00remaining
How to combine function_score with match query to boost recent documents?
You want to boost the _score of documents matching 'data science' in the 'content' field, but also boost documents published in the last 30 days. Which Elasticsearch query achieves this?
A
{
  "function_score": {
    "query": { "match": { "content": "data science" } },
    "functions": [
      {
        "filter": {
          "range": { "publish_date": { "gte": "now-30d/d" } }
        },
        "weight": 3
      }
    ],
    "boost_mode": "multiply"
  }
}
B
{
  "query": {
    "bool": {
      "must": { "match": { "content": "data science" } },
      "should": {
        "range": { "publish_date": { "gte": "now-30d/d" } }
      }
    }
  }
}
C
{
  "function_score": {
    "query": { "match": { "content": "data science" } },
    "boost": 3,
    "filter": {
      "range": { "publish_date": { "gte": "now-30d/d" } }
    }
  }
}
D
{
  "query": {
    "match": {
      "content": {
        "query": "data science",
        "boost": 3
      }
    }
  }
}
Attempts:
2 left
💡 Hint
Use function_score to combine query relevance with custom boosting based on a filter.

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