AI/ML7 min read

Vector Databases

Store and query high-dimensional embeddings with Pinecone, Weaviate, or pgvector. Build semantic search and RAG retrieval pipelines.

vector-databaseembeddingssemantic-searchai

Vector databases store high-dimensional embedding vectors and enable semantic similarity search. They are the retrieval backbone of RAG (Retrieval-Augmented Generation) systems, semantic search, recommendation engines, and duplicate detection at scale.

Choose pgvector for existing PostgreSQL systems (simplest operations). Choose Pinecone for managed, high-scale production. Choose Qdrant/Weaviate for self-hosted with advanced filtering. Generate embeddings once, store permanently, query many times.

Workflow

  1. Generate and normalize embeddingsThe quality of your embeddings determines the ceiling of your search quality; normalized vectors are required for cosine similarity.
  2. Set up pgvector for PostgreSQLpgvector adds vector search to existing PostgreSQL - no new infrastructure for teams already on Postgres.
  3. Use Pinecone for managed vector searchPinecone handles scaling, replication, and infrastructure - ideal for production with millions of vectors.
  4. Filter with metadataPure semantic search ignores business constraints; metadata filters combine semantic and structured search.
  5. Build a RAG retrieval pipelineRaw vector search retrieves chunks; RAG combines retrieval with generation for grounded, accurate answers.

Output Format

Embedding pipeline (batch + incremental), vector store with metadata, similarity search returning scored results, RAG pipeline with chunking, retrieval, and generation.

Examples

pgvector search: `SELECT id, content, 1 - (embedding <=> $1) AS similarity FROM documents WHERE metadata->>"tenant" = $2 ORDER BY embedding <=> $1 LIMIT 5`
Pinecone upsert: `await index.upsert(chunks.map(c => ({ id: c.id, values: c.embedding, metadata: { text: c.text, source: c.source, page: c.page } })))`
RAG prompt: `const context = results.map(r => r.metadata.text).join("

"); const prompt = `Context:
${context}

Question: ${question}
Answer based only on the context above.``

Edge Cases & Gotchas

  • Embedding model changes: if you switch models, you must re-embed the entire corpus (embeddings from different models are not comparable)
  • Chunking strategy matters: too small = lost context; too large = diluted relevance; experiment with 256-512 tokens with overlap for most prose content
  • Stale embeddings: when source content updates, re-embed and upsert - maintain a content hash to detect changes

When to Break These Rules

  • For exact keyword search (log analysis, code search), traditional full-text search (Elasticsearch, PostgreSQL tsvector) is faster and more precise than vector search.

Reference Files

  • pgvector: https://github.com/pgvector/pgvector
  • Pinecone docs: https://docs.pinecone.io/
  • Qdrant: https://qdrant.tech/documentation/
  • Chunking strategies: https://www.pinecone.io/learn/chunking-strategies/

More skills in this library

Browse all technical skills - Mobile, AI/ML, Web, Backend, Design, and Product.