design·lab

Data & storage

Vector Search

Embed text into vectors so that similar meaning lands near each other — then search by distance, not by matching words.

✗ Problem

Keyword search matches words, not meaning

A classic inverted index matches exact tokens. Search "car" and it will never surface a document that only says "automobile" or "vehicle" — even though a human reader knows they mean the same thing.

function keywordSearch(query, docs) {
  // exact token match only
  return docs.filter(d =>
    d.tokens.includes(query)
  );
}
keywordSearch("car", docs)
// → [] (doc only says "automobile")
Query: "car"
✗ no match
Doc: "automobile for sale"
Lexical match ≠ semantic match. Same meaning, different words → zero results.
✓ How it works

Map meaning into a vector space

An embedding model maps text (or images) into a high-dimensional vector so that similar meanings land near each other. Search becomes: embed the query, then find its nearest neighbors.

"automobile"
↓ embed()
[0.12, -0.87, …]
768-dim vector
// cosine similarity: how aligned two vectors are
cos(A, B) = (A · B) / (‖A‖ × ‖B‖)

// 1.0 = same direction, 0 = unrelated

At millions of vectors, comparing against every one is too slow. An ANN index (e.g. HNSW) organizes vectors into a navigable graph, so search is sub-linear — approximate, but fast.

✓ See it live

Nearest neighbors in embedding space

Pick a query word. Its k=3 nearest neighbors — by distance in this 2D embedding space — light up and rank below. Notice they're semantic neighbors, not string matches.

car
automobile
vehicle
truck
dog
puppy
cat
kitten
bank
finance
money
loan
    ✓ Takeaway

    Search by meaning, not by string

    • Embeddings map text/images into vectors where semantic similarity = spatial closeness.
    • Cosine similarity (or distance) ranks candidates by meaning, not exact tokens.
    • ANN indexes like HNSW make nearest-neighbor search sub-linear at scale.
    • Powers: semantic search, recommendations, and RAG (retrieval-augmented generation).
    • Caution: quality depends entirely on the embedding model; content changes require re-embedding.
    Pairs with a search index for hybrid search, and feeds the AI agent's RAG memory.