DEV Community

Danil Vilkhovskiy for Softenq

Posted on Edited on Originally published at softenq.com

Local Hybrid RAG Over 5,700 Docs: Why Dropping Directory Auto-Guessing Beat Chunk Context Tuning

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
Enter fullscreen mode Exit fullscreen mode
  • Zero cloud search APIs: Qdrant, BGE-M3 embeddings, and bge-reranker-v2-m3 run on local GPU servers.
  • No semantic result cache: In a technical KB where queries differ only by a port (8080 vs 8081), cosine similarity hits 0.965—above a 0.96 cache threshold—returning the wrong config. Result caching stays disabled.
  • Rerank depth discipline: Expanding candidates from 30/15 to 100/60 added zero Hit@5 gain (22 vs 23 of 27) 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:

  1. 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.
  2. 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.031 on the 36 post-switch questions).
  3. Cross-encoder reranking fixes #1 precision: Across 103 questions, bge-reranker-v2-m3 lifted Hit@1 from 51 to 67 (p = 0.002) at 355 ms vs 21 ms in 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 (57 answered in KB): 96% Hit@5 (55/57), 81% Hit@1 (46/57), MRR@5 0.87.
  • Latency: 288 ms median (408 ms p95) on single-node queries; 548 ms median in production logs (default <= 5 results, 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 in 164 vs 155 cases (p = 0.035) with 98% (161/164) accuracy once opened.

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)