Vector Search

Vector search turns every text into a list of numbers, a vector. Texts about similar things point in similar directions, and cosine similarity measures how close those directions are: 1 means the same direction, 0 means nothing in common.

Textkitten toy
→
Vector[0.45, 0.8, 0, …]
→
Cosineangle with the query
→
Rankingsmallest angle first

1. Query & Documents

Learn more: sparse vs. dense

Sparse vectors have one dimension per word of the vocabulary (thousands), mostly zeros. Two texts are similar only if they share words, so cat and kitten are as unrelated as cat and laptop.

Dense vectors (embeddings) have a few hundred dimensions, all filled, learned by a neural network from huge amounts of text. Words used in similar contexts end up close, so meaning can match even without shared words.

To stay self-contained, this demo fakes an embedding model: each word has hand-written scores on 6 concepts (animal, pet, machine, tech, food, sport), and a text is the average of its words. Real models also take context into account, which this toy doesn't. Known words:

2. Vectors

Each row is a text, each column a dimension.

3. Ranking

Learn more

cosine(A, B) = (A · B) / (‖A‖ × ‖B‖): the dot product, divided by the vectors' lengths. Dividing by the lengths means only the direction counts, so a long document isn't favored just for having more words.

The plot shows only 2 dimensions at a time, so angles there don't exactly match the scores, which use all of them. Pick the axes to explore.

Searching millions of vectors one by one would be too slow: real engines use approximate nearest neighbor indexes (like HNSW) that find the closest vectors without comparing them all.