Embeddings and Vector Search
What is an embedding?
An embedding is a list of numbers (a vector, often 256-3072 dimensions) that represents the meaning of text. Texts with similar meaning get vectors that point in similar directions, so "refund kab milega" and "when will I get my money back" end up close together even with no shared words. Claude has no embeddings endpoint; common choices are Voyage AI, OpenAI and Gemini embeddings, or open-source sentence-transformers models.
Cosine similarity vs dot product vs Euclidean distance?
| Measure | What it compares | Note |
|---|---|---|
| Cosine similarity | angle between vectors (-1 to 1) | ignores length; the default for text |
| Dot product | angle and length | same as cosine when vectors are normalised (most embedding APIs normalise) |
| Euclidean (L2) | straight-line distance | smaller = closer; same ranking as cosine for normalised vectors |
What is approximate nearest neighbour (ANN) search, and what is HNSW?
Exact search compares the query with every vector: fine for 10,000, too slow for 100 million. ANN indexes trade a little accuracy for big speed gains. HNSW (Hierarchical Navigable Small World) builds a layered graph: you start at a sparse top layer, greedily move toward the query, and drop to denser layers. Parameters like ef_search trade recall for speed. IVF (clusters) and product quantisation (compression) are other common methods.
How do you choose a vector database?
| Option | Pick it when |
|---|---|
| pgvector (PostgreSQL) | you already use Postgres; up to a few million vectors; want SQL joins and filters |
| Chroma | prototypes and local development |
| Qdrant / Weaviate / Milvus | large scale, rich filtering, hybrid search, self-host or cloud |
| Pinecone | fully managed, no operations work |
| FAISS | an in-process library for fast search, no database features |
Tip: Mention metadata filters (tenant, language, category) in your answer: they are needed for security and relevance.