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.

    tf-idf(t,D)=tf(t,D)×idf(t)

    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 nt 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.

    score(D,Q)=∑t∈Qidf(t)·tf(t,D)(k1+1)tf(t,D)+k1(1−b+b|D|avgdl)

    idf(t)=ln(1+N−nt+0.5nt+0.5)

    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 rankr(d) (first place is 1), add a constant k, and take the reciprocal. The sum is the score. Lists without d add nothing.

    RRF(d)=∑r1k+rankr(d)

    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.

    WordDocuments that contain itHow many
    tk-4811
    battery1, 4, 5, 64
    laptop7, 112
    the1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 1211

    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.

    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.

    1. It grows forever. A word that appears 10 times gets 10 times the share. Repeating a word is enough to climb the ranking.
    2. 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.

    tf (times the word appears)12510forever
    tf part in TF-IDF12510keeps growing
    tf part in BM25 (k1 = 1.2, average length)1.001.381.771.96gets 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.

    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.
    2. Weigh rare words: Σ idf × tf. N = 12. tk-48 has n = 1, so ln(1 + 11.5 ÷ 1.5) = ln(8.67) = 2.16. battery has n = 4, so ln(1 + 8.5 ÷ 4.5) = ln(2.89) = 1.06. Document 1 gets 2.16 + 1.06 = 3.22 and document 5 gets 2 × 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).
    3. Stop endless growth: Σ idf × tf × (k1 + 1) ÷ (tf + k1). With k1 = 1.2, tf 1 gives 1 × 2.2 ÷ 2.2 = 1 and tf 2 gives 2 × 2.2 ÷ 3.2 = 1.38. Document 5's battery share drops from 2.12 to 1.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.
    4. 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, so 1 − 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.

    DocumentWordsWord count
    Awireless earbuds wireless charging4
    Bwired earbuds2
    Cwireless mouse battery3

    The query is "wireless mouse".

    1. Average length. (4 + 2 + 3) ÷ 3 = 3.
    2. 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.
    3. 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, share 0.47 × 1.26 = 0.59. No "mouse". Score 0.59.
    4. B. Neither word is there. Score 0. With no shared words, the score is 0 whatever the length.
    5. 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. Score 0.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 matchesThe exact wordsMeaning (the direction of the vector)
    Good atProduct numbers, names, error codes, rare technical termsThe same idea in other words, queries written as a description
    Weak atSame 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 timeSplit words and build the inverted indexCompute and store an embedding for every document
    Score scaleFrom 0 up, no upper limit. Grows with more query wordsCosine similarity, from −1 to 1
    Why this order?You can split the score into word sharesHard 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".

    DocumentKeywordVectorRRF score
    11 PD-65 charger (right answer)#2#21 ÷ 62 + 1 ÷ 62 = 0.0323
    5 Battery replacement#1#51 ÷ 61 + 1 ÷ 65 = 0.0318
    6 LB-14 notebooknot there#11 ÷ 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.

    RRFWeighted sum
    UsesRanksScores
    You chooseJust k (usually 60)How to rescale, and α
    StrengthNo need to know the scales. Almost nothing to tuneKeeps a big score gap when there is one
    WeaknessA clear #1 in one list still drops if it is low in the otherScales 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.

    1. 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.
    2. 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 readsQuery and document separatelyQuery and document together
    Ahead of timeDocument vectors are storedNot possible
    Model runs per queryOnce (just the query)Once per candidate
    How good the order isUsually lowerUsually 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

    10. Key properties

    PropertyIn plain words
    BM25 = Σ share of each wordNo 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 ranksIt can merge scores on different scales, but big score gaps are thrown away
    Reranking only reorders the candidatesA document dropped in step 1 does not come back

    Where it is used

    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 hereWord in textbooks
    Keyword searchLexical search, sparse retrieval, full-text search
    Table from words to document numbersInverted index. The row for one word is a posting list
    How often the word appearsTerm frequency (tf)
    How rare the word isInverse document frequency (idf)
    Length factorDocument length normalization
    Repeats stop adding muchTerm frequency saturation
    BM25Okapi BM25. BM stands for best matching
    Vector searchDense retrieval, semantic search
    Combining two rankingsRank fusion, hybrid search
    Combining with ranks onlyRRF (Reciprocal Rank Fusion)
    Putting scores on the same scaleScore normalization
    Putting candidates in order againReranking
    Model that reads the query and document togetherCross-encoder
    Model that reads them separately and compares vectorsBi-encoder
    Splitting text into wordsTokenization. For Korean, morphological analysis
    Cutting words down to a common formStemming
    Rewriting the query, splitting it into smaller queriesQuery rewriting, query decomposition

    Sources

    ← Vector searchRetrieval metrics →