Lesson 3 of 3 Full-Text and Hybrid Search

Hybrid Search with Reciprocal Rank Fusion

This lesson explains why hybrid search fuses by rank, how reciprocal rank fusion works and how to implement it in T-SQL.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain why raw scores from different methods cannot be added.
  • Describe the reciprocal rank fusion formula.
  • Identify the T-SQL structure of a hybrid search query.

Why fuse by rank

Full-text RANK values range from 0 to 1,000 and grow with relevance, while cosine distances range from 0 to 2 and shrink with relevance. Their scales and distributions differ by query, so adding or averaging them is meaningless. Reciprocal rank fusion (RRF) ignores the raw scores and uses only each item's position in each list, which makes lists from any method comparable.

Scenario questions

Why not add the scores?

Exam skills 3.2.9

Choose the explanation for each fusion design choice.

Log in to try this activity Interactive activities are free with an account.

The RRF formula

For each item, RRF sums 1 / (k + rank) over the lists in which it appears, where rank starts at 1 and k is a constant, commonly 60. An item missing from a list contributes 0 for that list. Items are then ordered by the summed score, descending. The following table shows an example with k = 60.

ItemFull-text rankVector rankRRF score
Article 7141/61 + 1/64 = 0.0320
Article 3—11/61 = 0.0164
Article 92—1/62 = 0.0161
Article 5521/65 + 1/62 = 0.0315

Items found by both methods rise to the top. A smaller k gives top positions more influence, a larger k flattens differences, and multiplying each term by a weight favours one method when evaluation shows it is more reliable for the data.

The following diagram computes RRF scores for four articles.

Reciprocal rank fusion. For each item, RRF adds 1 divided by (k + rank) for every result list in which it appears, with k commonly 60; an item missing from a list adds nothing for that list. Items are then ordered by the sum, so items found by both full-text and vector search rise to the top. A join between the lists would lose items found by only one method; RRF keeps them.
Ranks are from the example above.
Fill in the value

Computing RRF

Exam skills 3.2.9

Compute RRF scores with k = 60, to four decimal places.

Log in to try this activity Interactive activities are free with an account.

Implementing hybrid search in T-SQL

The implementation ranks each method's top candidates in a CTE, combines them with a FULL OUTER JOIN so items found by only one method are kept, and computes the RRF score.

DECLARE @query nvarchar(400) = N'stove will not stay lit in wind';
DECLARE @q vector(1536) = AI_GENERATE_EMBEDDINGS(@query USE MODEL EmbeddingModel);

WITH Keyword AS (
    SELECT TOP (50) ft.[KEY] AS ArticleID,
           ROW_NUMBER() OVER (ORDER BY ft.[RANK] DESC) AS rnk
    FROM FREETEXTTABLE(Support.Article, (Title, Body), @query) AS ft
    ORDER BY ft.[RANK] DESC
), Semantic AS (
    SELECT TOP (50) e.ArticleID,
           ROW_NUMBER() OVER (ORDER BY VECTOR_DISTANCE('cosine', e.Embedding, @q)) AS rnk
    FROM Support.ArticleEmbedding AS e
    ORDER BY VECTOR_DISTANCE('cosine', e.Embedding, @q)
)
SELECT TOP (10) a.ArticleID, a.Title,
       COALESCE(1.0 / (60 + k.rnk), 0) + COALESCE(1.0 / (60 + s.rnk), 0) AS rrf_score
FROM Keyword AS k
FULL OUTER JOIN Semantic AS s ON s.ArticleID = k.ArticleID
JOIN Support.Article AS a ON a.ArticleID = COALESCE(k.ArticleID, s.ArticleID)
ORDER BY rrf_score DESC;

For large tables, the semantic CTE uses VECTOR_SEARCH with TOP (50) WITH APPROXIMATE in a subquery, and ROW_NUMBER is computed in an outer query, because window functions cannot be combined directly with WITH APPROXIMATE. Wrapping the logic in a stored procedure lets Data API builder, applications and RAG procedures reuse it.

Spot the bug

Hybrid search that drops results

Exam skills 3.2.8 · 3.2.9

Find the line that loses items found by only one method.

Log in to try this activity Interactive activities are free with an account.
Worked example

Items found by only one method

A developer implements hybrid search with an INNER JOIN between the keyword CTE and the vector CTE, then sums 1/(60 + rank). Articles that match only an error code, or only a paraphrase, never appear. What is wrong?

The INNER JOIN keeps only articles found by both methods. Use a FULL OUTER JOIN and COALESCE each term to 0, so articles found by one method still receive their single RRF term. Adding full-text RANK to cosine distance instead would mix incompatible scales, and increasing k does not bring back excluded rows.

Check yourself

With k = 60, what does an item ranked first in only one list score?

Show answer

1 / 61, about 0.0164.

Exam Tips

Remember for the exam

  • RRF: sum of 1 / (k + rank), k commonly 60, rank from 1.
  • Do not add raw full-text ranks to vector distances.
  • FULL OUTER JOIN plus COALESCE keeps items found by only one method.
  • Weights on each term favour the more reliable method.