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.
Text going through an embedding model to become a vector, which lands close to similar text in the meaning space
Typical open-source models output dense vectors with a few hundred dimensions. Larger commercial models can output vectors with several thousand numbers.

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.
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.
HNSW's three layers: a giant leap across the sparse top layer, then shorter hops on the denser layers below to the nearest node

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.
A search end to end: the query becomes a vector with the same embedding model, the index finds the nearest vectors, an optional metadata filter runs, and the original text comes back
We can see this in action using pgvector, an open-source extension for PostgreSQL:
sql
SELECT chunk_text FROM documents ORDER BY embedding <=> '[...query_vector_numbers...]' LIMIT 5;
The first line selects the original stored plain text from our table.The second line uses <=>, which is the cosine distance operator in pgvector. This calculates the geometric angle between the stored embeddings and the query vector, ordering the results ascendingly from the smallest angle to the largest.The third line restricts the output to the top five closest matches.

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

TermDefinition
EmbeddingA numerical representation capturing the semantic meaning of data
Embedding modelThe AI algorithm that translates raw text into an embedding
VectorA fixed-length list of floating-point numbers
Cosine similarityA mathematical metric measuring the angle between two vectors
Vector databaseA specialized database built to store, index, and query embeddings
ChunkA subdivided piece of a larger document
Nearest neighbour searchThe algorithmic task of finding the closest vectors to a query vector
ANN indexA data structure that trades strict accuracy for search speed
HNSWA graph-based index using sparse upper layers and dense lower layers
Hybrid searchA 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.