Practical: FAISS for Fast Search

Harry · 13 Sep 2026 · 2 views

What Is FAISS

FAISS is Facebook AI Similarity Search, a library of vector indexes that runs directly on your machine and handles billions of vectors with CPU and GPU support.

Build Your First Index

pip install faiss-cpu

import faiss
import numpy as np

d = 384
rng = np.random.default_rng(42)
xb = rng.random((10000, d)).astype("float32")
index = faiss.IndexFlatIP(d)   # inner product
index.add(xb)
print(index.ntotal)            # 10000

q = rng.random((1, d)).astype("float32")
scores, labels = index.search(q, k=5)
print(labels)

Inner product finds large dot products. For cosine behaviour, normalize vectors first.

HNSW for Speed

hnsw = faiss.IndexHNSWFlat(d, 32)
hnsw.add(xb)
scores, labels = hnsw.search(q, k=5)
print(labels)

Persisting an Index

faiss.write_index(hnsw, "hnsw.index")
loaded = faiss.read_index("hnsw.index")
print(loaded.ntotal)

GPU and Ops Notes

FAISS has no built-in metadata filtering or API server; pair it with your own storage layer, or use FAISS only for the index and keep metadata in SQL. On GPU, indexes move with index_gpu_to_cpu and friends.

Key Points

  • FAISS indexes live in your process; data never leaves the box.
  • IndexFlatIP is exact; HNSW is approximate but faster.
  • Normalize before using inner product as cosine.
  • write_index and read_index persist indexes.
Share this post:

Comments (0)

Please login or register to comment.