Vector Databases & Search

📌 Why do we need a special database?

Traditional relational databases (like MySQL or PostgreSQL) store data in neat rows and columns. They are highly optimized for exact keyword matching, like executing a query that says: WHERE customer_name = 'John Smith'.

Diagram showing a traditional table database vs a 3D vector space database
Diagram showing a traditional table database vs a 3D vector space database

However, an AI doesn't search for exact keywords; it searches for meaning. A Vector Database (like Pinecone, Weaviate, Milvus, or pgvector) is purpose-built to store, index, and query high-dimensional arrays of decimal numbers. They are optimized to perform complex geometry at lightning speed.

📌 The Two Types of Vector Search

Once your documents are embedded as vectors inside the database, how does the database quickly find the closest match to a user's question? There are two primary algorithms:

Diagram showing k-NN checking every point vs ANN checking only a clustered neighborhood
Diagram showing k-NN checking every point vs ANN checking only a clustered neighborhood

1. k-Nearest Neighbors (k-NN)

This is the "brute force" method. The database calculates the exact distance (e.g., Cosine Similarity) between the user's query vector and every single vector in the database. It then returns the top 'k' closest matches. It is 100% accurate, but if you have a billion vectors, checking every single one takes far too long.

2. Approximate Nearest Neighbors (ANN)

This is the industry standard for production apps. Instead of checking every vector, the database preemptively organizes the vectors by grouping similar ones into "neighborhoods" (clusters). When a query comes in, the database quickly figures out which neighborhood the query belongs in, and only searches inside that specific neighborhood.

You trade a tiny fraction of a percent of accuracy for massive speed improvements, allowing you to search millions of vectors in milliseconds.

📌 The Full Semantic Search Flow

Let's put it all together to see how an AI-powered search bar actually works behind the scenes:

  1. The user types a query into your app: "How to fix a leaky pipe."
  2. Your app sends that exact text to an Embedding API (like OpenAI's).
  3. The API responds with the query's mathematical meaning: [0.44, -0.11, 0.89...].
  4. Your app sends that vector to your Vector Database and asks for the 3 closest matches.
  5. The Vector Database uses an ANN algorithm to instantly locate the closest vectors in the coordinate space.
  6. The Database returns the original text attached to those vectors—which might be an article titled "Plumbing repair for dripping faucets".

Notice that the search succeeded perfectly even though the user's query didn't contain the words "plumbing", "repair", "dripping", or "faucets"! This is the power of semantic search.