What if your search could magically know which results matter most to you?
Why Relevance score (_score) in Elasticsearch? - Purpose & Use Cases
Imagine you have a huge library of books and someone asks you to find the most useful books about "gardening." You try to look through each book one by one, guessing which might be best without any clear way to compare them.
Manually checking each book is slow and confusing. You might miss the best ones or pick less useful ones because you have no clear way to measure how well each book matches the request.
Elasticsearch gives each search result a relevance score called _score. This score tells you how well each result matches the search terms, so you can easily pick the best matches without guessing.
results = [] for book in library: if 'gardening' in book.text: results.append(book) # No ranking, just a list
results = elasticsearch.search(query='gardening') # Each result has a _score showing relevance
It lets you quickly find and rank the most relevant information from huge data collections, saving time and improving accuracy.
When you search for "best pizza places" on a review site, the results are ranked by how well they match your search, showing the tastiest and closest options first thanks to _score.
Manual searching is slow and unorganized.
_score ranks results by relevance automatically.
This helps find the best matches quickly and easily.
Practice
_score field represent in an Elasticsearch search result?Solution
Step 1: Understand the purpose of
The_score_scoreis a numeric value assigned by Elasticsearch to each document based on how well it matches the search query.Step 2: Differentiate
Unlike document ID or timestamps,_scorefrom other fields_scorespecifically measures relevance, not identity or metadata.Final Answer:
The relevance score showing how well a document matches the query -> Option CQuick Check:
_score= relevance [OK]
_score means relevance, not ID or count [OK]- Confusing
_scorewith document ID - Thinking
_scoreis a timestamp - Assuming
_scorecounts documents
_score in an Elasticsearch query response JSON?Solution
Step 1: Understand Elasticsearch response structure
Search results are insidehits.hitsarray, each element representing a document with its_score.Step 2: Identify correct path to
The_score_scoreis a property of each hit object, so it is accessed ashits.hits[0]._scorefor the first result.Final Answer:
response.hits.hits[0]._score -> Option DQuick Check:
Access_scoreinside hits array [OK]
_score is inside each hit in hits.hits array [OK]- Trying to access
_scoredirectly on response - Using
hits._scorewhich is invalid - Using
response.scorewhich does not exist
_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"}Solution
Step 1: Understand match query scoring
Elasticsearch scores documents higher when query terms appear closer and in the same order in the field.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".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.Final Answer:
Document 1 -> Option AQuick Check:
Exact term order and proximity = highest_score[OK]
_score [OK]- Ignoring term order effect on score
- Assuming all documents with terms score equally
- Overlooking missing query terms in documents
_score equal to 1.0, which is unexpected:
{
"query": {
"match_all": {}
}
}
What is the likely reason for this behavior?Solution
Step 1: Understand
Thematch_allquery behaviormatch_allquery matches every document equally and assigns a default_scoreof 1.0 to all.Step 2: Check if this matches the observed behavior
Since all documents have_score1.0, it confirmsmatch_allis working as designed.Final Answer:
Thematch_allquery assigns a constant_scoreof 1.0 to all documents -> Option AQuick Check:
match_all= constant_score1.0 [OK]
match_all always gives _score 1.0 [OK]- Thinking
_scorevaries withmatch_all - Assuming missing field causes this
- Believing
_scoreis not returned by default
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 } } }
]
}
}
}Solution
Step 1: Understand boosting in Elasticsearch queries
Boosting is done by adding aboostparameter inside the query clause to increase_scorefor matching documents.Step 2: Identify correct syntax for boosting
The correct way is to use a singlematchquery withboostinside the field object, like{ "match": { "description": { "query": "premium", "boost": 2 } } }.Step 3: Evaluate given options
Use a singlematchwithboostinside the field object matches this syntax; other options either lack boost or use wrong query types.Final Answer:
Use a singlematchwithboostinside the field object -> Option BQuick Check:
Boost insidematchfield object increases_score[OK]
boost inside match field object for scoring [OK]- Using multiple matches without boost
- Applying boost to
match_allquery - Using
termquery without boost
Ready to prove it?
Pass the test and this pattern moves unknown
