Similarity Search Patterns
Patterns for implementing efficient similarity search in production systems.
When to Use This Skill
- Building semantic search systems
- Implementing RAG retrieval
- Creating recommendation engines
- Optimizing search latency
- Scaling to millions of vectors
- Combining semantic and keyword search
Core Concepts
1. Distance Metrics
| Metric | Formula | Best For | | ------------------ | ------------------ | --------------------- | --- | -------------- | | Cosine | 1 - (AΒ·B)/(βAββBβ) | Normalized embeddings | | Euclidean (L2) | βΞ£(a-b)Β² | Raw embeddings | | Dot Product | AΒ·B | Magnitude matters | | Manhattan (L1) | Ξ£ | a-b | | Sparse vectors |
2. Index Types
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β Index Types β
βββββββββββββββ¬ββββββββββββββββ¬ββββββββββββββββββββ€
β Flat β HNSW β IVF+PQ β
β (Exact) β (Graph-based) β (Quantized) β
βββββββββββββββΌββββββββββββββββΌββββββββββββββββββββ€
β O(n) search β O(log n) β O(βn) β
β 100% recall β ~95-99% β ~90-95% β
β Small data β Medium-Large β Very Large β
βββββββββββββββ΄ββββββββββββββββ΄ββββββββββββββββββββTemplates and detailed worked examples
Full template library and detailed worked examples live in references/details.md. Read that file when you need the concrete templates.
Best Practices
Do's
- Use appropriate index - HNSW for most cases
- Tune parameters - ef_search, nprobe for recall/speed
- Implement hybrid search - Combine with keyword search
- Monitor recall - Measure search quality
- Pre-filter when possible - Reduce search space
Don'ts
- Don't skip evaluation - Measure before optimizing
- Don't over-index - Start with flat, scale up
- Don't ignore latency - P99 matters for UX
- Don't forget costs - Vector storage adds up