Marcio Cunha

Production RAG Pipelines: Hybrid Search and Vector Reranking

Learn how to build resilient RAG architectures by combining vector search with BM25 and cross-encoder reranking models to eliminate hallucinations and boost enterprise accuracy.

Marcio Cunha•5 min
Also available in:PortuguêsEspañol
Summary
  • Pure vector search frequently struggles when retrieving exact alphanumeric strings, product codes, or specific IDs.
  • Merging vector search results with traditional keyword-based BM25 dramatically elevates initial retrieval hit rates.
  • Reranking models act as a highly demanding final filter, reorganizing retrieved snippets by deep semantic relevance.
  • The added latency introduced by reranking requires strategic caching and parallel processing to keep user experience smooth.
  • Monitoring response quality in production demands periodic audits of similarity scores and end-user feedback loops.

The Hidden Challenges of Information Retrieval in AI Systems

When building artificial intelligence assistants using proprietary data, the standard vector search method is usually the starting point. This process turns documents into long sequences of numbers called embeddings, allowing the system to find passages by proximity of meaning. In practice, this means that if you ask about 'financial issues', the system can find texts discussing 'money troubles'. However, this magical approach often stumbles on simple details like product codes, corporate acronyms, or ID numbers, where exact string matching matters much more than conceptual proximity.

To solve this shortcoming, modern software engineering relies on hybrid retrieval, which combines the best of two very different worlds. On one side, we have vector search, specialized in capturing general context and hidden user intent. On the other, we have classic exact-word matching algorithms like BM25, widely used for decades in traditional search engines. When we combine these two forces, we ensure the system not only understands what the user meant, but also locates that specific string or protocol number lost somewhere inside a five-hundred-page contract.

The Architecture of Hybrid Retrieval with BM25 and Embeddings

Implementing a hybrid strategy requires combining scores coming from completely different universes. Vector similarity typically yields a value between zero and one based on the angle between geometric vectors, while BM25 produces term-frequency scores that can grow indefinitely. To merge these metrics fairly, we use mathematical normalization techniques like Min-Max scaling or Reciprocal Rank Fusion, known as RRF. In practice, RRF simply looks at the position where each document appeared in the two separate lists and computes a combined score based on those ranks, ignoring the original numerical scales.

This flow begins when the user's question arrives at the application backend. It is sent simultaneously to the vector database and the traditional text index. The vector database returns the top twenty passages based on semantics, while BM25 returns another twenty passages based on exact keyword matching. Next, a fusion algorithm unifies these two lists, removing duplicates and generating a consolidated set of thirty or forty promising candidates. This initial filter is fast, but it still brings plenty of noise and documents that are only vaguely relevant to the problem at hand.

The Critical Role of Reranking in Response Precision

Having thirty candidate documents is great to ensure we do not miss any useful information, but it is far too much for the language model to process without getting confused or inflating computing costs. This is precisely where reranking steps in, utilizing a specialized model known as a Cross-Encoder. Unlike traditional models that generate separate vectors for the query and each document, the Cross-Encoder reads the question and the document side by side, crossing every word of the question with every word of the text to evaluate real compatibility. In practice, it is as if a very experienced human reviewer quickly reads each found excerpt and gives a grade from zero to ten on the real usefulness of that paragraph in answering the customer's doubt.

The major performance gain of this approach is that we can feed raw hybrid search results into the reranker, which will sort documents from most relevant to least relevant with surgical precision. Then, we take only the top four or five validated snippets and hand them over to the final text generation model. This drastically reduces the noise entering the artificial intelligence context, eliminating hallucinations and ensuring much more concise and grounded answers based on company data. The obvious trade-off of this choice is processing cost, as cross-ranking models demand more computational power and add a few hundred milliseconds to the final response. To complement this implementation in production environments, see our related technical analyses on Hybrid Vector Search and Reranking: Building AI Information Retrieval Engines and Building Information Retrieval Systems with Hybrid Search and Cross-Encoder Reranking.

from sentence_transformers import CrossEncoder

# Load lightweight and production-efficient reranking model
reranker_model = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')

def rerank_documents(query, documents, top_k=3):
    # Prepare [query, document] pairs for cross-evaluation
    pairs = [[query, doc['text']] for doc in documents]
    scores = reranker_model.predict(pairs)
    
    # Associate scores back to original documents
    for i, doc in enumerate(documents):
        doc['score'] = float(scores[i])
        
    # Sort from most relevant to least relevant
    sorted_docs = sorted(documents, key=lambda x: x['score'], reverse=True)
    return sorted_docs[:top_k]

Latency Mitigation Strategies and Operational Optimization

Adding extra search and reranking layers to a system that already needs to query databases and language models can turn a fast API into a slow, frustrating service. To keep latency under control in a production environment, the architecture must be designed with a heavy focus on concurrency and caching. Vector database queries and BM25 index lookups should run in parallel using asynchronous programming, ensuring total search time equals the slowest service rather than the sum of both. Furthermore, frequent or very similar questions asked by different users should have their intermediate results cached using high-performance tools like Redis.

Another critical engineering point is hardware sizing and choosing the right reranking model. Smaller models based on the Transformer architecture can run with latencies under thirty milliseconds on commodity CPUs or small GPU instances, delivering an impressive precision boost without wrecking the infrastructure budget. It is also fundamental to establish strict limits on text sizes sent to the reranker, chopping giant paragraphs into smaller, standardized pieces known as chunks before starting the indexing and search process.

Building a robust RAG pipeline goes far beyond connecting an AI library to a document database. It requires a clear understanding of pure vector search limitations and the courage to adopt hybrid architectures followed by rigorous reranking steps. In practice, the long-term success of an enterprise assistant depends as much on the quality of indexed data as on how efficiently the system filters out noise before presenting it to the generative model. Continuously monitoring search behavior and adjusting weights between BM25 and vectors based on user feedback ensures the application evolves alongside real business needs.

Keeping active observability at every pipeline stage helps identify bottlenecks and understand precisely why specific responses failed to meet expectations. With a solid foundation comprising hybrid retrieval, efficient reranking, and constant monitoring, engineers can deliver reliable, secure artificial intelligence solutions ready for large-scale enterprise use.