AI & LLM
Updated for 2026

Vector Databases & Similarity Search Cheatsheet

Core operations, index creation, HNSW tuning, metadata filtering, and client syntax for Pinecone, Qdrant, Milvus, and Chroma.

Target Version Compatibility

Interactive Skill Mastery

Mark commands as learned to build your customized reference tracker. Retained locally in this browser.

Level:Novice
Command Mastery Progress0 of 17 Mastered (0%)

Pinecone

pc.create_index(name='index', dimension=1536, metric='cosine', spec=ServerlessSpec(cloud='aws', region='us-east-1'))
AdvancedPerformance
Creates a serverless Pinecone index optimized for OpenAI Ada-002 or text-embedding-3-small dimension sizes.

When to Use

When storing, indexing, or conducting high-performance nearest-neighbor search query sweeps on multi-dimensional floating point embeddings.

Common Mistakes

Failing to normalize vector coordinate arrays before running Dot Product metric queries, causing wild and inaccurate proximity scores.

Shortcut / Pro-Tip

Build indexes with Hierarchical Navigable Small World (HNSW) graphs and configure high efSearch parameters to maximize search recall.

Example

pc.create_index(name='index', dimension=1536, metric='cosine', spec=ServerlessSpec(cloud='aws', region='us-east-1'))

Output Example

Console / Terminal
High-dimensional spatial search index compiled and queried. Proximity distances and matched vectors retrieved.
index.upsert(vectors=[('id-1', [0.1, 0.2, ...], {'genre': 'comedy'})], namespace='ns1')
AdvancedPerformance
Upserts dense vector embeddings accompanied by structured metadata key-value payloads into isolated namespaces.

When to Use

When storing, indexing, or conducting high-performance nearest-neighbor search query sweeps on multi-dimensional floating point embeddings.

Common Mistakes

Failing to normalize vector coordinate arrays before running Dot Product metric queries, causing wild and inaccurate proximity scores.

Shortcut / Pro-Tip

Build indexes with Hierarchical Navigable Small World (HNSW) graphs and configure high efSearch parameters to maximize search recall.

Example

index.upsert(vectors=[('id-1', [0.1, 0.2, ...], {'genre': 'comedy'})], namespace='ns1')

Output Example

Console / Terminal
High-dimensional spatial search index compiled and queried. Proximity distances and matched vectors retrieved.
index.query(vector=[0.1, 0.2, ...], top_k=5, filter={'genre': 'comedy'}, include_metadata=True)
AdvancedPerformance
Queries a Pinecone index for top k nearest neighbors with metadata matching and vector coordinates.

When to Use

When storing, indexing, or conducting high-performance nearest-neighbor search query sweeps on multi-dimensional floating point embeddings.

Common Mistakes

Failing to normalize vector coordinate arrays before running Dot Product metric queries, causing wild and inaccurate proximity scores.

Shortcut / Pro-Tip

Build indexes with Hierarchical Navigable Small World (HNSW) graphs and configure high efSearch parameters to maximize search recall.

Example

index.query(vector=[0.1, 0.2, ...], top_k=5, filter={'genre': 'comedy'}, include_metadata=True)

Output Example

Console / Terminal
High-dimensional spatial search index compiled and queried. Proximity distances and matched vectors retrieved.
index.describe_index_stats()
AdvancedPerformance
Retrieves total vector counts, dimensionality, index fullness, and active namespaces configured in the Pinecone index.

When to Use

When storing, indexing, or conducting high-performance nearest-neighbor search query sweeps on multi-dimensional floating point embeddings.

Common Mistakes

Failing to normalize vector coordinate arrays before running Dot Product metric queries, causing wild and inaccurate proximity scores.

Shortcut / Pro-Tip

Build indexes with Hierarchical Navigable Small World (HNSW) graphs and configure high efSearch parameters to maximize search recall.

Example

index.describe_index_stats()

Output Example

Console / Terminal
High-dimensional spatial search index compiled and queried. Proximity distances and matched vectors retrieved.

