Embeddings and Vector Databases Explained
Blog / Embeddings and Vector Databases Explained
What is a vector database, and what actually are embeddings? And why does every AI app suddenly need one?
The Keyword Search Problem
Traditional keyword search algorithms like BM25 rely on exact token matches. They look for the exact words you typed.Because they only match text exactly, they fail to capture the actual meaning behind a query.For example, a search for "authentication middleware" might easily miss a closely related page titled "security documentation". If the exact words are missing, the search returns nothing.So how can we solve this? This is where embeddings come in.What is an Embedding
An embedding model is a neural network that converts input text into a vector. A vector is a fixed-length list of floating-point numbers.The model maps text with similar semantic meaning to coordinates that are close to each other in a high-dimensional space.
Measuring Similarity
To find out how close two pieces of text are, we measure the distance between their vectors in this space.The most common metric is cosine similarity. This measures the geometric angle between two vectors pointing from the origin. It ignores the length or magnitude of the vectors entirely.If the angle is small, the vectors point in the same direction, and the semantic similarity is high.What a Vector Database Stores
Before saving large documents, they are split into smaller, context-rich pieces called chunks. A vector database is built specifically to store and query these items.For each chunk, the database stores three things:- The numerical vector generated by the embedding model.
- The original plain text of the chunk.
- Attached metadata like the source URL or access permissions.
Exact Search vs Approximate Search
Comparing a search query to every single vector guarantees perfect accuracy. This brute-force approach is called exact nearest neighbour search.But computing the distance to every row becomes unscalably slow for millions of vectors.To fix this, vector databases use an ANN index, which stands for Approximate Nearest Neighbour. These indexes heavily reduce the search space, trading a tiny bit of accuracy for massive speed.A leading algorithm for this is HNSW, or Hierarchical Navigable Small World.HNSW organizes vectors into layers of a proximity graph. Sparse upper layers act like a rough map to make giant leaps across the data. The search then drops down into progressively denser lower layers for exact, fine-grained navigation.
A Search End to End
Let's walk through a search step by step:- Step 1: the user provides a raw text query.
- Step 2: the query is converted into a vector using the exact same embedding model used for the stored chunks.
- Step 3: the database navigates the index to find the closest stored vectors.
- Step 4: it can optionally filter these results using the stored metadata.
- Step 5: it returns the original text of those matching chunks.

SELECT chunk_text FROM documents
ORDER BY embedding <=> '[...query_vector_numbers...]'
LIMIT 5;
Where They're Used
Vector databases power modern semantic search features. They are the engine behind recommendation systems.They also serve as the retrieval step in Retrieval-Augmented Generation pipelines for language models.Pitfalls
Vector searches come with a few common pitfalls to keep in mind:- Results are approximate, meaning the absolute closest mathematical match might occasionally be missed.
- Vectors from different models cannot be compared, so changing your model requires re-embedding your entire database from scratch.
- Semantic search struggles with exact technical identifiers like error codes, because embeddings blur precise strings into general concepts.
- To handle exact matching, production systems combine keyword search with vector search into a single pipeline, which is known as hybrid search.
Recap
| Term | Definition |
|---|---|
| Embedding | A numerical representation capturing the semantic meaning of data |
| Embedding model | The AI algorithm that translates raw text into an embedding |
| Vector | A fixed-length list of floating-point numbers |
| Cosine similarity | A mathematical metric measuring the angle between two vectors |
| Vector database | A specialized database built to store, index, and query embeddings |
| Chunk | A subdivided piece of a larger document |
| Nearest neighbour search | The algorithmic task of finding the closest vectors to a query vector |
| ANN index | A data structure that trades strict accuracy for search speed |
| HNSW | A graph-based index using sparse upper layers and dense lower layers |
| Hybrid search | A retrieval method merging exact keyword search with semantic vector search |
About TechPrep
Never walk into a technical interview unprepared again. TechPrep empowers software engineers to stop guessing and start getting offers. We provide the exact questions asked by tech companies across Data Structures & Algorithms, System Design, Low-Level Design & AI coding rounds. Don't leave your career up to chance. Join thousands of engineers who have successfully navigated the tech hiring maze and landed roles at top tech companies.