Relevance scoring helps show the most useful search results first. It ranks results by how well they match your search.
Why relevance scoring ranks results in Elasticsearch
GET /index/_search
{
"query": {
"match": {
"field": "search term"
}
}
}The match query scores documents based on how well they match the search term.
Higher scores mean better matches and appear higher in results.
GET /books/_search
{
"query": {
"match": {
"title": "adventure"
}
}
}GET /products/_search
{
"query": {
"match": {
"description": "wireless headphones"
}
}
}This search looks for documents in the 'library' index where the 'summary' field matches 'climate change'. Elasticsearch scores each document by how well it matches, so the best matches appear first.
GET /library/_search
{
"query": {
"match": {
"summary": "climate change"
}
}
}Relevance scores are numbers that show how well a document matches the query.
Scores depend on factors like term frequency and how rare the term is in all documents.
You can customize scoring to fit your needs using different query types and functions.
Relevance scoring ranks search results by how well they match your query.
Higher scores mean better matches and appear first in results.
This helps users find the most useful information quickly.
Practice
Solution
Step 1: Understand relevance scoring purpose
Relevance scoring measures how closely a document matches the search query terms.Step 2: Compare options to scoring concept
Only How well a document matches the search query relates to matching quality, others are unrelated to scoring.Final Answer:
How well a document matches the search query -> Option AQuick Check:
Relevance scoring = match quality [OK]
- Confusing scoring with document size
- Thinking scoring depends on creation date
- Assuming number of fields affects score
Solution
Step 1: Identify query parts affecting scoring
Thematchquery scores documents based on how well they match the query text.Step 2: Differentiate from other parts
filterandaggsdo not affect scoring;sortorders results but doesn't calculate score.Final Answer:
match -> Option BQuick Check:
Match query = scoring influence [OK]
- Thinking filters affect scoring
- Confusing sort with scoring
- Assuming aggregations influence score
{
"query": {
"match": {
"title": "apple"
}
}
}
Documents:
1. {"title": "apple pie recipe"}
2. {"title": "banana apple smoothie"}
3. {"title": "fruit salad"}
Solution
Step 1: Understand match query scoring
Documents containing the exact term "apple" in the title get higher scores. Document 1 has "apple" at the start and is more focused.Step 2: Compare documents by term presence
Document 1 has "apple" prominently; Document 2 has "apple" but mixed with "banana"; Document 3 lacks "apple".Final Answer:
Document 1 -> Option CQuick Check:
Exact term presence = higher score [OK]
- Assuming all documents score equally
- Choosing document without the term
- Ignoring term position and frequency
{
"query": {
"bool": {
"filter": {
"match": { "content": "coffee" }
}
}
}
}
What is the main issue?Solution
Step 1: Understand bool query filter behavior
Thefilterclause filters documents but does not affect relevance scoring.Step 2: Identify why scoring is missing
Usingfilterwithmatchdisables scoring; to rank by relevance, usemustinstead.Final Answer:
"filter" does not calculate relevance scores -> Option AQuick Check:
Filter disables scoring, use must for relevance [OK]
- Thinking filter affects scoring
- Assuming match is misspelled
- Ignoring bool query clause differences
{
"query": {
"bool": {
"must": {
"match": { "text": "Elasticsearch" }
},
"filter": {
"term": { "status": "draft" }
}
}
}
}Solution
Step 1: Understand must and filter roles
mustruns scoring queries;filterfilters without scoring;must_notexcludes documents.Step 2: Apply exclusion for drafts
To exclude drafts, usemust_notwith a term filter on status "draft"; keepmustfor scoring match query.Final Answer:
Use "must" for match and "must_not" for filtering drafts -> Option DQuick Check:
Must scores, must_not excludes [OK]
- Using filter to exclude documents (doesn't exclude)
- Using should which affects scoring differently
- Filtering match query disables scoring
Ready to prove it?
Pass the test and this pattern moves unknown