Qdrant

client.create_collection(collection_name='col', vectors_config=VectorParams(size=1536, distance=Distance.COSINE))
BeginnerBasics
Initializes a high-performance vector collection in Qdrant with defined dimension sizes and cosine distance metrics.

When to Use

When storing, indexing, or conducting high-performance nearest-neighbor search query sweeps on multi-dimensional floating point embeddings.

Common Mistakes

Failing to normalize vector coordinate arrays before running Dot Product metric queries, causing wild and inaccurate proximity scores.

Shortcut / Pro-Tip

Build indexes with Hierarchical Navigable Small World (HNSW) graphs and configure high efSearch parameters to maximize search recall.

Example

client.create_collection(collection_name='col', vectors_config=VectorParams(size=1536, distance=Distance.COSINE))

Output Example

Console / Terminal
High-dimensional spatial search index compiled and queried. Proximity distances and matched vectors retrieved.
client.search(collection_name='col', query_vector=[0.1, ...], query_filter=Filter(must=[FieldCondition(key='genre', match=MatchValue(value='comedy'))]), limit=5)
IntermediateAdvanced
Searches Qdrant with pre-filtering constraints, ensuring results match structured conditions before distance calculations.

When to Use

When storing, indexing, or conducting high-performance nearest-neighbor search query sweeps on multi-dimensional floating point embeddings.

Common Mistakes

Failing to normalize vector coordinate arrays before running Dot Product metric queries, causing wild and inaccurate proximity scores.

Shortcut / Pro-Tip

Build indexes with Hierarchical Navigable Small World (HNSW) graphs and configure high efSearch parameters to maximize search recall.

Example

client.search(collection_name='col', query_vector=[0.1, ...], query_filter=Filter(must=[FieldCondition(key='genre', match=MatchValue(value='comedy'))]), limit=5)

Output Example

Console / Terminal
High-dimensional spatial search index compiled and queried. Proximity distances and matched vectors retrieved.
client.upsert(collection_name='col', points=[PointStruct(id=1, vector=[0.1, ...], payload={'role': 'user'})])
BeginnerBasics
Inserts or updates vector points with structured payload variables into an active Qdrant collection.

When to Use

When storing, indexing, or conducting high-performance nearest-neighbor search query sweeps on multi-dimensional floating point embeddings.

Common Mistakes

Failing to normalize vector coordinate arrays before running Dot Product metric queries, causing wild and inaccurate proximity scores.

Shortcut / Pro-Tip

Build indexes with Hierarchical Navigable Small World (HNSW) graphs and configure high efSearch parameters to maximize search recall.

Example

client.upsert(collection_name='col', points=[PointStruct(id=1, vector=[0.1, ...], payload={'role': 'user'})])

Output Example

Console / Terminal
High-dimensional spatial search index compiled and queried. Proximity distances and matched vectors retrieved.

Milvus

connections.connect(alias='default', host='localhost', port='19530')
BeginnerBasics
Establishes a connection to a local or remote Milvus server instance using the PyMilvus client library.

When to Use

When storing, indexing, or conducting high-performance nearest-neighbor search query sweeps on multi-dimensional floating point embeddings.

Common Mistakes

Failing to normalize vector coordinate arrays before running Dot Product metric queries, causing wild and inaccurate proximity scores.

Shortcut / Pro-Tip

Build indexes with Hierarchical Navigable Small World (HNSW) graphs and configure high efSearch parameters to maximize search recall.

Example

connections.connect(alias='default', host='localhost', port='19530')

Output Example

Console / Terminal
High-dimensional spatial search index compiled and queried. Proximity distances and matched vectors retrieved.
schema.add_field(field_name='vector', datatype=DataType.FLOAT_VECTOR, dim=1536)
BeginnerBasics
Configures Milvus collection schema field containing floating point vector embeddings with defined dimensionality.

When to Use

When storing, indexing, or conducting high-performance nearest-neighbor search query sweeps on multi-dimensional floating point embeddings.

