Module 5: Keyword vs Semantic Search
5. Hybrid Search: The Best of Both Worlds
Overview
Hybrid search combines keyword search (precision on exact matches) with semantic search (understanding of meaning). It's the approach used by modern production systems (Elasticsearch + embeddings, Weaviate hybrid, Pinecone with filters).
Hybrid search strategies
Strategy 1: Filter + Semantic
1. Filter with keyword (e.g. documents that mention "Python")
2. Rank with semantic (order by vector similarity)
Example:
Query: "Python best practices"
Step 1 (keyword): Filter the docs that contain "Python"
→ 1000 documents found
Step 2 (semantic): Rank those 1000 by cosine
→ The top-10 most relevant to "best practices"
The advantage: It guarantees that "Python" appears (keyword) while still finding the most relevant results (semantic).
Strategy 2: Rank fusion
1. Run keyword search → Ranking A (top-100)
2. Run semantic search → Ranking B (top-100)
3. Fuse the two rankings (e.g. RRF - Reciprocal Rank Fusion)
RRF (Reciprocal Rank Fusion):
score(doc) = α/rank_keyword + β/rank_semantic
Example:
Doc X: rank_keyword=5, rank_semantic=3
→ score = 1/5 + 1/3 ≈ 0.53
Doc Y: rank_keyword=1, rank_semantic=50
→ score = 1/1 + 1/50 ≈ 1.02
Doc Y has the higher final score → it appears first
The advantage: Documents that appear in both rankings (keyword AND semantic) get a boost.
Strategy 3: Cascade
1. Try keyword search
→ If there are enough results (e.g. > 10) → return them
→ If there are few results (< 10) → continue
2. Try semantic search
→ Supplement with the semantic results
The advantage: Efficient (keyword is faster). It uses semantic only when necessary.
Weaviate hybrid search (a real example)
Query: "machine learning"
Configuration:
- alpha = 0.5 (a 50% keyword, 50% semantic balance)
- alpha = 0.0 (100% keyword)
- alpha = 1.0 (100% semantic)
The internal algorithm:
final_score = alpha × semantic_score + (1-alpha) × keyword_score
Typical use: alpha=0.5 (balanced) or alpha=0.7 (favoring semantic).
Elasticsearch + embeddings (a real example)
Setup:
1. Index the normal text (keyword with BM25)
2. Add an "embedding" field (a 1536D vector)
3. On a query:
- Run keyword search (BM25)
- Run vector search (kNN)
- Combine the scores (a weighted sum)
When to use hybrid search
Use hybrid when:
- ✅ Queries are mixed (some exact, others conceptual)
- ✅ You need precision (keyword) AND coverage (semantic)
- ✅ You have the resources for both methods
- ✅ Users expect something "Google-like" (it understands intent but respects exact matches)
Examples:
- A technical documentation search engine
- E-commerce (an exact product name vs a conceptual description)
- Support systems (exact ticket IDs vs descriptions)
Hybrid trade-offs
Advantages:
- ✅ The best of both worlds
- ✅ More robust (if one fails, the other compensates)
Disadvantages:
- ❌ Greater complexity (maintaining two indexes)
- ❌ Higher cost (embeddings + an inverted index)
- ❌ Tuning the weights (alpha) requires experimentation
Real implementations
Weaviate:
- Native support for hybrid search
- An
alphaparameter, tunable per query
Elasticsearch:
- Keyword (BM25) + kNN search (embeddings)
- A script to combine the scores
Pinecone:
- Vector search + metadata filtering (pseudo-hybrid)
- Keyword filters before the vector search
Custom:
- Run keyword (Elasticsearch) and semantic (Pinecone) in parallel
- Fuse them in the application (RRF)
Summary
Key points:
- Hybrid: Keyword + semantic
- Strategies: Filter+semantic, rank fusion, cascade
- Advantages: Precision + coverage
- Disadvantages: More complexity, higher cost
- In production: Weaviate, Elasticsearch, custom fusion
Next capsule: 06-when-to-use-each.md — Decisions by use case.