TF-IDF and BM25 scoring in Elasticsearch - Time & Space Complexity
When searching documents, Elasticsearch uses scoring methods like TF-IDF and BM25 to rank results.
We want to understand how the time to calculate these scores grows as the number of documents or terms increases.
Analyze the time complexity of this Elasticsearch query using BM25 scoring.
GET /my_index/_search
{
"query": {
"match": {
"content": "search terms here"
}
}
}
This query searches documents matching the given terms and scores them using BM25 by default.
Look at what repeats during scoring:
- Primary operation: Calculating term frequency and inverse document frequency for each term in each document.
- How many times: For each query term, Elasticsearch checks every document containing that term.
As the number of documents and query terms grow, the scoring work increases.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 documents, 2 terms | About 20 checks (2 terms x 10 docs) |
| 100 documents, 2 terms | About 200 checks |
| 1000 documents, 5 terms | About 5000 checks |
Pattern observation: The work grows roughly with the number of documents times the number of query terms.
Time Complexity: O(n * m)
This means the scoring time grows proportionally with the number of documents n and the number of query terms m.
[X] Wrong: "Scoring time depends only on the number of query terms, not on documents."
[OK] Correct: Each term must be checked in every document that contains it, so more documents mean more scoring work.
Understanding how scoring scales helps you explain search performance and design efficient queries in real projects.
What if we changed the scoring method from BM25 to a simpler constant score? How would the time complexity change?
Practice
Solution
Step 1: Understand TF-IDF basics
TF-IDF scores words by multiplying term frequency (TF) with inverse document frequency (IDF), but it does not adjust for document length or term frequency saturation.Step 2: Understand BM25 improvements
BM25 improves TF-IDF by adding term frequency saturation (diminishing returns for repeated terms) and normalizing by document length, making scores more balanced.Final Answer:
BM25 considers term frequency saturation and document length normalization, TF-IDF does not. -> Option CQuick Check:
BM25 adds saturation and length normalization [OK]
- Thinking TF-IDF is default in Elasticsearch
- Believing BM25 ignores IDF
- Assuming TF-IDF always scores higher
Solution
Step 1: Recall Elasticsearch similarity options
Elasticsearch uses "bm25" by default. To switch to TF-IDF, you use the "classic" similarity setting.Step 2: Match parameter to TF-IDF
The "classic" similarity corresponds to TF-IDF scoring, so setting "similarity": "classic" enables TF-IDF.Final Answer:
{ "similarity": "classic" } -> Option AQuick Check:
TF-IDF similarity is "classic" [OK]
- Using "tfidf" as similarity name (not valid)
- Assuming "default" switches to TF-IDF
- Confusing bm25 with TF-IDF
Solution
Step 1: Understand BM25 term frequency effect
BM25 applies a saturation function to term frequency, so each additional occurrence adds less to the score than the previous one.Step 2: Compare to linear increase
Unlike TF-IDF which can increase linearly, BM25 limits the impact of repeated terms to avoid overvaluing them.Final Answer:
The score increases but with diminishing returns due to term frequency saturation. -> Option AQuick Check:
BM25 uses saturation, not linear increase [OK]
- Assuming linear score increase with term frequency
- Thinking score stays constant with repeats
- Believing score decreases with more terms
Solution
Step 1: Understand similarity changes require reindexing
Changing similarity settings affects how documents are scored, but Elasticsearch applies these only after reindexing the data.Step 2: Identify common mistake
If you don't reindex, old scores remain, so results won't reflect the new TF-IDF similarity.Final Answer:
You forgot to reindex the data after changing similarity settings. -> Option DQuick Check:
Reindexing needed after similarity change [OK]
- Believing TF-IDF ignores IDF
- Thinking BM25 must be disabled cluster-wide
- Assuming query syntax changes for TF-IDF
Solution
Step 1: Understand BM25 parameters for short docs
BM25's b parameter controls length normalization. Lowering b reduces penalty on short documents, improving their scores.Step 2: Compare to TF-IDF and other options
TF-IDF lacks length normalization control. Increasing k1 ignores saturation, which can hurt relevance. Disabling IDF is incorrect.Final Answer:
Use BM25 with a lower b parameter to reduce length normalization effect. -> Option BQuick Check:
Lower b helps short docs in BM25 [OK]
- Assuming TF-IDF is better for short docs by default
- Increasing k1 to ignore saturation hurts relevance
- Disabling IDF reduces search quality
Ready to prove it?
Pass the test and this pattern moves unknown
