On paper, building RAG (Retrieval-Augmented Generation) sounds trivial. Cut documents into 500-token chunks, turn each chunk into an embedding with whatever model is trending, then store them in a vector database. Done, just wire it to the LLM. When a question comes in, fetch the chunk with the highest cosine similarity score.
That plan holds until the system meets real user queries. Once it hits production and gets tested against thousands of real queries, the illusion that "RAG is easy" collapses:
- A user searches for a specific error code (
ERR_CONN_REFUSED_504), and gets back a general explainer on internet networking. - A user searches for specific hardware (
MacBook Pro M3 Max), and gets handed chunks aboutMacBook Air M2, because "the meaning is close, right?".
Those two examples look trivial, but the symptom is the same. The system returns what feels relevant, not what you asked for. How? In this article we take apart why dense vector search misleads, when pure semantic search fails completely, and why hybrid search plus a cross-encoder re-ranker is now mandatory in production. 🔍
Key takeaways
- Pure cosine similarity has a blind spot for exact word matches: error codes, serial numbers, and rare variable names carry almost no semantic weight, so queries like that lose to long generic paragraphs.
- Hybrid search runs two searchers at once, dense vector for meaning and BM25 for exact words, then merges the results.
- Reciprocal Rank Fusion (RRF) merges two ranked lists without caring about score scales, because it only counts positions.
- Two-stage retrieval: the first stage pulls 50-100 fast candidates, the second filters down to 3-5 chunks with a cross-encoder that scores query and document together.
- If your agent answers nonsense while the data is right there, the first suspect is the retrieval stage, not the LLM.
Where dense vector search loses the trail
Dense embeddings map meaning into a high-dimensional vector space, usually 768 to 1,536 dimensions. Their strength is exactly what makes classic search rigid: synonyms and implied intent. The query "how do I return an item" sits very close in meaning to the document line "guide for submitting a buyer transaction refund". Yet the two share almost no words at all.
That strength turns into a weakness once the data is full of domain-specific terms.
1. Blind to exact word matches
Embeddings tend to flatten meaning. Serial numbers, warehouse SKU codes, variable names (auth_middleware), acronyms, and people's names are tokens that rarely appear in an embedding model's training corpus. Their semantic weight is thin. As a result, a query that needs an exact match loses to a long generic sentence.
This is not just a hunch. Let's look at the evidence. There is a benchmark that measures it, called BEIR (Thakur et al., 2021). It holds 18 datasets from different domains, used to test cross-domain retrieval. There, a dense retrieval model that wins in one domain loses on average to BM25 in a domain it has never seen. The paper's conclusion: BM25 is a strong baseline, and the most accurate approach is re-ranking, which unfortunately costs the most compute.
2. Fooled by document length and shallow context
Cosine similarity only measures the angle between two vectors. It does not know which words matter inside a chunk. A long paragraph packed with common words can score 0.82, close to the maximum of 1, even when it does not answer the question. A high score here only means "these two vectors point the same way". It really does not mean "this is the answer".
Back to the classic algorithm: BM25
To patch that blind spot, modern IR systems are embracing an algorithm proven over decades: BM25 (Best Matching 25), part of the sparse lexical search family. It works by counting how often query words appear in a document, with damping. The more often a word appears, the smaller the extra gain. Scores are also normalized against document length.
The two approaches complement each other:
- Dense vector search wins on abstract concepts, paraphrase, and meaning that was never written down as is.
- Sparse lexical search (BM25) wins on exact matches (error codes, IDs, serial numbers, industry-specific terms), without being distorted by general semantic closeness.
A system that runs both at once and merges the results is called hybrid search. That pattern is now a built-in feature of search engines: Elasticsearch has an rrf retriever type, OpenSearch has hybrid search through a normalization processor, Weaviate has a hybrid query with an alpha parameter. A version note, checked on October 19, 2026: since Weaviate v1.24, the default hybrid fusion is relativeScoreFusion, not the pure rank-based rankedFusion.
Merging two scores of different scale: Reciprocal Rank Fusion
One problem appears as soon as two result lists sit side by side. BM25 scores have no upper bound (14.8 is normal), while cosine similarity tops out at 1. Adding two numbers from different scales raw is the same as handing the final result to chance.
The way out that the industry uses now is Reciprocal Rank Fusion (RRF), from the paper by Cormack et al. at SIGIR 2009:
RRF score(document) = Σ 1 / (k + rank_of_document_in_engine_m)
m∈M
k = 60 (the number the original paper recommends)
The trick: raw scores are thrown away, only positions are kept. A document that shows up at rank 3 in BM25 and rank 5 in vector search gains score from both lists at once. It usually rises to the top, even when it is not first in either list. The constant k damps things, so one engine putting a document at the top does not automatically dominate. The original paper tested 30 configurations and concluded that k = 60 is close to optimal. More important: the exact number is not critical.
The second layer: cross-encoder re-ranking
Hybrid search is already much better, but it still works as a bi-encoder. Query and document are turned into vectors separately, and the model never sees both side by side. Fast for filtering, weak for judging precision.
That is where the second layer comes in: a cross-encoder re-ranker such as Cohere Rerank or its open source counterpart, BAAI/bge-reranker-v2-m3. This model takes a whole (query, document) pair and runs full self-attention over both at once. So it can judge whether this chunk really answers the query, not just whether it points the same way. The price: one full inference for every candidate, and the output cannot be pre-indexed like a vector. That is why it only runs in the second stage.
The pattern becomes two-stage retrieval:
- Stage 1, retrieval (recall). Hybrid search narrows hundreds of thousands of chunks down to 50-100 candidates. This search is cheap because everything runs on an index.
- Stage 2, re-ranking (precision). Those 50-100 candidates plus the original query go into the cross-encoder, and 3-5 top chunks come out for the LLM context.
On model choice, bge-reranker-large from older tutorials has been replaced by BAAI/bge-reranker-v2-m3. The v2 model is multilingual and about eight times more used: 17 million downloads against 2 million for the large version (checked October 19, 2026 on Hugging Face). If you are just starting, use the new version.
The flow in Python
First step, merge the results of the two searchers with RRF. The function is only seven lines:
from collections import defaultdict
def reciprocal_rank_fusion(dense_ranks, sparse_ranks, k=60, top_n=5):
"""Gabungkan peringkat dense search dan BM25 pakai RRF."""
rrf_scores = defaultdict(float)
# 1. Rank contribution from dense vector search
for rank, doc_id in enumerate(dense_ranks):
rrf_scores[doc_id] += 1.0 / (k + (rank + 1))
# 2. Rank contribution from BM25
for rank, doc_id in enumerate(sparse_ranks):
rrf_scores[doc_id] += 1.0 / (k + (rank + 1))
# 3. Sort by the highest merged score
ranked = sorted(rrf_scores.items(), key=lambda item: item[1], reverse=True)
return [doc_id for doc_id, _ in ranked[:top_n]]
dense_ranks = ["artikel-jaringan-umum", "chunk-err-504", "troubleshooting-http"] # both engines' results for "ERR_CONN_REFUSED_504"
sparse_ranks = ["chunk-err-504", "kode-error-lengkap", "artikel-jaringan-umum"]
print(reciprocal_rank_fusion(dense_ranks, sparse_ranks, top_n=3))
Let's look at the output:
['chunk-err-504', 'artikel-jaringan-umum', 'kode-error-lengkap']
Look at chunk-err-504 there. It is not first in either list: rank 2 in vector search, rank 1 in BM25. Yet it rises to the top, because it gains from both lists. Meanwhile artikel-jaringan-umum, which is only a general explainer, still slips through, because it is in both lists too. RRF is a candidate filter, not the final judge. The job of striking out leftovers like that belongs to the second layer.
Once the candidate list is ready, one more step. How? Pass it to the cross-encoder:
import numpy as np
from sentence_transformers import CrossEncoder # sentence-transformers 6.1.0, released Sep 18, 2026
reranker = CrossEncoder("BAAI/bge-reranker-v2-m3")
query = "gimana cara fix error ERR_CONN_REFUSED_504 di gateway?"
pairs = [[query, doc] for doc in candidate_documents] # (query, document) scored TOGETHER
scores = reranker.predict(pairs)
order = np.argsort(scores)[::-1][:3] # take the top 3 chunks
final_context_chunks = [candidate_documents[i] for i in order]
One important note on those scores. The numbers are not probabilities from 0 to 1, and they cannot be compared across queries. A score of 3.2 in one query means nothing in another. Use them to rank, never as a fixed threshold like "drop anything you see below 0.5". That kind of threshold only throws away good chunks on queries whose scores happen to be low.
So if your RAG is off the rails now
If your agent hallucinates or misses the user's technical data, the first suspect is not how clever the LLM is. Most RAG failures in the field start in the retrieval stage handing over garbage: garbage in, garbage out. ⚠️
Let's order the fixes from cheapest:
- Put BM25 next to your vector store, and run the two searches together.
- Merge the results with RRF. No retraining needed, and no weights for you to tune by hand.
- Close with a cross-encoder re-ranker that filters the last 3-5 chunks.
If you are also handling agent memory that changes over time, this retrieval problem is only one of its two layers. The layer about time and contradictory facts gets its own write-up in AI Agent Memory: From Vector Store to Temporal Graph.
The blind spot is not in the expensive embedding model. It is in the assumption that one kind of search can answer every shape of question. Two searchers plus one judge still cost less than one large model forced to guess.