// LOADING
// LOADING
// LOADING_ARTICLE
Traditional databases store and retrieve data by exact match or range comparison. A WHERE name = 'foo' query works because equality is well-defined. But meaning is not equal or unequal—it is similar or dissimilar in continuous degrees. Vector databases are purpose-built to query by similarity, making them the storage layer for semantic search, recommendation systems, and retrieval-augmented generation.
An embedding model converts text (or an image, audio clip, or any other input) into a dense numeric vector—an array of typically hundreds or thousands of floating-point numbers. The model is trained such that inputs with similar meaning map to vectors that are close together in the high-dimensional space.
For example, the sentences "the dog ran across the park" and "a canine sprinted through the garden" should produce vectors that are close to each other, while "federal interest rate policy" should produce a vector far from both.
For a deeper grounding in how embeddings work and how to generate them, see Embeddings: The Foundation of AI Search.
Vector databases measure closeness using one of a few distance or similarity functions:
For text search, cosine similarity or dot product on L2-normalized embeddings is the standard.
A naive similarity search scans every stored vector and computes distance to the query vector. At millions of vectors, this is too slow for interactive applications. Vector databases use Approximate Nearest Neighbor (ANN) algorithms to trade a small amount of accuracy for orders-of-magnitude speed.
The dominant ANN algorithms:
In practice you tune two parameters: ef_construction (quality during index build) and ef_search (quality vs. speed at query time).
Pure vector search retrieves the most semantically similar items globally. Production applications typically need filtered search: "find the most similar product description, but only within category X and published after 2024."
Vector databases handle this with metadata filters that run alongside the ANN search. The implementation matters:
Semantic search misses exact matches. If a user searches for a product SKU or a specific error code, a semantic model may not retrieve the right document because the embedding encodes meaning, not spelling. Hybrid search combines vector similarity with a keyword (BM25) index and blends their scores.
ts// Pseudocode — most managed vector DBs expose a hybrid search parameter const results = await vectorDB.search({ query: userQuery, vector: queryEmbedding, hybridAlpha: 0.5, // 0 = pure keyword, 1 = pure vector topK: 10, });
Hybrid search is a significant quality improvement for RAG pipelines. See Retrieval-Augmented Generation (RAG), Explained for how retrieval quality affects the full pipeline.
For applications with fewer than a few million vectors and existing PostgreSQL infrastructure, pgvector is often the pragmatic choice. You get HNSW and IVF indexes, metadata filtering via standard SQL WHERE clauses, and transactional consistency for free.
ts// pgvector query via Prisma raw SQL const results = await prisma.$queryRaw<{ id: string; content: string }[]>` SELECT id, content FROM documents WHERE category = 'support' ORDER BY embedding <=> ${queryVector}::vector LIMIT 5 `;
The <=> operator is cosine distance. <#> is inner product (dot product negated), <-> is L2.
How you structure your vector index affects retrieval quality substantially:
Once retrieval is working, passing the top-k chunks to a generation model is straightforward:
tsimport Anthropic from "@anthropic-ai/sdk"; const client = new Anthropic(); async function answerWithContext( userQuery: string, chunks: string[] ): Promise<string> { const context = chunks.join("\n\n"); const message = await client.messages.create({ model: "claude-opus-4-8", max_tokens: 1024, system: "Answer using only the provided context. Be concise.", messages: [ { role: "user", content: `Context:\n${context}\n\nQuestion: ${userQuery}`, }, ], }); const block = message.content[0]; if (block.type !== "text") throw new Error("Unexpected content"); return block.text; }
For the full picture of building a Claude-powered search feature, see Building Your First AI Feature with the Claude API and Adding AI-Powered Search to Your Blog with Embeddings.
Vector databases bring semantic understanding to storage and retrieval. The core concept—embed once, query by similarity—is simple, but the engineering details (ANN algorithms, metadata filtering, hybrid search, index structure) determine whether a search feature feels precise or frustratingly random. Start with pgvector if you are already on PostgreSQL, graduate to a dedicated vector store as scale demands, and invest in retrieval quality before tuning the generation layer. See the projects page for applications built with these patterns.