BM25 vs Semantic Search vs Hybrid RAG: What My Benchmarks Revealed
I built a codebase RAG system using BM25, semantic search, reciprocal rank fusion (RRF), and cross-encoder reranking. I expected hybrid search to outperform semantic search alone. It didn't.
On my evaluation set, semantic search achieved an MRR@5 of 0.854, while hybrid RRF dropped to 0.692. Adding cross-encoder reranking changed the result. Here's what I tested and what I learned.
The Problem
Searching a codebase isn't the same as searching ordinary text. A developer might ask: Where does the application check whether a user is logged in? But the relevant implementation could be a function named decode_access_token.
BM25 can find exact identifiers. Semantic search can match questions to code with different wording. I wanted to see whether combining both approaches would improve retrieval quality.
What I Built
My project, AI Codebase Intelligence, indexes software repositories and lets developers ask questions about their code. The system uses:
- FastAPI for the backend
- PostgreSQL for persistence
- Redis and Celery for background indexing
- Pinecone for vector storage
- BM25 for lexical retrieval
- Sentence Transformers for semantic embeddings
- RRF for combining rankings
- A cross-encoder for reranking
- Ollama for generating answers grounded in retrieved code
The retrieval pipeline looks like this:
Question
|
v
BM25 + Semantic Search
|
v
Reciprocal Rank Fusion
|
v
Cross-Encoder Reranking
|
v
Relevant Code Chunks
|
v
Local LLM
|
v
Answer + Source Citations
The Benchmark
I created a small ground-truth evaluation set and compared four retrieval methods. I measured:
- Hit@5: Whether at least one relevant result appears in the top five.
- MRR@5: How highly the first relevant result ranks within the top five, averaged across queries.
Here are the results:
| Retrieval Method | Hit@5 | MRR@5 |
|---|---|---|
| BM25 | 0.750 | 0.500 |
| Semantic Search | 1.000 | 0.854 |
| Hybrid RRF | 0.875 | 0.692 |
| RRF + Reranking | 1.000 | 0.875 |
These results come from my project's evaluation set, not a standardized public benchmark.
What Surprised Me
Hybrid RRF performed worse than semantic search alone. Combining BM25 and semantic search actually reduced both Hit@5 and MRR@5 in this experiment.
The initial hybrid implementation used equal-weight RRF. One possible explanation is that combining the ranked lists pushed some relevant semantic results lower. However, I haven't isolated the exact cause.
The important point is that adding another retrieval component did not automatically improve the results.
What Changed After Reranking?
I added a cross-encoder reranker after RRF. Instead of relying only on the original retrieval rankings, the cross-encoder evaluates the query and each candidate together to produce a new ordering.
The result:
- Hit@5 increased from 0.875 to 1.000.
- MRR@5 increased from 0.692 to 0.875.
Compared with semantic search alone, the final pipeline achieved the same Hit@5 and a slightly higher MRR@5. That improvement gave me a measurable reason to keep the reranking stage.
Beyond Retrieval
The project also includes the engineering needed to make the retrieval system usable. Repository indexing runs asynchronously using Celery and Redis. Repeated RAG responses can be cached in Redis.
Generated answers include source file and line-range citations, making it easier to verify the retrieved evidence. I also added read-only MCP tools for searching code, retrieving files, listing symbols, and asking questions about a repository.
Limitations
This was a small evaluation, so the results shouldn't be treated as universal. Different datasets, repositories, embedding models, fusion parameters, and rerankers may produce different outcomes.
I also measured retrieval quality, not end-to-end answer accuracy or latency. Those would be important next steps.
The Takeaway
The biggest lesson from this project wasn't that hybrid RAG is better or worse than semantic search. It was that retrieval quality needs to be measured instead of assumed.
My first hybrid implementation added complexity while reducing performance. Cross-encoder reranking improved the results, but the benefit only became clear after running the evaluation. A more complicated RAG architecture isn't automatically a better one.
Read the full technical article, including the architecture and implementation details: BM25 vs Semantic Search vs Hybrid RAG: What My Benchmarks Revealed
About the Author
I'm Borjan Ladinski, an AI Engineer, AI Expert, and AI Consultant based in Skopje, North Macedonia. I build AI agents, LLM applications, RAG systems, machine learning solutions, and business automation.
My work includes production AI systems, retrieval benchmarks, and experimental LLM inference on microcontrollers. I'm available for AI consulting, custom AI solutions, and engineering projects.
Explore my AI projects and consulting work
Comments
No comments yet. Start the discussion.