RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Fredoline Eruo
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /Vector Stores and Embeddings
  6. /Ch. 9
Vector Stores and Embeddings

09. FAISS Index Types

Chapter 9 of 18 · 20 min
KEY INSIGHT

FAISS offers dozens of index types—flat indexes for accuracy, IVF indexes for speed, HNSW for memory-efficient approximate search. Choosing an index means trading off speed, memory, accuracy, and build time. ### IndexFlatL2 Exact nearest neighbor search using brute force. No approximation. ```python import faiss import numpy as np dimension = 384 index = faiss.IndexFlatL2(dimension) # Add 50,000 vectors vectors = np.random.random((50000, dimension)).astype('float32') index.add(vectors) # Query query = np.random.random((1, dimension)).astype('float32') k = 5 # Number of nearest neighbors distances, indices = index.search(query, k) print(f"Nearest indices: {indices}") print(f"Distances: {distances}") ``` Accurate but slow for large datasets. O(n) search time. ### IndexIVFFlat Inverted file index with clustering. Faster search by limiting candidates to nearby clusters. ```python import faiss dimension = 384 nlist = 100 # Number of clusters # Create quantizer (inner product index for L2) quantizer = faiss.IndexFlatL2(dimension) # nlist clusters, measure distance with L2 index = faiss.IndexIVFFlat(quantizer, dimension, nlist, faiss.METRIC_L2) # Must train before adding vectors training_vectors = np.random.random((10000, dimension)).astype('float32') index.train(training_vectors) # Now add vectors vectors = np.random.random((50000, dimension)).astype('float32') index.add(vectors) # Set nprobe (clusters to search) index.nprobe = 10 # Search 10 nearest clusters distances, indices = index.search(query, k) ``` IVF reduces search time from O(n) to O(n/nlist + nprobe * k). With 100 clusters and nprobe=10, you search roughly 10% of the data. ### IndexHNSWFlat Hierarchical Navigable Small World graph. Excellent speed with good accuracy. ```python import faiss dimension = 384 M = 32 # Number of connections per node index = faiss.IndexHNSWFlat(dimension, M, faiss.METRIC_L2) index.hnsw.efConstruction = 40 # Build-time quality index.hnsw.efSearch = 64 # Search-time quality vectors = np.random.random((50000, dimension)).astype('float32') index.add(vectors) distances, indices = index.search(query, k) ``` HNSW is typically the fastest for in-memory search but uses more RAM. Memory usage is approximately `dimension * ntotal * 4 bytes * (1 + M/2)`.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Compare search times for IndexFlatL2, IndexIVFFlat, and IndexHNSWFlat with 100,000 vectors. Measure query latency for each.

← Chapter 8
FAISS Installation
Chapter 10 →
IVF vs HNSW