LangChain provides specific abstractions to facilitate hybrid search. For instance:
EnsembleRetriever: This class in langchain.retrievers accepts a list of retrievers (such as a BM25 retriever and a vector store retriever) and unifies their results. It typically uses the Reciprocal Rank Fusion (RRF) algorithm to rerank documents based on their positions in the individual result lists. You can assign weights to prioritize specific retrievers within the RRF calculation.# Example using EnsembleRetriever (assumes retrievers are initialized)
from langchain.retrievers import EnsembleRetriever
# Assume bm25_retriever and faiss_retriever are already configured
# Example: pip install rank_bm25, faiss-cpu
# Initialize sparse retriever (e.g., BM25)
# ... setup bm25_retriever ...
# Initialize dense retriever (e.g., FAISS vector store)
# ... setup faiss_retriever ...
# Combine them using EnsembleRetriever (uses Weighted Reciprocal Rank Fusion)
ensemble_retriever = EnsembleRetriever(
retrievers=[bm25_retriever, faiss_retriever],
weights=[0.4, 0.6] # Optional: Weights applied to the RRF calculation
)
# Use the hybrid retriever
query = "How to fix connection timeout?"
hybrid_results = ensemble_retriever.invoke(query)
print(hybrid_results)
Cleaner syntax. Built-in debugging. Production-ready from day one.
Built for the AI systems behind ApX Machine Learning
Was this section helpful?
EnsembleRetriever, detailing its usage and how it combines results from multiple retrievers, including sparse and dense methods.© 2026 ApX Machine LearningEngineered with