Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Workflow:Deepset ai Haystack Hybrid Document Search

From Leeroopedia
Knowledge Sources
Domains Information_Retrieval, NLP, Search
Last Updated 2026-02-11 20:00 GMT

Overview

End-to-end process for combining keyword-based (BM25) and semantic (embedding) retrieval strategies with reranking to achieve superior document search accuracy.

Description

This workflow implements a hybrid document search pipeline that leverages the complementary strengths of two retrieval methods. BM25 retrieval excels at exact keyword matching while embedding retrieval captures semantic similarity. The pipeline runs both retrievers in parallel against the same document store, merges their results using a DocumentJoiner, and then applies a cross-encoder reranker (TransformersSimilarityRanker) to produce a final relevance-ordered document list. This approach consistently outperforms either retrieval method used alone.

Usage

Execute this workflow when a single retrieval method is insufficient for your search quality requirements. Hybrid search is particularly valuable when your document corpus contains both technical terminology (where BM25 excels) and natural language descriptions (where semantic search excels), or when recall is critical and you want to maximize the chance of finding relevant documents.

Execution Steps

Step 1: Prepare Document Store with Embeddings

Ensure the document store contains documents with both text content (for BM25) and pre-computed vector embeddings (for embedding retrieval). Use a SentenceTransformersDocumentEmbedder to generate embeddings and write them to the store.

Key considerations:

  • Documents must have both content and embedding fields populated
  • Use the same embedding model for both indexing and querying

Step 2: Configure BM25 Retriever

Instantiate an InMemoryBM25Retriever connected to the document store. BM25 performs keyword-based retrieval using term frequency and inverse document frequency scoring.

Key considerations:

  • No embedding model required; operates on raw text content
  • Effective for exact term matching and rare keyword queries

Step 3: Configure Embedding Retriever

Instantiate an InMemoryEmbeddingRetriever connected to the same document store. This retriever performs vector similarity search against the document embeddings.

Key considerations:

  • Requires a text embedder to convert the query to a vector at query time
  • Uses SentenceTransformersTextEmbedder with the same model used for document embedding

Step 4: Merge Retrieved Documents

Use a DocumentJoiner to combine the results from both retrievers into a single document list. The joiner handles deduplication when the same document appears in both result sets.

Key considerations:

  • Documents retrieved by both methods appear once in the merged list
  • Join strategy options: concatenation, merge, reciprocal rank fusion

Step 5: Rerank with Cross-Encoder

Apply a TransformersSimilarityRanker to reorder the merged documents by relevance to the query. The cross-encoder model scores each query-document pair jointly, providing more accurate relevance judgments than either retrieval method alone.

Key considerations:

  • Cross-encoder models are more accurate but slower than bi-encoders
  • top_k parameter controls the number of final results returned
  • The ranker receives the query as a separate input for scoring

Step 6: Connect and Execute Pipeline

Wire all components together with parallel branches for the two retrievers feeding into the joiner, then the ranker. Execute with the query provided to all three query-consuming components.

Pseudocode:

Connect text_embedder -> embedding_retriever
Connect bm25_retriever -> joiner
Connect embedding_retriever -> joiner
Connect joiner -> ranker
Run with query for bm25_retriever, text_embedder, and ranker

Execution Diagram

GitHub URL

Workflow Repository