Similarity Search Patterns

by wshobson46891e7e60daNo licenseListed Oct 8, 2026Updated Oct 8, 2026

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

AI-generated overview

Guidance on implementing efficient similarity search with vector databases, covering distance metrics, index types and tuning practices.

What it does
This skill provides patterns for implementing similarity search in production systems. It explains distance metrics such as cosine, Euclidean, dot product and Manhattan, and compares index types including flat, HNSW and IVF+PQ by complexity, recall and suitable data size. It also lists best practices for tuning parameters, hybrid search, recall monitoring, pre-filtering and cost awareness, with further templates and worked examples in a reference file.
When to use it
Use it when building semantic search or RAG retrieval, implementing nearest neighbor queries, creating recommendation engines, or optimizing and scaling vector search latency. It suits teams choosing index types and tuning recall and speed trade-offs.
Requirements
No scripts are shipped; it is instructions only. It references a companion file, references/details.md, for templates and worked examples. No specific tools, packages, runtimes, credentials or network access are stated.

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

Source and attribution

Source:wshobson/agentsinplugins/llm-application-dev/skills/similarity-search-patternsat commit46891e7

License: No license

Content belongs to its original authors. SourceWeft indexes it from a public repository.

Report or request removal