Common Mistakes

Failing to normalize vector coordinate arrays before running Dot Product metric queries, causing wild and inaccurate proximity scores.

Shortcut / Pro-Tip

Build indexes with Hierarchical Navigable Small World (HNSW) graphs and configure high efSearch parameters to maximize search recall.

Example

schema.add_field(field_name='vector', datatype=DataType.FLOAT_VECTOR, dim=1536)

Output Example

Console / Terminal
High-dimensional spatial search index compiled and queried. Proximity distances and matched vectors retrieved.
index_params.add_index(field_name='vector', index_type='HNSW', metric_type='COSINE', params={'M': 16, 'efConstruction': 200})
AdvancedPerformance
Constructs a highly optimized Hierarchical Navigable Small World (HNSW) spatial search graph inside Milvus.

When to Use

When storing, indexing, or conducting high-performance nearest-neighbor search query sweeps on multi-dimensional floating point embeddings.

Common Mistakes

Failing to normalize vector coordinate arrays before running Dot Product metric queries, causing wild and inaccurate proximity scores.

Shortcut / Pro-Tip

Build indexes with Hierarchical Navigable Small World (HNSW) graphs and configure high efSearch parameters to maximize search recall.

Example

index_params.add_index(field_name='vector', index_type='HNSW', metric_type='COSINE', params={'M': 16, 'efConstruction': 200})

Output Example

Console / Terminal
High-dimensional spatial search index compiled and queried. Proximity distances and matched vectors retrieved.

Chroma DB

collection = chroma_client.get_or_create_collection(name='col', embedding_function=openai_ef)
BeginnerBasics
Gets or instantiates a local in-memory Chroma DB collection using a custom integrated embedding function provider.

When to Use

When storing, indexing, or conducting high-performance nearest-neighbor search query sweeps on multi-dimensional floating point embeddings.

Common Mistakes

Failing to normalize vector coordinate arrays before running Dot Product metric queries, causing wild and inaccurate proximity scores.

Shortcut / Pro-Tip

Build indexes with Hierarchical Navigable Small World (HNSW) graphs and configure high efSearch parameters to maximize search recall.

Example

collection = chroma_client.get_or_create_collection(name='col', embedding_function=openai_ef)

Output Example

Console / Terminal
High-dimensional spatial search index compiled and queried. Proximity distances and matched vectors retrieved.
collection.query(query_embeddings=[[0.1, ...]], n_results=5, where={'genre': 'comedy'})
BeginnerBasics
Queries Chroma collection with nested conditions, returning matched documents, metadata, and calculated distances.

When to Use

When storing, indexing, or conducting high-performance nearest-neighbor search query sweeps on multi-dimensional floating point embeddings.

Common Mistakes

Failing to normalize vector coordinate arrays before running Dot Product metric queries, causing wild and inaccurate proximity scores.

Shortcut / Pro-Tip

Build indexes with Hierarchical Navigable Small World (HNSW) graphs and configure high efSearch parameters to maximize search recall.

Example

collection.query(query_embeddings=[[0.1, ...]], n_results=5, where={'genre': 'comedy'})

Output Example

Console / Terminal
High-dimensional spatial search index compiled and queried. Proximity distances and matched vectors retrieved.
chroma_client.heartbeat()
BeginnerBasics
Sends a ping to verify client-server connectivity and ensure the Chroma DB database backend is active and responsive.

When to Use

When storing, indexing, or conducting high-performance nearest-neighbor search query sweeps on multi-dimensional floating point embeddings.

Common Mistakes

Failing to normalize vector coordinate arrays before running Dot Product metric queries, causing wild and inaccurate proximity scores.

Shortcut / Pro-Tip

Build indexes with Hierarchical Navigable Small World (HNSW) graphs and configure high efSearch parameters to maximize search recall.

Example

chroma_client.heartbeat()

Output Example

Console / Terminal
High-dimensional spatial search index compiled and queried. Proximity distances and matched vectors retrieved.

