What Is a Vector Database? How AI Search Works Under the Hood
Ask a normal search box for “how do I reset my router password” and it looks for those exact words. Ask an AI search tool the same question phrased as “I forgot the login for my home wifi box” and it still finds the right help page, with almost no words in common. So what is a vector database? It is a database built to store and search numerical representations of meaning, called embeddings, so software can find things similar in sense rather than identical in spelling.
Chatbots that answer from your own documents and code search that finds a function by what it does both lean on this idea. This article walks through how a vector database turns text into numbers, how it finds the nearest match, and when it earns its place in your stack.
What Is a Vector Database, Exactly?
A vector database stores vectors: long lists of numbers that capture the meaning of a piece of text, an image, or a sound clip. Each vector is produced by an embedding model, and items with similar meaning end up close together in mathematical space. The database answers one question fast: given this vector, which stored vectors are closest to it?
That is a different job from what a relational database does. Postgres or MySQL answer exact questions, such as finding rows where the price equals 50. A vector database answers a fuzzy question: find the ten documents whose meaning is closest to this query. The storage engine, the index, and the query language are all shaped around that one operation. In practice, you store a chunk of text, its vector, and metadata such as the source file.
Embeddings: Turning Meaning Into Numbers
An embedding is a list of numbers, often a few hundred to a few thousand long, that represents one piece of content. An embedding model reads a sentence and outputs the list: two sentences that mean nearly the same thing produce lists that are close together, while two unrelated sentences produce lists that are far apart.
Take three sentences: “the cat sits on the mat”, “a feline rests on a rug”, and “quarterly revenue rose 12 percent”. The first two land near each other in vector space because they describe the same scene. The third lands far away because it belongs to a different topic. Meaning becomes geometry, and “similar” becomes “nearby”. If you want the mechanics in more detail, this guide to how vector embeddings work with code examples walks through generating them in Python.
Embeddings are not perfect. They capture what the model learned from its training data, so they inherit its blind spots: a model trained mostly on English web text ranks English documents better than others, and domain jargon can confuse a general model, since “branch” means one thing in a bank and another in git. Vectors from one model also cannot be compared with vectors from another, so pick one model per collection and stick with it.
How Similarity Search Actually Works
Once everything is a vector, finding similar items becomes a distance problem: the database measures the distance between the query vector and stored vectors, then returns the smallest distances. The most common measure is cosine similarity, which looks at the angle between two vectors, and it is the default because the angle tracks meaning better than the raw size of the numbers.
Checking every stored vector one by one works fine for a few thousand items, but a brute-force scan over a few million takes too long for a search box that should answer in milliseconds. So vector databases build an approximate nearest neighbor index, usually HNSW, a graph structure that lets the search hop from vector to nearby vector without scanning the whole collection. Results are close to the true nearest neighbors, and the speed gain is enormous.
What Happens Inside When You Run a Query
A typical AI search request moves through four steps. First, the user’s question is turned into a vector by the same embedding model that embedded the stored documents. Second, the database searches its index for the nearest stored vectors, usually the top 10 to 50. Third, the application fetches the original text, filters by metadata such as “only docs from the billing section”, and optionally re-ranks them. Fourth, the top chunks go into a language model prompt, or straight to the results page.
Notice what the database does not do: it does not understand the question, write an answer, or judge whether a document is true. It ranks by closeness in vector space, so if the stored documents contain wrong information, the search will faithfully return the wrong information. Metadata filtering also helps: a filter on “version = current” keeps stale answers out before ranking starts.
Vector Database vs Keyword Search
Keyword search and vector search answer different questions, and most real products need both. Keyword search is exact, predictable, and fast, and it wins when the user types a product code, an error message, or a name. Vector search wins when the user describes a problem in their own words and the answer uses different words entirely.
A customer types “my invoice shows a charge I do not recognise”, and keyword search may surface the billing FAQ if those words appear. But a paraphrase such as “why did you take money from my card” shares almost no words with that FAQ, so keyword search misses it while vector search finds it. The flip side is precision: keyword search never returns a document that lacks the word you typed, while vector search can return a plausible-looking document about something adjacent. Many teams run both searches, merge the lists, and let a reranker decide the final order. The practical guide to hybrid search covers how BM25 keyword ranking and vector search are combined in a real pipeline.
| Aspect | Keyword search | Vector search |
|---|---|---|
| Matches on | Exact words and phrases | Meaning and paraphrase |
| Best for | Product codes, names, error messages | Questions asked in natural language |
| Weak spot | Synonyms and rephrasing | Exact terms that must match |
| Result style | Predictable, explainable | Fuzzy, ranked by closeness |
| Typical cost | Cheap, built into most databases | Needs an embedding model plus an index |
Vector Database vs a Postgres Extension
The first question most developers ask is whether they need a separate product at all, because Postgres with the pgvector extension can store vectors and run similarity search with a single SQL query. For many projects, that is the right answer: if you already run Postgres, your collection is under a few million vectors, and your team knows SQL, pgvector removes an entire service from your architecture.
A dedicated vector database earns its place when the workload grows past what a general-purpose database handles well: tens of millions of vectors, heavy write rates, or latency targets under 50 milliseconds at scale. The comparison of pgvector and dedicated vector databases lays out where each option stops being enough, and the comparison of Pinecone, Weaviate, Qdrant, and Milvus covers the dedicated side in detail. Start with the tool you already run: moving from pgvector to a dedicated system later is a migration, not a rewrite.
When You Actually Need One
A vector database is worth it when your product’s core value depends on finding things by meaning: a chatbot that answers from your documentation, a support search that understands paraphrases, a recommendation feed, duplicate detection across millions of records, and semantic code search.
You probably do not need one when keyword search already works: an admin panel filtering orders by status, a blog search over a few hundred posts, or a lookup by username will not get better with vectors. The same applies when your data is small: with a few thousand documents, you can store the vectors in a Postgres table and scan them directly. If a scan of your whole collection answers in under 100 milliseconds, an index buys nothing; past a few million vectors or hundreds of queries per second, a dedicated system starts to matter.
Picking the Right Setup for Your Project
Start from the retrieval quality you need, not from the database brand. Build a small test set first: 30 to 50 real questions your users ask, each paired with the document that should win. Run them through a prototype and check how often the right document lands in the top five.
Once the prototype works, the setup decision gets simple. A side project or an internal tool with a modest collection can live on pgvector inside the Postgres you already run. A product whose search is the product, with millions of vectors and strict latency targets, justifies a dedicated system, managed or self-hosted depending on your team’s appetite for running infrastructure. If you are wiring this into a larger AI feature, this tutorial on building a RAG pipeline in Python shows how the vector search, the prompt, and the language model fit together.