Vector Databases for AI Cheat Sheet
Explains embeddings, approximate nearest neighbor search, and indexing strategies like HNSW, with code for storing and querying vectors using common libraries.
2 PagesIntermediateMar 20, 2026
Core Concepts
Foundations of vector search.
- Embedding- A dense numeric vector representing the semantic meaning of text, images, or other data, produced by a model
- Similarity metric- Cosine similarity, dot product, or Euclidean (L2) distance used to compare vectors; must match how the embedding model was trained
- Approximate Nearest Neighbor (ANN)- Trades a small amount of recall for large speedups over exact nearest-neighbor search at scale
- HNSW (Hierarchical Navigable Small World)- Graph-based ANN index offering strong recall/speed trade-offs; the most common index type in production vector DBs
- IVF (Inverted File Index)- Clusters vectors into partitions (via k-means) and searches only the nearest partitions, common in FAISS
- Metadata filtering- Combining vector similarity search with structured filters (e.g., date, category) in the same query
Local ANN Search with FAISS
Build and query an in-memory HNSW index.
python
import faissimport numpy as npdim = 384index = faiss.IndexHNSWFlat(dim, 32) # 32 = M, neighbors per nodeindex.hnsw.efConstruction = 200vectors = np.random.rand(10000, dim).astype('float32')index.add(vectors)query = np.random.rand(1, dim).astype('float32')distances, indices = index.search(query, k=5) # top-5 nearest neighbors
Storing and Querying with Chroma
A lightweight vector DB for embedding-based retrieval (e.g., RAG).
python
import chromadbclient = chromadb.Client()collection = client.create_collection("docs")collection.add( ids=["doc1", "doc2"], documents=["Paris is the capital of France.", "The Eiffel Tower is in Paris."], metadatas=[{"source": "wiki"}, {"source": "wiki"}],)results = collection.query(query_texts=["What city is the Eiffel Tower in?"], n_results=2)print(results["documents"])
Choosing a Vector Database
Key differentiators across common options.
- FAISS- Library, not a server; fastest for local/in-memory search but no built-in persistence, filtering, or multi-tenancy
- Chroma- Lightweight, easy local setup, popular for prototyping RAG applications
- Pinecone- Fully managed cloud service with metadata filtering, namespaces, and horizontal scaling built in
- pgvector- Postgres extension adding vector columns/indexes, useful when you want vectors alongside existing relational data
- Weaviate / Milvus / Qdrant- Self-hostable or managed vector DBs with hybrid (vector + keyword) search and filtering support
Pro Tip
Always use the same distance metric the embedding model was trained/normalized for (usually cosine similarity) -- mixing, say, an L2 index with cosine-normalized embeddings silently degrades retrieval quality without throwing an error.
Was this cheat sheet helpful?
Explore Topics
#VectorDatabasesForAI#VectorDatabasesForAICheatSheet#DataScience#Intermediate#CoreConcepts#Local#ANN#Search#Databases#MachineLearning#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance