The relevance score (_score) helps you find the best matching documents for your search. It shows how well each result fits your query.
Relevance score (_score) in Elasticsearch
GET /index/_search
{
"query": {
"match": {
"field": "search term"
}
}
}The _score is automatically calculated for each document returned by the query.
You can use _score to sort or filter results.
GET /products/_search
{
"query": {
"match": {
"name": "coffee"
}
}
}GET /articles/_search
{
"query": {
"multi_match": {
"query": "climate change",
"fields": ["title", "content"]
}
}
}GET /books/_search
{
"query": {
"match": {
"author": "Rowling"
}
},
"sort": [
{"_score": "desc"}
]
}This query searches the "library" index for documents with "adventure" in the title. Each result has a _score showing how well it matches.
GET /library/_search
{
"query": {
"match": {
"title": "adventure"
}
}
}A higher _score means a better match to your search query.
Scores depend on the query type and the data in your documents.
You can customize scoring with advanced queries, but the default is usually good for simple searches.
The _score shows how well each document matches your search.
Use it to sort or filter search results by relevance.
It helps users find the most useful information quickly.
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
