When building an internal RAG knowledge base for AI coding and operations agents (~5,700 documents and ~49,000 chunks in Russian mixed with English ports, hosts, and service names), semantic-only vector search misses exact identifiers—while naive metadata rules silently kill recall before ranking even starts.
We built a 100% self-hosted hybrid search stack (Qdrant dense + BM25 + BGE-M3 + bge-reranker-v2-m3 exposed over the open MCP protocol) and benchmarked every change in an evaluation harness before rollout. (Complete methodology, statistical tests, and charts: Softenq Case Study.)
Architecture: Local Hybrid Retrieval + MCP
from qdrant_client import models
# text: query after synonym expansion; query_vec: dense BGE-M3 vector
candidates = qdrant.query_points(
collection_name=COLLECTION,
prefetch=[
models.Prefetch(query=query_vec, limit=30),
models.Prefetch(
query=models.Document(
text=text,
model="qdrant/bm25",
options={
"stemmer": {"type": "snowball", "language": "russian"},
"stopwords": "russian",
"tokenizer": "word",
},
),
using="bm25",
limit=30,
),
],
query=models.FusionQuery(fusion=models.Fusion.RRF),
limit=30,
)
# bge-reranker-v2-m3 cross-encoder rescores the top 15; top 5 go to the agent
-
Zero cloud search APIs: Qdrant,
BGE-M3embeddings, andbge-reranker-v2-m3run on local GPU servers. -
No semantic result cache: In a technical KB where queries differ only by a port (
8080vs8081), cosine similarity hits0.965—above a0.96cache threshold—returning the wrong config. Result caching stays disabled. -
Rerank depth discipline: Expanding candidates from
30/15to100/60added zero Hit@5 gain (22vs23of27) while quadrupling median harness latency (487 ms→2,063 ms).
What Actually Moved Retrieval Accuracy (p < 0.001)
On 76 held-out and chunk-context questions collected after our first cleanups:
| Metric | Former Search | First Cleanups (Keyword Filter) | Current Search (+ Qdrant BM25) |
|---|---|---|---|
| Hit@1 (right doc first) | 34 / 76 | 42 / 76 |
49 / 76 (p < 0.001) |
| Hit@5 (right doc in top 5) | 45 / 76 | 55 / 76 (p = 0.006) |
63 / 76 (p = 0.039) |
| MRR@5 | 0.50 | 0.62 | 0.73 |
| In top 30 candidates | 50 / 76 | 60 / 76 | 70 / 76 |
Three engineering lessons stood out:
-
Automatic directory selection was the #1 bug: Guessing a subdirectory from question keywords acted as a hard filter and caused 7 of 9 top-five misses and every zero-result query. Now we search the entire KB unless the caller passes an explicit
path_prefix. -
BM25 scoring beats boolean keyword filters: A hard keyword filter required every query word to appear, returning nothing on long questions. Switching to Qdrant BM25 lifted Hit@5 on long/context questions (
10 wins, 2 losses,p = 0.039;6 wins, 0 losses,p = 0.031on the 36 post-switch questions). -
Cross-encoder reranking fixes #1 precision: Across 103 questions,
bge-reranker-v2-m3lifted Hit@1 from51to67(p = 0.002) at355 msvs21 msin the harness—and every rollout goes through an automated A/B quality gate against the live production copy.
Meanwhile, Contextual Retrieval chunk summaries (~49,000 chunks) showed no statistically significant gain on an 82-question blind check, so we kept complexity low and left them out of production.
Production Performance & Why MCP Agents Beat Single-Call RAG (95% vs 75%)
-
Live agent queries (
57answered in KB):96%Hit@5 (55/57),81%Hit@1 (46/57), MRR@50.87. -
Latency:
288 msmedian (408 msp95) on single-node queries;548 msmedian in production logs (default<= 5results, no directory filter). -
MCP Agent vs One-Shot Chain (Qwen 3.8 27B on 177 blind-scored questions):
- Single-call top-5 RAG chain:
133 / 177(75%) correct answers (82%when gold doc was in top-5). - Same model acting as an MCP agent (calling search + reading full documents):
169 / 177(95%) correct (p < 0.001), locating the source doc in164vs155cases (p = 0.035) with98%(161/164) accuracy once opened.
- Single-call top-5 RAG chain:
Engineered by Softenq — self-hosted enterprise AI, Go/Java/Python systems, and observability engineering. Full experimental tables and McNemar/bootstrap stats: https://www.softenq.com/cases/ai-knowledge-search/.
Top comments (0)