Concepts / K-Nearest Neighbors for Retrieval

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.

  • Programming

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?

  • The search failed to find any pages at all

  • The pages contain the word but may not match the meaning you wanted

  • The pages are ranked in reverse order

Reveal answer
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

Definition

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.

QueryCheap term-basedretrieverbroad set of candidatesCandidate setsmaller and more relevantPrecise rerankerreorders existingcandidatesRanked resultsqueryfetchespassed forwardreordered
What happens to the candidate set at each stage, and how does the second step reorder what the first step produced?

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.

Definition

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.

Worked example

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?

  1. 1
    Stage 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.

  2. 2
    Stage 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.

  3. 3
    Division 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.

Result

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.

QueryTerm-based retrieverranked list 1Fusion stepReciprocal rank fusionMerged rankingEmbedding-basedretrieverranked list 2same querysame queryrankingrankingone final list
How documents flow from multiple retrievers into one merged ranking, and where does the fusion step sit?

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.

Formula, step by step
  1. 1
    d is a document, and r is its position in one retriever's ranking, with 1 meaning first place
  2. 2
    A first place gives $\frac{1}{1} = 1$
  3. 3
    A second place gives $\frac{1}{2} = 0.5$
  4. 4
    The 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
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

Worked example

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.

  1. 1
    Score 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$.

  2. 2
    Score 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$.

  3. 3
    Add 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.

  4. 4
    Sort by total

    Sorting from highest total to lowest gives A, then B, then C as the merged ranking.

Result

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.

Retriever 1, rank 1Doc B, score 1Doc A total1.5Retriever 1, rank 2Doc A, score 0.5Doc B total1.33Retriever 1, rank 3Doc C, score 0.33Doc C total0.83Retriever 2, rank 1Doc A, score 1Retriever 2, rank 2Doc C, score 0.5Retriever 2, rank 3Doc B, score 0.3310.50.3310.50.33
Each document's position in each list yields a per-ranking score, and the scores for the same document are combined into its fused total.

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.

AspectTerm-based retrievalEmbedding-based retrieval
How a match is decidedThe document contains the query's wordThe document's content matches the meaning of the query
Example from the sourceA keyword search for transformer returns every page with that wordVector search pulls out context matching the question being asked
Weakness the source points outThe word can match while the intended meaning is missingNot stated in the source material
Role in a sequential chainCheap first step that gathers a broad set of candidatesMore 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 like

    Assuming the top document in a list gets 0 and lower documents get larger numbers

    Why it fails

    The score falls as rank increases, so first place is the highest score in that list.

    Do this instead

    First place gives 1, second gives 0.5, and third gives about 0.33.

  • Common mistake

    Comparing raw relevance scores across retrievers

    What it looks like

    Adding one retriever's raw relevance number directly to another retriever's raw number

    Why it fails

    RRF does not look at raw relevance scores. Each retriever's raw scores may be on a different scale.

    Do this instead

    Use 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 like

    Assuming the second step scans the whole collection again

    Why it fails

    Reranking reorders the candidates that the first step already produced.

    Do this instead

    Think 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 like

    Presenting the sum of per-ranking scores as the fixed rule of RRF

    Why it fails

    The 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 instead

    Describe the summing as one direct way of combining the scores, and say so when presenting it.

Scoring a New Pair of Rankings

MEDIUM

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.

Related concepts

More concepts starting with K