Similarity Search Patterns

作者 wshobson46891e7e60da无许可证收录于 2026年10月8日更新于 2026年10月8日

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

AI 生成的概览

提供使用向量数据库实现高效相似度搜索的指导,涵盖距离度量、索引类型与调优实践。

功能
该技能提供在生产系统中实现相似度搜索的模式。它讲解余弦、欧氏、点积和曼哈顿等距离度量,并按复杂度、召回率和适用数据规模比较 Flat、HNSW 与 IVF+PQ 等索引类型。它还列出参数调优、混合搜索、召回率监控、预过滤和成本意识等最佳实践,并在参考文件中提供更多模板与详细示例。
适用场景
适用于构建语义搜索或 RAG 检索、实现最近邻查询、创建推荐引擎,以及优化和扩展向量搜索延迟的场景。也适合需要选择索引类型并权衡召回率与速度的团队。
运行要求
不附带脚本,仅为说明性内容。它引用配套文件 references/details.md 以获取模板和详细示例。未说明需要特定工具、软件包、运行时、凭据或网络访问。

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

来源与署名

来源:wshobson/agents位于plugins/llm-application-dev/skills/similarity-search-patterns提交46891e7

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架