惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
博客园_首页
WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
有赞技术团队
有赞技术团队
阮一峰的网络日志
阮一峰的网络日志
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
酷 壳 – CoolShell
酷 壳 – CoolShell
Y
Y Combinator Blog
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
Engineering at Meta
Engineering at Meta
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
N
Netflix TechBlog - Medium
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
G
Google Developers Blog
Last Week in AI
Last Week in AI

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
What is Hybrid search in RAGs?
Rushank Sava · 2026-04-30 · via DEV Community

Rushank Savant

⚠️Need of Hybrid Search

We have documents containing error codes in python with their respective definitions and use-cases. User writes a query to know about "What is ERR_404_AUTH?"

  • Classic Rag: Will retrieve all the authentication and error related context it can find from vector db (document embeddings).

  • Lexical search: Will search for terms ["What", "is", "ERR_404_AUTH"]

  • Hybrid search: Will search for keyword "ERR_404_AUTH" and retrieve semantically similar documents using similarity search.


🛠️Using BM25

Take BM25 as extended version of TF-IDF for key-word based search.

The step-wise implementation of BM25 in LangChain is straightforward because LangChain provides a built-in BM25Retriever.

Here is the step-wise implementation alongside the intuition.

# pip install rank_bm25
from langchain_community.retrievers import BM25Retriever
from langchain_core.documents import Document

# Chunks from your text splitter
chunks = [
    Document(page_content="The AX-705 engine uses a 4-stroke cycle."),
    Document(page_content="Maintenance for AX-705 requires synthetic oil."),
    Document(page_content="Four-stroke engines are common in modern cars.")
]

# Step: Build the BM25 index (The Inverted Index)
bm25_retriever = BM25Retriever.from_documents(chunks)
bm25_retriever.k = 2  # Retrieve top 2

Enter fullscreen mode Exit fullscreen mode


✨Creating the Hybrid "Ensemble"

To get the best of both worlds (exact keywords + semantic meaning), you merge your vector retriever with the BM25 retriever.

from langchain.retrievers import EnsembleRetriever

# Assume 'chroma_retriever' is already created from your syllabus
hybrid_retriever = EnsembleRetriever(
    retrievers=[bm25_retriever, chroma_retriever],
    weights=[0.3, 0.7]  # 30% importance to Keywords, 70% to Meaning
)

Enter fullscreen mode Exit fullscreen mode


🔍The 4 Steps of BM25 (Under the Hood)

When you call hybrid_retriever.invoke("AX-705 engine"), the BM25 part follows these steps:
1. Tokenization: The query "AX-705 engine" is split into ["ax-705", "engine"].

2. Lookup: The retriever looks into its "Inverted Index" (a dictionary) to see which document chunks contain these exact strings.

3. Scoring (f(q, d)): It calculates a score for each match using the BM25 formula:
- Rareness: Since "AX-705" is rare in the database, it's worth more points than "engine".
- Saturation: Even if a doc mentions "engine" 100 times, its score won't skyrocket (preventing "keyword stuffing" from winning).
- Length Penalty: If a tiny 10-word chunk matches both words, it ranks higher than a massive 1000-word chunk matching both.

4. Ranking: It returns a list of chunks sorted by this score.


👉Next steps:

Reciprocal Rank Fusion (RRF): The Glue:

When the Ensemble Retriever gets the BM25 list and the Vector list, it needs to combine them. Since their scores are on different scales (one is 0-1, the other could be 0-25), it uses RRF.

Logic: It looks at the rank (position) of a document in both lists.
Intuition: If a document is #1 in BM25 but #50 in Vector Search, it still gets a high total score because it's a "perfect" keyword match.

I hope this was useful..