Relevance score (_score) in Elasticsearch - Time & Space Complexity
When Elasticsearch searches documents, it calculates a relevance score called _score to rank results.
We want to understand how the time to calculate these scores grows as we search more documents.
Analyze the time complexity of the following Elasticsearch query snippet.
GET /my_index/_search
{
"query": {
"match": {
"content": "search term"
}
}
}
This query searches documents matching "search term" and calculates a relevance score for each matched document.
Look at what repeats when Elasticsearch processes this query.
- Primary operation: Calculating the
_scorefor each matched document. - How many times: Once for every document that matches the query.
As the number of matched documents grows, the number of score calculations grows too.
| Input Size (matched docs) | Approx. Operations (score calculations) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows directly with the number of matched documents.
Time Complexity: O(n)
This means the time to calculate relevance scores grows linearly with the number of matched documents.
[X] Wrong: "Calculating the _score is constant time no matter how many documents match."
[OK] Correct: Each matched document needs its own score calculation, so more matches mean more work.
Understanding how scoring scales helps you explain search performance clearly and shows you grasp how Elasticsearch handles queries behind the scenes.
"What if we added a filter that reduces matched documents before scoring? How would that affect the time complexity?"
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
