Similarity Search

Similarity search is the core job of a vector database: given a query vector, find the stored vectors that are closest to it. It's how "find things like this" works — the operation behind semantic search, RAG retrieval, and recommendations. Everything else in a vector database exists to make this one operation fast and accurate.

💡 In one line: Similarity search takes a query vector and returns the stored vectors most similar to it, ranked by a distance metric.

What is Similarity Search?

Given a query vector, the database finds the most similar (nearest) stored vectors and returns them ranked by a distance or similarity metric. It's the fundamental retrieval step — the "read" side of a vector database.

How It Works

The search runs in a few steps.

Whiteboard
Whiteboard diagram


Distance / Similarity Metrics

The comparison uses a metric (from the earlier Embeddings subtopics):

  • Cosine similarity — the angle between vectors (most common for text).
  • Euclidean distance — the straight-line gap.
  • Dot product — fast, and equal to cosine on normalised vectors.

The metric must match how the vectors were created and stored.

Exact vs. Approximate

  • Exact (brute-force) — compare the query to every stored vector. Perfectly accurate, but slow at scale (cost grows with the number of vectors).
  • Approximate (ANN) — use a smart index to find near-neighbours fast, with a tiny accuracy trade-off. Essential once you have millions of vectors. (See the ANN Search subtopic.)

Search + Filters

Similarity search is often combined with metadata filters — "find similar and match these conditions" — for precise, production-ready retrieval.

Scores & Ranking

Results come back ranked, each with a score (or distance). Higher similarity — or lower distance — means a closer match. You typically use these scores to decide how many results to keep, or to set a relevance threshold.

Related Concepts

This subtopic is the umbrella; the next three zoom in:

  • K-Nearest Neighbours (KNN) — the idea of the k closest points.
  • Top-K retrieval — returning the k best matches.
  • ANN search — the fast, approximate way to do it at scale.

Code Example


Summary

  • Similarity search finds the stored vectors closest to a query vector.
  • The flow: embed the query → compare to stored vectors → rank → return the closest.
  • Comparison uses a metric — cosine, Euclidean, or dot product.
  • Exact search is accurate but slow; approximate (ANN) is fast at scale.
  • It's often paired with metadata filters, and results come back ranked with scores. EOF echo created