What is a Vector Database?
A vector database stores and searches high-dimensional vectors, usually embeddings, so AI applications can quickly find information that is semantically similar to a query.
By the end of this lesson, you will be able to:
- Explain what a vector database stores and why embeddings are useful.
- Understand the difference between a vector and a normal database record.
- Describe how similarity search works at a high level.
- Understand metadata, filtering, indexing, and top-k retrieval.
- Build a small Python mental model of a vector search system.
- Explain how vector databases fit into semantic search and RAG.
What Is a Vector Database?
A vector database is a database designed to store vectors and efficiently retrieve the vectors that are most similar to a query vector. In Generative AI, those vectors commonly come from an embedding model.
Why Do We Need One?
Embeddings turn text, images, audio, or other content into vectors. Once you have thousands or millions of vectors, you need a practical way to store them, search them, filter them, update them, and retrieve the best matches.
Knowledge bases can contain a large number of embedded documents.
Exact keyword matching may miss relevant wording and concepts.
Searching every vector naively becomes expensive as the collection grows.
Real applications often need filters such as product, tenant, language, or date.
What Does It Store?
A useful mental model is that one stored item contains more than just a vector. It usually combines the vector with an identifier, metadata, and often the original text or a reference to it.
Vector Database vs. Traditional Database
They are not necessarily competitors. Many production systems use both: a relational or document database for application data and a vector database or vector index for semantic retrieval.
Exact / structured lookup
WHERE customer_id = 4821- Rows and structured fields
- Exact values and ranges
- Transactions and relationships
Similarity lookup
Find content similar to this meaning- Vectors and metadata
- Nearest-neighbor search
- Semantic retrieval
How Similarity Search Works
The application first converts the user's query into an embedding. The vector database then compares that query vector with stored vectors and returns the closest matches according to the selected distance or similarity metric.
What Does βTop-kβ Mean?
Most retrieval systems ask for the top-k nearest results. If k = 5, the search returns the five highest-ranked candidates after applying the selected search rules.
A high similarity score does not automatically mean the result is correct. Retrieval quality should be evaluated on your real questions and documents.
Metadata Makes Retrieval More Useful
Similarity is only one dimension of retrieval. Metadata lets you narrow the search before or during vector retrieval.
Indexing: How Search Becomes Fast
If a collection contains a huge number of vectors, comparing a query with every single vector can be expensive. Vector systems therefore use indexing and approximate nearest-neighbor techniques to search efficiently.
Simple conceptually, but costly at large scale.
Use an index to find promising nearby candidates quickly.
Rank the candidate vectors and return the best matches.
Python: A Tiny Vector Store Mental Model
This example is intentionally simple. It shows the data flow without depending on a particular vector database vendor.
documents = [
{"id": "doc-1", "text": "Reset your password from account settings.", "vector": [0.90, 0.10]},
{"id": "doc-2", "text": "Track your order from the shipping page.", "vector": [0.20, 0.85]},
{"id": "doc-3", "text": "Update your billing address in your profile.", "vector": [0.65, 0.35]},
]
query_vector = [0.82, 0.18]
# In a real vector database, the index performs this search efficiently.
# Here we keep the example conceptual and focus on the stored structure.
for document in documents:
print(document["id"], document["text"], document["vector"])
print("Query vector:", query_vector)Python: Calculate a Simple Similarity Ranking
Before using a database, it helps to understand the operation it accelerates. This small example ranks two-dimensional vectors with cosine similarity.
import numpy as np
def cosine_similarity(a, b):
a = np.array(a, dtype=float)
b = np.array(b, dtype=float)
denominator = np.linalg.norm(a) * np.linalg.norm(b)
return float(np.dot(a, b) / denominator)
query = [0.82, 0.18]
candidates = {
"password reset": [0.90, 0.10],
"order tracking": [0.20, 0.85],
"billing address": [0.65, 0.35],
}
ranked = sorted(
((name, cosine_similarity(query, vector))
for name, vector in candidates.items()),
key=lambda item: item[1],
reverse=True,
)
for name, score in ranked:
print(f"{score:.3f} {name}")Vector Database in a RAG System
RAG uses retrieval to bring relevant external information into the model's context. A vector database is commonly used as the retrieval layer for that workflow.
Vector Database vs. Vector Index
These terms are related but not identical. A vector index is the search structure; a vector database is a broader storage and retrieval system that can manage vectors, metadata, persistence, and APIs.
Common Vector Database Operations
Add a new document or replace an existing record.
Return the nearest vectors for a query embedding.
Combine semantic search with structured metadata.
Change metadata, content references, or embeddings.
Delete stale, private, or invalid content.
Retrieve a known vector record directly.
When Should You Use a Vector Database?
Find relevant content even when the query and document use different words.
Retrieve company documents, manuals, policies, or product knowledge for an LLM.
Find products, articles, images, or users with similar representations.
Search for images using image embeddings instead of filenames alone.
Retrieve similar historical examples before making a decision.
Surface documents or media that are conceptually close to a selected item.
What a Vector Database Does Not Do
The embedding model normally converts content into vectors. The database stores and searches those vectors.
A vector search system retrieves candidates. An LLM can then reason over retrieved context.
Poor chunking, poor embeddings, weak filters, or bad queries can still produce poor retrieval.
Most applications still need conventional databases for structured transactional data.
How to Choose One Later
You do not need to choose a vendor in this lesson. When you reach that decision, compare the system against your actual workload.
Design a Tiny Knowledge Base
Imagine you are building a customer-support RAG application.
- Choose 10 support articles.
- Split them into searchable chunks.
- Assign each chunk an ID and metadata such as category and language.
- Create an embedding for every chunk.
- Imagine a user asks: βHow do I reset my password?β
- Describe what the vector database should return as the top 3 results.
Test Your Understanding
Store and efficiently retrieve vectors and associated records using similarity search.
An identifier and metadata, plus content or a reference to the source content.
Return the k highest-ranked candidate results from the search.
No. It retrieves relevant information; the LLM can use that information to generate an answer.
They make nearest-neighbor search much more efficient on large vector collections.
30-Second Recap
Key Takeaways
- A vector database is designed to store and retrieve high-dimensional vectors efficiently.
- Embeddings represent the meaning or characteristics of content as vectors.
- Similarity search finds records whose vectors are close to a query vector.
- Metadata filtering can make retrieval more precise and safer.
- Indexes help vector search scale beyond naive comparisons.
- Vector databases are commonly used in semantic search, recommendations, and RAG.
- The database retrieves information; it does not replace the embedding model or LLM.