# Hybrid search: BM25 keywords plus vectors, fused by rank

Vectors capture meaning, keywords capture exact words. Hybrid search runs both and fuses the rankings, and is usually the cheapest large gain after the baseline.

<div data-sim="ragx" data-v="hybrid"><p>Interactive simulation. It needs JavaScript; the text below explains what it shows.</p></div>

## What you are looking at

Step 1 is vector search, step 2 is BM25 with its rare-word weighting, step 3 is reciprocal rank fusion: every list contributes 1 / (60 + rank). The table shows each chunk's rank in both lists and its fused score. All RAG simulations share one knowledge base of 18 chunks from 11 documents of a fictional outdoor-gear shop, and three questions: one phrased differently from the documents, one that needs two documents, and one about the whole corpus.

## Things to try

- Compare the two rankings for the fabric question: BM25 finds 'Ridge 2' exactly.
- Look for chunks that rank well in only one list and see where fusion puts them.
- Compare the context recall with the naive RAG page.

## How it works in real systems

Elasticsearch, OpenSearch, Weaviate, Qdrant and pgvector with full-text search all support hybrid retrieval. Reciprocal rank fusion with k = 60 is the common default because it needs no calibration.

## Code lab

The same idea in real code, step by step. Copy each block into a notebook or a file and run it in order.

<div class="cl-start"></div>

### Step 1 · Index the knowledge base

Install the libraries and index a small knowledge base with a sentence-embedding model. Normalised embeddings make the dot product equal to cosine similarity.

```bash
pip install sentence-transformers rank-bm25 numpy openai
```

```python
import numpy as np
from sentence_transformers import SentenceTransformer
chunks = [
    "Most items can be returned within 30 days of delivery for a full refund.",
    "Damaged or defective items qualify for a full refund or a free replacement; send a photo of the damage to support first.",
    "Refunds are issued within five business days after the returned item reaches our warehouse.",
    "Parcels damaged in transit must be reported to the carrier within 48 hours of delivery.",
    "The Ridge 2 tent weighs 1.9 kg and uses Aurora ripstop fabric for its fly sheet.",
    "Aurora ripstop fabric is woven by Lumen Textiles at its mill in Porto, Portugal.",
    "Customer complaints in Q3 were mostly about late deliveries during the September peak.",
]
emb = SentenceTransformer("all-MiniLM-L6-v2")                 # 384 dimensions, small and fast
E = emb.encode(chunks, normalize_embeddings=True)
def dense_search(q, k=3):
    s = E @ emb.encode([q], normalize_embeddings=True)[0]
    top = np.argsort(-s)[:k]
    return [(int(i), float(s[i])) for i in top]
```

### Step 2 · Add BM25 keyword search

```python
from rank_bm25 import BM25Okapi
import re
tok = lambda s: re.findall(r"[a-z0-9]+", s.lower())
bm25 = BM25Okapi([tok(c) for c in chunks])
def bm25_search(q, k=5):
    s = bm25.get_scores(tok(q)); return [int(i) for i in np.argsort(-s)[:k] if s[i] > 0]
print(bm25_search("Ridge 2 fabric"))     # exact names are where keywords shine
```

### Step 3 · Fuse with reciprocal rank fusion

Scores from BM25 and cosine are on different scales; RRF ignores the scores and uses only ranks.

```python
def rrf(lists, k=60):
    score = {}
    for l in lists:
        for r, i in enumerate(l): score[i] = score.get(i, 0) + 1 / (k + r + 1)
    return sorted(score, key=score.get, reverse=True)
def hybrid(q, k=3):
    return rrf([[i for i, _ in dense_search(q, 10)], bm25_search(q, 10)])[:k]
print(hybrid("Who makes the fabric used in the Ridge 2 tent?"))
```

### Step 4 · In a vector database

Most vector databases do this server-side. In Postgres, combine a full-text query and a vector query and fuse in SQL or in Python; in Qdrant and Weaviate, hybrid queries are a single API call. Keep k = 60 unless your evaluation says otherwise.

<div class="cl-end"></div>

## More simulations

[← Naive RAG](/ai-simulations/rag-naive) · [All AI Simulations](/ai-simulations) · [Rerank →](/ai-simulations/rag-rerank)
