K-Nearest Neighbors for Retrieval
Hybrid search combines term-based retrieval with embedding-based retrieval, and the two can be arranged in sequence or in parallel as an ensemble.
Why One Retrieval Method Falls Short
Imagine you search a large document collection for the word transformer. A keyword search will happily return every page containing that word, but those pages may be about an electrical device, a neural network design, or a film. The word matched, yet the meaning you wanted may be missing. A search system that relies on only one retrieval method inherits that method's weakness.
What do you think happens?
A keyword search for transformer returns fifty pages that all contain the word. What is the main problem with that result list?
Reveal answer
The pages contain the word but may not match the meaning you wanted
A keyword match confirms that the word appears on the page, not that the page is about the sense of the word you intended. The word matched, yet the intended meaning may be missing.
The practical response is to combine retrieval methods so that each one covers for the other's blind spots. The source states the motivation directly: each retrieval algorithm has its own advantages, so a production retrieval system typically combines several approaches rather than committing to one. The rest of this article follows the two main ways of combining them.
What Hybrid Search Means
Hybrid search combines term-based retrieval with embedding-based retrieval. The two methods can be arranged in sequence, where one feeds the next, or in parallel as an ensemble, where several run on the same query and their outputs are merged.
The phrase hybrid search describes the combination itself, not a single algorithm. Once that is clear, the real design question becomes how the methods are arranged. Term-based retrieval finds documents by the words they contain. Embedding-based retrieval, which the source also calls vector search, pulls out content that matches the meaning of the query. Each approach can be placed in a chain or run side by side.
Tracing Candidates Through a Two-Stage Chain
In a sequential arrangement, the first retriever is chosen for being cheap and reasonably good at finding a broad set of candidates, even if it is not very precise. The source gives a term-based system as the example of this first step. The second retriever is chosen for being more precise, even though it costs more to run. It only needs to look at the candidates the first step produced, and that is what makes its expense affordable.
Reading the pipeline from left to right shows why the arrangement is efficient. Each stage hands a smaller and more relevant set forward, so the expensive step never has to examine the entire collection. The second step has a specific name: reranking. Reranking does not search from scratch. It reorders the candidates that already exist so that the ones actually related to the query rise to the top.
Reranking is the second, more precise and more expensive step in a sequential retrieval chain. It reorders the candidates produced by a cheaper first retriever rather than searching the whole collection again.
A Keyword Filter Followed by Meaning Search
The source offers a second chain with a different shape. Suppose the query is: Who is responsible for the most sales to X? The first step uses the keyword X to fetch all documents associated with that customer. The second step then uses vector search to pull out the context matching the question about who is responsible for the most sales.
Narrowing by identifier, then matching the question
A company collection contains documents for many customers. The query asks who is responsible for the most sales to customer X. How does the chain narrow the collection?
- 1Stage one: exact identifier
The keyword X is used to fetch every document associated with that customer. The collection shrinks to documents tied to one exact identifier.
- 2Stage two: open-ended part of the question
Vector search runs over the reduced set and pulls out the context that matches the question about who is responsible for the most sales.
- 3Division of labor
The keyword step handles the precise part of the query, and the vector step handles the part that is phrased in open-ended language.
The keyword step narrows the collection by an exact identifier, and the vector step handles the open-ended part of the question.
Retrievers Running Side by Side
The other arrangement does not chain the algorithms. Instead, several retrievers receive the same query at the same time, and each produces its own ranking of documents. The source describes this as using the algorithms in parallel as an ensemble. A retriever works by ranking documents according to their relevance scores for the query, so each retriever's output is a ranked list. The remaining task is to combine those lists into one final ranking.
The diagram makes one point clear: the two retrievers never see each other's output while they work. They are independent, which is why they can run together. The merging happens afterward, and that merging is the job of a combination method. The source names one such method, reciprocal rank fusion.
Scoring Documents by Position
Reciprocal rank fusion, abbreviated RRF, is the algorithm the source gives for combining different rankings. It does not look at the raw relevance scores each retriever produced. It looks only at where each document landed in each list. Each document receives a score based on its rank in a given retriever's list, and the higher the document ranks, the higher that score is.
- 1d is a document, and r is its position in one retriever's ranking, with 1 meaning first place
- 2A first place gives $\frac{1}{1} = 1$
- 3A second place gives $\frac{1}{2} = 0.5$
- 4The score falls as the rank number grows, so a third place gives $\frac{1}{3} \approx 0.33$
Because the rule depends only on position, a retriever whose raw scores are on a different scale from another retriever's can still be combined with it. There is no need to convert the scales to a common unit first. A document's score from one list is simply determined by where it sits in that list.
What do you think happens?
What per-ranking score does a document receive if it lands in third place in one retriever's list?
Reveal answer
$\frac{1}{3}$, about 0.33
The rule uses the reciprocal of the rank, so third place gives one third.
Fusing Two Rankings Step by Step
Combining a term-based ranking with an embedding ranking
Retriever 1 ranks the documents as B, A, C. Retriever 2 ranks them as A, C, B. Compute the RRF score of each document and order them for the merged ranking.
- 1Score each document in Retriever 1
B is first, so it gets 1. A is second, so it gets 0.5. C is third, so it gets $\frac{1}{3} \approx 0.33$.
- 2Score each document in Retriever 2
A is first, so it gets 1. C is second, so it gets 0.5. B is third, so it gets $\frac{1}{3} \approx 0.33$.
- 3Add each document's per-ranking scores
A totals $0.5 + 1 = 1.5$. B totals $1 + \frac{1}{3} \approx 1.33$. C totals $\frac{1}{3} + 0.5 \approx 0.83$. This adding step is an illustration of how the pieces fit together, since the source itself does not spell out this final combining step.
- 4Sort by total
Sorting from highest total to lowest gives A, then B, then C as the merged ranking.
The merged ranking is A, B, C. Document A ranks first because it sits near the top of both lists, even though neither list places it first in both.
Term-Based Versus Embedding-Based Matching
The two retrieval approaches decide that a document matches a query in different ways, and that difference is the reason hybrid search exists. The table below contrasts them using only the behavior the source describes.
| Aspect | Term-based retrieval | Embedding-based retrieval |
|---|---|---|
| How a match is decided | The document contains the query's word | The document's content matches the meaning of the query |
| Example from the source | A keyword search for transformer returns every page with that word | Vector search pulls out context matching the question being asked |
| Weakness the source points out | The word can match while the intended meaning is missing | Not stated in the source material |
| Role in a sequential chain | Cheap first step that gathers a broad set of candidates | More precise step that works on the candidates |
Term-based and embedding-based retrieval as described in the source
Mistakes When Reading Fusion Scores
Common mistake Giving first place a score of zero
What it looks likeAssuming the top document in a list gets 0 and lower documents get larger numbers
Why it failsThe score falls as rank increases, so first place is the highest score in that list.
Do this insteadFirst place gives 1, second gives 0.5, and third gives about 0.33.
Common mistake Comparing raw relevance scores across retrievers
What it looks likeAdding one retriever's raw relevance number directly to another retriever's raw number
Why it failsRRF does not look at raw relevance scores. Each retriever's raw scores may be on a different scale.
Do this insteadUse each document's position in each list, and compute the per-ranking score from that position.
Common mistake Treating reranking as a brand new search
What it looks likeAssuming the second step scans the whole collection again
Why it failsReranking reorders the candidates that the first step already produced.
Do this insteadThink of the second stage as working only on the smaller candidate set passed forward.
Common mistake Believing the source specifies the final adding step
What it looks likePresenting the sum of per-ranking scores as the fixed rule of RRF
Why it failsThe source gives the per-ranking score and notes that the summing is an illustration. It does not spell out the final combining step.
Do this insteadDescribe the summing as one direct way of combining the scores, and say so when presenting it.
Scoring a New Pair of Rankings
Retriever X ranks the documents as P, Q, R, S. Retriever Y ranks them as R, P, S, Q. Compute the per-ranking RRF score for each document in each list, then add the two scores for each document and state the merged order.
Hints
- Use the rule that a document in position r receives 1 divided by r.
- Position 4 gives one quarter, which is 0.25.
- Document P appears in position 1 in list X and position 2 in list Y.
Key Takeaways
- Hybrid search combines term-based retrieval with embedding-based retrieval, and the two can be arranged in sequence or in parallel as an ensemble.
- In a sequential chain, a cheap first retriever gathers a broad set of candidates and a more precise, more expensive reranker reorders them.
- In a parallel ensemble, several retrievers rank the same query independently, and their rankings must be merged into one final ranking.
- Reciprocal rank fusion scores each document by its position in each list, so first place gives 1, second place gives 0.5, and the score falls as rank increases.
- The source gives the per-ranking RRF score but does not specify the final combining step, so adding per-ranking scores should be presented as an illustrative method.