Data & storage
Vector Search
Embed text into vectors so that similar meaning lands near each other — then search by distance, not by matching words.
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")
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.
// 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.
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.
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.