Keyword search (BM25), hybrid search and reranking
Some people search for "TK-48 battery", with a product number. Others type "earbuds whose sound never cuts out" and describe what they want. Search that matches words finds the first one well. Search that matches meaning finds the second one well. Here you compute both, and the ways to combine them, on 12 made-up shop pages.
Short answer. Keyword search finds documents that contain the words of the query. BM25 is the best-known formula for its score. Vector search finds documents with a similar meaning, even when the words are different. They are good at different things, so hybrid search combines the two rankings. Reranking is the step after that: it takes the few best candidates and puts them in order again with a more precise model.
Show the 12 documents
Each box is one document. The first number is the document number, the last number is its score in that ranking. Lines join the same document. ★ orange is the right answer for that query. A small toy model: the vector scores come from toy vectors built with a small word-to-topic table that a person wrote. Real vector search uses an embedding model that reads the whole text (section 5).
Try this. Click the three queries one by one. ① "TK-48 battery": keyword #1, vector #5, combined (RRF) #3. ② "earbuds whose sound never cuts out": not in the keyword list at all (no shared words), vector #1, RRF #3. ③ "charge laptop fast": keyword #2, vector #2, RRF #1. No single method puts all three right answers at #1. In the combined ranking, though, all three are in the top 3.
Definitions
The text below explains these with 12 made-up shop texts. These are the textbook definitions. Let Q be the query, D a document and t a word. The tf of a word is the number of times it appears in the document.
- inverted index
- A data structure that lists, for each word, the documents that contain it. It finds the documents with the query words without reading every document.
- TF-IDF (term frequency-inverse document frequency)
- The number of times a word appears in a document (term frequency, tf) multiplied by a weight that grows as fewer documents contain the word (inverse document frequency, idf). The document score is the sum of this value over the query words.
- BM25
- A keyword search score function that stops the score from growing without end as tf grows (k₁) and adjusts for document length |D| compared with the average length avgdl (b). N is the number of documents and is the number of documents that contain word t. There are several forms of idf; below is the form this page uses, which never goes negative.
- hybrid search
- Search that joins the results of keyword search (such as BM25) and vector search into one ranking. Ways to join them include RRF, which uses only ranks, and a weighted sum, which first puts each list's scores on the same scale (score normalization) and then adds them with weights.
- reciprocal rank fusion (RRF)
- A way to join several ranked lists. For each list r in which document d appears, take its rank (first place is 1), add a constant k, and take the reciprocal. The sum is the score. Lists without d add nothing.
- reranking
- A step that takes only the few top candidates from the first search and orders them again with a more exact, slower model. A cross-encoder, a common reranking model, joins the query and the document, puts them into one model together, and gives one relevance score.
1. Keyword search and the inverted index
Keyword search splits the query into words, finds the documents that contain those words, and scores them. With many documents you cannot read them one by one. So you build an inverted index ahead of time. It works like the index at the back of a book: for each word, a list of the documents that contain it. Here are a few rows of the inverted index for this page's 12 documents.
| Word | Documents that contain it | How many |
|---|---|---|
tk-48 | 1 | 1 |
battery | 1, 4, 5, 6 | 4 |
laptop | 7, 11 | 2 |
the | 1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12 | 11 |
To search for "TK-48 battery", you open only two rows: tk-48 and battery. Only the documents in those rows (1, 4, 5, 6) get a score. The other 8 are never looked at. Even with a million documents, you read only the rows for the query words, so it is fast. To add a new document, you append its number to the row of each of its words.
The length of a row (how many documents) is useful too. A short row means a rare word, and a rare word that matches is a strong clue. That is the idf in the next section.
2. From TF-IDF to BM25
There are two simple ideas for scoring a document.
- How often does the word appear (tf)? If a query word appears many times in a document, the document is probably about it.
- Is the word rare (idf)?
tk-48is in only one document, so a match almost names the document.theis in 11 documents, so a match tells you almost nothing.
Multiply the two and you get TF-IDF: share of a word = tf × idf, and the document score is the sum of the shares of the query words. It has two problems.
- It grows forever. A word that appears 10 times gets 10 times the share. Repeating a word is enough to climb the ranking.
- Long documents win. More words means more chances to match a query word by accident.
BM25 is a formula that fixes both.
score = Σ (for each query word) idf × tf × (k1 + 1) ÷ (tf + k1 × length factor)
length factor = 1 − b + b × (words in the document ÷ average words per document)
idf = ln(1 + (N − n + 0.5) ÷ (n + 0.5)) N = number of documents, n = documents that contain the word
To see this formula built from TF-IDF one piece at a time, go to 3. How the formula is built.
Σ: compute it for each query word and add them all up.tf: how many times the word appears in the document.idf: a weight that is larger for rarer words.ln: the natural logarithm.- k1: how much repeats count. In a document of average length, the tf part
tf × (k1 + 1) ÷ (tf + k1)can never go abovek1 + 1, however large tf gets. A smaller k1 makes it level off sooner. With k1 = 0, it only checks whether the word is there or not. - b: how much length counts. For a document of average length, the length factor is 1. A longer document gets a factor above 1, so its shares shrink. A shorter one gets a factor below 1, so its shares grow. With b = 0, length is ignored. Common default values are k1 = 1.2 and b = 0.75.
- The form of idf. The original form
ln((N − n + 0.5) ÷ (n + 0.5))turns negative for a word that appears in more than half of the documents. This page adds 1 inside the log, so it never goes negative. A word that appears in every document still gets a small positive idf.
| tf (times the word appears) | 1 | 2 | 5 | 10 | forever |
|---|---|---|---|---|---|
| tf part in TF-IDF | 1 | 2 | 5 | 10 | keeps growing |
| tf part in BM25 (k1 = 1.2, average length) | 1.00 | 1.38 | 1.77 | 1.96 | gets close to 2.2 |
Each row is one document. The number in a word's column is that word's share (idf × tf part), and tf is how many times the word appears in the document. A dot means the word is not there. The idf in the column header is the same for every document. The length factor is one number per document. Text is lowercased and split on spaces and punctuation, with no other changes (section 8).
Try this. ① With "TK-48 battery", document 1 gets 2.02 from tk-48 and 0.99 from battery, 3.02 in total (each number is rounded). tk-48 is in only 1 document, so its idf is 2.16. battery is in 4, so its idf is 1.06. ② Document 5 says battery twice and gets 1.45. Document 4 says it once and gets 1.12. Twice is not double. Set k1 to 0 and both become 1.06. Raise k1 to 3 and they spread out to 1.68 and 1.14. ③ Document 1 has 24 words, more than the average of 20.58, so its length factor is 1.12. Set b to 0 and nothing is cut: it gets 3.22 (= 2.16 + 1.06). ④ Type "the speaker". the is in 11 documents, so its idf is only 0.12, while speaker gets 2.16. The other documents that contain "the" get only 0.11 to 0.19 from it. Common words spread tiny points everywhere and hardly change the order.
3. How the formula is built: BM25
Here we build the BM25 formula one piece at a time, starting from simply counting words. The data is the same as the first screen of the experiment above: the query "TK-48 battery", 12 documents, k1 = 1.2, b = 0.75. Only documents 1, 4, 5 and 6 contain at least one of the two words. The other 8 score 0 at every step.
Click "Next" to go one step at a time. A piece is added to the formula at the top (the orange background marks the new piece), the bars grow or shrink by that much, and the reason appears below. The dark green part is the share of tk-48, the light green part is the share of battery, and the number on the right is the total score. The orange dashed frame is the bar length at the previous step, and ★ marks the right answer, document 1.
- Count the words:
score = Σ tf. Σ means "add up over the query words". Document 1 has tk-48 once and battery once: 2. Document 5 has battery twice: 2. Documents 4 and 6 have battery once: 1. Why: a document with more of the query words is probably more about the query. But documents 1 and 5 tie for #1. A count cannot tell a match on the product number from a common word written twice. - Weigh rare words:
Σ idf × tf. N = 12. tk-48 has n = 1, soln(1 + 11.5 ÷ 1.5) = ln(8.67) = 2.16. battery has n = 4, soln(1 + 8.5 ÷ 4.5) = ln(2.89) = 1.06. Document 1 gets2.16 + 1.06 = 3.22and document 5 gets2 × 1.06 = 2.12, so document 1 is #1 on its own. Up to here, this is TF-IDF. Why: a word found in only one document almost names that document, while a word found in many is a weak clue. The only property we use is "the smaller n, the larger idf". How this ln form comes out of the probability model of search is not proved here (see Robertson and Zaragoza in the sources). - Stop endless growth:
Σ idf × tf × (k1 + 1) ÷ (tf + k1). With k1 = 1.2, tf 1 gives1 × 2.2 ÷ 2.2 = 1and tf 2 gives2 × 2.2 ÷ 3.2 = 1.38. Document 5's battery share drops from 2.12 to1.06 × 1.38 = 1.46. Documents 1, 4 and 6 have tf 1 and stay the same. Why: repeating a word should not be enough to climb the ranking. This form grows more and more slowly as tf grows, and never goes above k1 + 1 = 2.2 (the tf part table in section 2). The (k1 + 1) on top makes tf = 1 give exactly 1. It multiplies every share by the same number, so it does not change the order. How this form comes out of the probability model is not covered here. - Cut long documents:
Σ idf × tf × (k1 + 1) ÷ (tf + k1 × length factor).length factor = 1 − b + b × (words ÷ average words). Document 1 has 24 words, more than the average of 20.58, so1 − 0.75 + 0.75 × 24 ÷ 20.58 = 1.12. Its shares become 2.02 (tk-48) and 0.99 (battery), and its score drops from 3.22 to 3.02. Document 5 has 21 words, factor 1.02: 1.46 → 1.45. Document 4 has 18 words, factor 0.91: 1.06 → 1.12. Document 6 has 20 words, factor 0.98: 1.06 → 1.07. This is BM25. Why: a long document has more words, so more chances to match a query word by accident. A factor above 1 makes the bottom of the fraction bigger and the share smaller. A factor below 1 does the opposite. The limit k1 + 1 stays the same. With b = 0 the factor is always 1, and you are back at step 3.
4. Work it out by hand: three documents, two words
Let us compute BM25 all the way for three tiny documents, with k1 = 1.2 and b = 0.75.
| Document | Words | Word count |
|---|---|---|
| A | wireless earbuds wireless charging | 4 |
| B | wired earbuds | 2 |
| C | wireless mouse battery | 3 |
The query is "wireless mouse".
- Average length.
(4 + 2 + 3) ÷ 3 = 3. - idf. "wireless" is in two documents, A and C:
ln(1 + (3 − 2 + 0.5) ÷ (2 + 0.5)) = ln(1.6) = 0.47. "mouse" is only in C:ln(1 + (3 − 1 + 0.5) ÷ (1 + 0.5)) = ln(2.67) = 0.98. The rarer word "mouse" weighs about twice as much. - A. Length factor
1 − 0.75 + 0.75 × 4 ÷ 3 = 1.25(longer than average). "wireless" twice:2 × 2.2 ÷ (2 + 1.2 × 1.25) = 4.4 ÷ 3.5 = 1.26, share0.47 × 1.26 = 0.59. No "mouse". Score 0.59. - B. Neither word is there. Score 0. With no shared words, the score is 0 whatever the length.
- C. Average length, so the length factor is 1. "wireless" once:
1 × 2.2 ÷ (1 + 1.2) = 1, share 0.47. "mouse" once: share 0.98. Score0.47 + 0.98 =1.45.
The order is C (1.45), A (0.59), B (0). A has "wireless" twice, but it gets only 0.59, a little more than the 0.47 that C gets for one "wireless". The second time does not count in full (saturation), and A loses a bit more for being long (length factor).
5. Keywords and vectors are good at different things
Vector search turns each document and the query into a list of numbers (a vector) and finds the ones pointing in a similar direction (high cosine similarity). The model that turns text into vectors is an embedding model. See Vector search and Embeddings for the details.
The vectors on this page are a small toy model. A table written by a person gives each word one topic: earbuds, headset, sound and music are "sound"; wireless, bluetooth, link and dropouts are "connection"; and so on, 8 topics in all. The vector of a text has 8 slots that count its words per topic. A real embedding model reads the whole text and makes a vector with far more slots. It also understands words the table does not list, and the order of words.
The vector for "earbuds whose sound never cuts out" is sound 2 · connection 1. Document 3, the AX-9 headset, is sound 2 · connection 3. They share no words at all, yet document 3 is the closest, with a cosine of 0.87. The other way round, in "TK-48 battery" the table does not know tk-48, so the vector cannot see it. The query vector is just power 1, so document 4, with the most power words (power, charge, battery), comes first, and the right answer, document 1, falls to #5.
| Keyword search (BM25) | Vector search | |
|---|---|---|
| What it matches | The exact words | Meaning (the direction of the vector) |
| Good at | Product numbers, names, error codes, rare technical terms | The same idea in other words, queries written as a description |
| Weak at | Same meaning, different words ("earbuds" vs "headset", "cuts out" vs "dropouts") | Telling apart numbers one character apart (TK-48 vs TK-84), new words the model never learned |
| Work done ahead of time | Split words and build the inverted index | Compute and store an embedding for every document |
| Score scale | From 0 up, no upper limit. Grows with more query words | Cosine similarity, from −1 to 1 |
| Why this order? | You can split the score into word shares | Hard to explain |
6. Combining two rankings: RRF and weighted sum
To merge two rankings, it is tempting to add the scores. But the scales are different. In this experiment the top BM25 score is around 2 to 3 points, and it gets bigger with more query words. Cosine stays between 0 and 1. Add them as they are, and BM25, with the bigger scale, decides almost everything. Even BM25 alone has a different scale for each query, so there is no rule like "2 points is high". There are two common ways around this.
RRF: use only the ranks
RRF score = Σ (for each list the document is in) 1 ÷ (k + rank)
It throws the scores away and looks only at the place in each list. If a document is missing from a list, it gets nothing from that list. People usually use k = 60, the value from the paper that introduced RRF. Here it is for "charge laptop fast".
| Document | Keyword | Vector | RRF score |
|---|---|---|---|
| 11 PD-65 charger (right answer) | #2 | #2 | 1 ÷ 62 + 1 ÷ 62 = 0.0323 |
| 5 Battery replacement | #1 | #5 | 1 ÷ 61 + 1 ÷ 65 = 0.0318 |
| 6 LB-14 notebook | not there | #1 | 1 ÷ 61 = 0.0164 |
Document 11, high in both lists, beats the documents that are #1 in only one list. You can also see what k does. With k = 60, the share for #1, 1 ÷ 61 = 0.0164, and the share for #2, 1 ÷ 62 = 0.0161, are almost the same. With k = 1, they are 1 ÷ 2 = 0.5 and 1 ÷ 3 = 0.33, far apart. The larger k is, the more a document that does well in several lists beats one that tops a single list.
Weighted sum: put the scores on a 0 to 1 scale, then add
Bring each list's scores to the same scale, then add them with weights. This experiment divides each list by its top score (so #1 becomes 1). Then total = (1 − α) × keyword + α × vector. With α = 0 you see only keywords, with α = 1 only vectors. There are other ways to rescale too (for example, also moving the lowest score to 0).
Combine "TK-48 battery" with α = 0.5 and document 1 gets 0.5 × 1 + 0.5 × (0.55 ÷ 0.95) = 0.79, which is #1. With RRF it was #3. RRF does not know that document 1's keyword score (3.02) was far above #2 (1.45), because it looks only at ranks.
| RRF | Weighted sum | |
|---|---|---|
| Uses | Ranks | Scores |
| You choose | Just k (usually 60) | How to rescale, and α |
| Strength | No need to know the scales. Almost nothing to tune | Keeps a big score gap when there is one |
| Weakness | A clear #1 in one list still drops if it is low in the other | Scales change from query to query, so α is hard to tune |
Try this. In the experiment at the top, click "Weighted sum". ① "TK-48 battery" is #1 at α 0.5 and #3 at α 0.8. ② "earbuds whose sound never cuts out" is #3 at α 0.5, #2 at 0.8 and #1 at 1. ③ "charge laptop fast" is #2 at α 0 (keywords only) and also #2 at α 1 (vectors only), but #1 at 0.5.
7. Reranking: pick wide, then look closely
Even when the combined ranking puts the right answer near the top, it is not always #1. Reranking takes the top few (5 in this experiment) and puts them in order again with a more precise model. Search becomes two steps.
- Pick (fast). Keyword, vector or hybrid search. Thanks to the inverted index and vectors computed ahead of time, it stays fast with many documents. It is less precise.
- Look again (slow). A reranking model. It is more precise but slow, so it only sees the candidates that step 1 passed on.
The reranking model is often a cross-encoder. You join the query and a document into one input, feed them to the model together, and get one score for "how well does this match". Inside the model, the words of the query and the words of the document look at each other as they are read. So it can tell better that "never cuts out" and "without dropouts" mean the same thing, or that TK-48 and TK-84 are different products.
That is also why it is slow. Vector search (a bi-encoder) computes the document vectors ahead of time, and when a query comes in, it only computes the query and compares. A cross-encoder needs the query and the document together to give a score, so nothing can be computed before the query arrives. With 100 candidates, it runs the model 100 times. Using it on every document would mean reading every document again for every query.
| Vector search (bi-encoder) | Reranking (cross-encoder) | |
|---|---|---|
| How it reads | Query and document separately | Query and document together |
| Ahead of time | Document vectors are stored | Not possible |
| Model runs per query | Once (just the query) | Once per candidate |
| How good the order is | Usually lower | Usually higher |
A small toy model: there is no real reranking model here. Instead, a person gave each document a relevance grade (0 to 3) for each query ahead of time, and that grade stands in for the model's score. When grades tie, the order from step 1 is kept.
Try this. ① On the first screen ("TK-48 battery", top 5 by RRF), the right answer, document 1, is #3 among the candidates and #1 after reranking. ② Pick "earbuds whose sound never cuts out" and click "Top 5 by keyword". Keyword search found only 2 documents, and the right answer, document 3, is not one of them. It is still missing after reranking. Switch to "Top 5 by RRF" and the right answer goes from #3 to #1. ③ With "TK-48 battery", click "Top 5 by vector". The right answer only just makes the list at #5, and reranking moves it to #1. Had step 1 passed on only 4 candidates, it would have been lost.
Reranking only reorders the candidates it receives. A document dropped in step 1 never gets a chance in step 2. So step 1 casts a wide net, so that the right answer is somewhere in the list, and step 2 fixes the order. How to measure "is it in the list?" and "what rank is it?" with numbers is in Retrieval metrics.
8. Splitting words: in Korean, this decides the quality
Keyword search only finds a document when the words are the same. In English, splitting on spaces mostly gives you words, but "charge", "charges" and "charger" are still three different words. This page's tokenizer does not change words at all, which is why "charge" in the query does not match "charger" in document 11. Real systems often cut words down to a common stem first. In Korean the problem is much bigger. Small grammar words stick to the end of a word (like "battery-SUBJECT", "battery-TOPIC", "battery-OBJECT"), and verb endings change, so splitting on spaces alone turns one word into many forms that cannot find each other. Korean search uses a morphological analyzer to cut each word into meaningful pieces and puts only the meaningful pieces in the inverted index. Another way that needs no analyzer is to cut text into overlapping pieces of two or three characters (n-grams). How you split words largely decides the quality of keyword search in Korean, and the query and the documents must be split the same way.
9. Fixing the query side
- Query rewriting. Rewrite the query in words the documents are likely to use, then search (for example, "never cuts out" becomes "steady connection").
- Query decomposition. Split a query that asks several things at once into small queries, search for each, and merge the results.
10. Key properties
| Property | In plain words |
|---|---|
BM25 = Σ share of each word | No shared words means 0. You can split the score word by word to see why |
| Rare words add a lot (idf) | In this experiment tk-48 gets 2.16, and the, which is in 11 documents, gets 0.12 |
| A share stops growing (k1), and long documents are cut (b) | A word 10 times is not 10 times one, but about 2 times (k1 = 1.2) |
| RRF uses only ranks | It can merge scores on different scales, but big score gaps are thrown away |
| Reranking only reorders the candidates | A document dropped in step 1 does not come back |
Where it is used
- Shop search and company document search. Some people type product numbers, others describe what they want. Use only one kind of search and one group of people cannot find things.
- RAG. Picking the text to hand to an LLM. The LLM cannot see what search missed, so search quality sets the quality of the answer. Continue in RAG.
- Error code and code search. Error numbers and function names need the exact characters, which keyword search is good at.
- Two-step search. Many search systems pick candidates with a fast method and then reorder them with a precise model.
Frequently asked questions
What is hybrid search?
It runs keyword search and vector search together and merges the two rankings into one. Keyword search is good when the exact words matter, like a product number. Vector search is good when the same idea is written in different words. Each one covers the other's gaps.
How is BM25 different from TF-IDF?
Both give a high score to a document where a rare word shows up often. BM25 stops the score from growing forever when a word repeats (k1), and it corrects for length so long documents do not win just because they have more words (b).
Why is k 60 in RRF?
It is the value used in the paper that introduced RRF. A small k makes the gap between first and second place large. A large k gives documents a little further down a fairer share. You can measure on your own data and change it.
Can I use only vector search?
You can, but it easily misses queries where the exact characters matter, like product numbers, names and error codes. In this page's experiment, the right answer for "TK-48 battery" is #1 in the keyword ranking and #5 in the vector ranking.
Why not use the reranking model from the start?
It needs to read the query and the document together to give a score, so nothing can be computed ahead of time for each document. Reading every document for every query is far too slow when there are many documents. So it is used only on a short list picked by a fast method.
Words
| Word used here | Word in textbooks |
|---|---|
| Keyword search | Lexical search, sparse retrieval, full-text search |
| Table from words to document numbers | Inverted index. The row for one word is a posting list |
| How often the word appears | Term frequency (tf) |
| How rare the word is | Inverse document frequency (idf) |
| Length factor | Document length normalization |
| Repeats stop adding much | Term frequency saturation |
| BM25 | Okapi BM25. BM stands for best matching |
| Vector search | Dense retrieval, semantic search |
| Combining two rankings | Rank fusion, hybrid search |
| Combining with ranks only | RRF (Reciprocal Rank Fusion) |
| Putting scores on the same scale | Score normalization |
| Putting candidates in order again | Reranking |
| Model that reads the query and document together | Cross-encoder |
| Model that reads them separately and compares vectors | Bi-encoder |
| Splitting text into words | Tokenization. For Korean, morphological analysis |
| Cutting words down to a common form | Stemming |
| Rewriting the query, splitting it into smaller queries | Query rewriting, query decomposition |
Sources
- Robertson, Zaragoza (2009). The Probabilistic Relevance Framework: BM25 and Beyond. The BM25 formula and the meaning of k1 and b.
- Cormack, Clarke, Büttcher (2009). Reciprocal Rank Fusion outperforms Condorcet and individual Rank Learning Methods. The RRF formula and k = 60.
- Nogueira, Cho (2019). Passage Re-ranking with BERT. Reordering candidates with a model that reads the query and the passage together.