Indexing & Metrics

Distance Formulas: Cosine: 1 - (A · B) / (||A|| ||B||) | L2 (Euclidean): ||A - B||^2 | Dot Product: A · B
BeginnerBasics
Key distance and similarity formulas used for mapping spatial distances in high-dimensional vector spaces.

When to Use

When storing, indexing, or conducting high-performance nearest-neighbor search query sweeps on multi-dimensional floating point embeddings.

Common Mistakes

Failing to normalize vector coordinate arrays before running Dot Product metric queries, causing wild and inaccurate proximity scores.

Shortcut / Pro-Tip

Build indexes with Hierarchical Navigable Small World (HNSW) graphs and configure high efSearch parameters to maximize search recall.

Example

Distance Formulas: Cosine: 1 - (A · B) / (||A|| ||B||) | L2 (Euclidean): ||A - B||^2 | Dot Product: A · B

Output Example

Console / Terminal
High-dimensional spatial search index compiled and queried. Proximity distances and matched vectors retrieved.
HNSW Tuning: M (outgoing links: 4-64) | efConstruction (build speed/depth: 100-500) | efSearch (query search depth: 16-128)
BeginnerBasics
Crucial HNSW hyper-parameters balancing indexing time, memory footprint, recall accuracy, and search queries per second (QPS).

When to Use

When storing, indexing, or conducting high-performance nearest-neighbor search query sweeps on multi-dimensional floating point embeddings.

Common Mistakes

Failing to normalize vector coordinate arrays before running Dot Product metric queries, causing wild and inaccurate proximity scores.

Shortcut / Pro-Tip

Build indexes with Hierarchical Navigable Small World (HNSW) graphs and configure high efSearch parameters to maximize search recall.

Example

HNSW Tuning: M (outgoing links: 4-64) | efConstruction (build speed/depth: 100-500) | efSearch (query search depth: 16-128)

Output Example

Console / Terminal
High-dimensional spatial search index compiled and queried. Proximity distances and matched vectors retrieved.
IVF_FLAT Indexing: nlist (number of clusters: 256-65536) | nprobe (search cluster depth: 1-512)
AdvancedPerformance
Inverted File Index (IVF_FLAT) partition parameters dividing vector space into clusters to avoid brute-force scanning.

When to Use

When storing, indexing, or conducting high-performance nearest-neighbor search query sweeps on multi-dimensional floating point embeddings.

Common Mistakes

Failing to normalize vector coordinate arrays before running Dot Product metric queries, causing wild and inaccurate proximity scores.

Shortcut / Pro-Tip

Build indexes with Hierarchical Navigable Small World (HNSW) graphs and configure high efSearch parameters to maximize search recall.

Example

IVF_FLAT Indexing: nlist (number of clusters: 256-65536) | nprobe (search cluster depth: 1-512)

Output Example

Console / Terminal
High-dimensional spatial search index compiled and queried. Proximity distances and matched vectors retrieved.
Scalar Quantization (SQ8): Reduces 32-bit floats to 8-bit integers
AdvancedPerformance
Compresses embedding dimensions by mapping floating point intervals to a compact 1-byte representation, slashing RAM use by 75%.

When to Use

When storing, indexing, or conducting high-performance nearest-neighbor search query sweeps on multi-dimensional floating point embeddings.

Common Mistakes

Failing to normalize vector coordinate arrays before running Dot Product metric queries, causing wild and inaccurate proximity scores.

Shortcut / Pro-Tip

Build indexes with Hierarchical Navigable Small World (HNSW) graphs and configure high efSearch parameters to maximize search recall.

Example

Scalar Quantization (SQ8): Reduces 32-bit floats to 8-bit integers

Output Example

Console / Terminal
High-dimensional spatial search index compiled and queried. Proximity distances and matched vectors retrieved.

Vector Databases Best Practices

1Select Distance Metrics carefully

Align your database distance calculations with your embedding models. Use Cosine Similarity for normalized vectors, Dot Product for maximum performance, and L2 (Euclidean) for clustering applications.

