Find items that are similar in meaning, using numbers called embeddings.
A vector database stores embeddings: lists of numbers that capture the meaning of text, images or audio. It finds the items whose numbers are closest to your query, so it finds things that are similar, not just equal.
Normal databases match exact values: id = 5, name = "Asha". Search by meaning is different. "car" and "automobile" share no letters, yet mean the same. An embedding model (a neural network) reads the text and outputs a vector, a list of numbers. Things with similar meaning end up at nearby points in this number space.
To search, you embed the query with the same model, then ask for the stored vectors nearest to it. "Nearest" is measured with a similarity metric: cosine similarity (the angle between vectors), Euclidean distance (straight line), or dot product.
| Measure | What it checks | Use when |
|---|---|---|
| Cosine similarity | The angle between two vectors. Length does not matter. | Text embeddings, where direction carries the meaning |
| Euclidean (L2) distance | Straight-line distance between two points. | When length also matters, such as some image features |
| Dot product | Angle and length together. Larger is more similar. | Recommenders. Same as cosine if vectors are normalised to length 1 |
| Index | Idea | Good and bad |
|---|---|---|
| IVF (inverted file) | Cluster vectors into cells. Search only the few cells nearest the query. | Small and simple. Recall depends on how many cells you probe. |
| HNSW | A layered graph of links between close vectors. Walk it from coarse to fine. | Fast and high recall. Uses lots of memory and is slower to build. |
| PQ (product quantisation) | Compress each vector into short codes. | Saves memory a lot. Loses a bit of accuracy. |
| LSH (hashing) | Hash so that similar vectors land in the same bucket. | Simple idea. Often beaten by HNSW in practice. |
| Disk-based graphs (for example DiskANN) | Keep most of the index on SSD. | Handles huge sets cheaply. More complex. |
-- <=> is cosine distance in pgvector SELECT id, title FROM documents ORDER BY embedding <=> '[0.12, -0.40, 0.88, ...]' LIMIT 5;
A toy with 60 items in 2 dimensions and fixed positions (no random numbers). Real embeddings have hundreds or thousands of dimensions and millions of vectors, and real indexes (IVF, HNSW) are more refined. The ideas carry over: similarity is a number, exact search is a full scan, and approximate search trades a little recall for a lot of speed.
| Term | Meaning in easy words |
|---|---|
| Embedding | A list of numbers that represents the meaning of an item |
| Vector | An ordered list of numbers |
| Dimension | How many numbers are in each vector, such as 384 or 1536 |
| Cosine similarity | How close the directions of two vectors are |
| kNN | Find the k nearest vectors |
| ANN | Approximate nearest neighbour search: fast, may miss some results |
| Recall | The share of true nearest neighbours that the search returned |
| HNSW | A graph index with several layers for fast approximate search |
| IVF | An index that clusters vectors and searches only some clusters |
| RAG | Retrieval-augmented generation: look up documents, then let a model answer using them |
| Choice | We gain | We lose |
|---|---|---|
| Exact search | Perfect recall | Slow on large data |
| Approximate search | Speed and scale | Some recall, extra tuning |
| More dimensions | Richer meaning | More memory and slower search |
| Quantisation (compression) | Much less memory | A little accuracy |
| Vector DB vs vector column in Postgres | Specialised features and scale | Another system to run (the extension is simpler for small data) |
| Hybrid (keyword + vector) | Catches exact terms and meaning | More moving parts |