Skip to content
similarity-search-patterns logo

Similarity Search Patterns

similarity-search-patterns

Implement efficient similarity search with vector databases. Use when building semantic search, implementing nearest neighbor queries, or optimizing retrieval performance.

wshobson/agents10.2Kinstalls40.2Kstars

SKILL.md

Full skill instructions

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

More skills from wshobson

Comprehensive testing strategies for Python using pytest, fixtures, mocking, and test-driven development.

40.2K 34.6K
View

Profile and optimize Python code using cProfile, memory profilers, and performance best practices.

40.2K 34.4K
View

Comprehensive patterns for Next.js 14+ App Router, Server Components, and modern full-stack React development.

40.2K 31.5K
View
code-review-excellence logo
wshobson/agents

code-review-excellence

Systematic code review practices for constructive feedback, bug detection, and team knowledge sharing.

40.2K 29.4K
View
api-design-principles logo
wshobson/agents

api-design-principles

REST and GraphQL API design principles for building scalable, developer-friendly APIs.

40.2K 29.4K
View

Use this skill when designing or reviewing a PostgreSQL-specific schema. Covers best-practices, data types, indexing, constraints, performance patterns, and advanced features

40.2K 26.9K
View

Use this skill when designing or reviewing a PostgreSQL-specific schema. Covers best-practices, data types, indexing, constraints, performance patterns, and advanced features

40.2K 26.9K
View
fastapi-templates logo
wshobson/agents

fastapi-templates

Production-ready FastAPI project structure with async patterns, dependency injection, and layered architecture.

40.2K 25.2K
View
mobile-android-design logo
wshobson/agents

mobile-android-design

Material Design 3 and Jetpack Compose patterns for building modern, adaptive Android applications.

40.2K 23.7K
View
e2e-testing-patterns logo
wshobson/agents

e2e-testing-patterns

Comprehensive guide to building reliable, maintainable end-to-end test suites with Playwright and Cypress.

40.2K 23.6K
View
architecture-patterns logo
wshobson/agents

architecture-patterns

Implement proven backend architecture patterns for maintainable, testable, and scalable systems.

40.2K 23.3K
View
mobile-ios-design logo
wshobson/agents

mobile-ios-design

Native iOS app design with SwiftUI, Apple HIG compliance, and adaptive layouts for iPhone and iPad.

40.2K 22.9K
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View