2Build Indexes with HNSW Graphs

For low-latency, high-QPS applications, index collections using Hierarchical Navigable Small World (HNSW) graphs. Adjust efConstruction and M to fine-tune index build speed vs. search recall.

3Optimize with Pre-Filtering Constraints

Configure metadata schemas and indexes to execute metadata filters *before* the vector search calculation (pre-filtering), avoiding precision loss and random walks.

4Partition Collections using Namespaces

Establish isolated namespaces or partitions within a single index to separate multi-tenant data, preventing cross-tenant document leakage.

5Monitor Vector Dimension sizes

Match index configurations exactly to your generator models (e.g. 1536 for OpenAI text-embedding-3-small, 3072 for text-embedding-3-large) to prevent dimensional mismatches on upsert.

Common Vector Databases Errors & Solutions

Error

Dimensional mismatch exception on vector upsert

Solution

Verify that the dimension parameter defined during index creation matches your embedding model output (e.g., Ada-002 outputting 1536, Cohere v3 outputting 1024).

Error

Slow query execution times (high latency) under peak load

Solution

The index is likely running out of RAM or searching too deep. Lower your efSearch search parameter, scale up database memory allocations, or switch to Quantized vector storage.

Error

Post-filtering returning zero results on metadata queries

Solution

Filtering *after* vector search (post-filtering) discards matched nearest neighbors that fail metadata checks. Switch to strict database-level 'pre-filtering' to search only valid records.

Error

Memory consumption skyrocketing on HNSW indexes

Solution

HNSW stores full graph links in memory. Reduce the graph connectivity parameter 'M' (e.g., to 12 or 16) or apply Scalar Quantization (SQ) or Product Quantization (PQ) to compress vectors.

Error

Inaccurate distance scores on dot product calculations

Solution

Dot product only matches cosine similarity if vector coordinates are normalized. Ensure all input vectors are L2-normalized on generation, or switch the database metric to Cosine.

Common Vector Databases Interview Questions

Q1How does Hierarchical Navigable Small World (HNSW) enable sub-linear vector search?

HNSW is a graph-based indexing algorithm that creates a multi-layered skip-list of vectors. Upper layers contain long-range connections for fast, coarse spatial jumps, while lower layers contain dense, short-range connections for granular navigation. Search traverses top-down, finding the local neighborhood in logarithmic time.

Q2What is Product Quantization (PQ) and how does it save memory?

Product Quantization is a compression technique that splits high-dimensional vectors into smaller sub-vectors, runs K-means clustering on these sub-vectors to define centroids, and replaces each sub-vector with a short 1-byte centroid ID index. This can reduce memory footprint by 95% at the cost of minor recall accuracy loss.

Q3Explain the difference between Pre-Filtering, Post-Filtering, and Single-Stage Filtering in Vector Search.

Post-Filtering searches vectors first and discards results that do not match metadata, often returning fewer results than requested. Pre-Filtering filters records by metadata first and searches vectors only among matched rows, which is slow if matches are huge. Single-Stage (Iterative) Filtering traverses the index graph while actively checking metadata on every node, balancing speed and accuracy.

Q4Why is Cosine Similarity identical to Dot Product for normalized vectors?

Cosine similarity is mathematically defined as the dot product of two vectors divided by the product of their magnitudes: (A · B) / (||A|| ||B||). If vectors are L2-normalized, their magnitudes are exactly 1, reducing the formula to a simple dot product (A · B), which bypasses expensive square root divisions.

Q5What are the trade-offs between Flat (Brute-Force) Indexing and Approximate Nearest Neighbor (ANN) Indexing?

Flat Indexing computes exact distances against every vector in the database, ensuring 100% recall accuracy but scaling linearly O(N), which is too slow for large datasets. ANN indexing (using HNSW, IVF, or ScaNN) trades minor recall accuracy (typically 95-99%) for constant or logarithmic search latency O(log N), making it highly scalable.