Most search systems built on embedding vectors search a large collection for several vectors close to a given vector. Faiss is Meta's library for approximate nearest-neighbor (ANN) search. Many search engines and vector databases, including txtai and Chroma, depend on libraries of this kind. I installed Faiss and checked whether nearest-neighbor search worked with networking disabled.
Faiss searches large collections of high-dimensional vectors for those closest to a specified vector. Meta publishes Faiss under the MIT License (*1).
1. Install Faiss
FROM docker.io/library/python:3.12.13-slim
RUN pip install --no-cache-dir --quiet 'faiss-cpu==1.14.3' 'numpy==2.5.1'
WORKDIR /work
I used no external models, such as embedding models, so the test required no pretrained model download.
podman build -t faiss-sandbox .
2. Find the correct neighbors across three clusters
I generated 50 32-dimensional vectors around each of three centroids, for 150 vectors total, then searched with a query vector near one cluster. I ran the test with networking disabled.
import numpy as np
import faiss
rng = np.random.default_rng(0)
dim = 32
cluster_centers = rng.normal(0, 10, size=(3, dim))
vectors = np.vstack([
cluster_centers[i] + rng.normal(0, 0.5, size=(50, dim))
for i in range(3)
]).astype("float32")
labels = np.repeat([0, 1, 2], 50)
index = faiss.IndexFlatL2(dim)
index.add(vectors)
query = (cluster_centers[1] + rng.normal(0, 0.5, size=dim)).astype("float32").reshape(1, -1)
distances, indices = index.search(query, k=5)
print("nearest 5 neighbors' cluster labels:", labels[indices[0]].tolist())
podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
-v "$(pwd)":/work -w /work faiss-sandbox \
python search_demo.py
-v makes the specified directory readable and writable from the container. Run this in an empty directory reserved for the experiment.
Faiss returned:
total vectors indexed: 150
nearest 5 neighbors' cluster labels: [1, 1, 1, 1, 1]
distances: [10.49, 11.06, 11.71, 11.72, 12.03]
Faiss selected all five nearest neighbors from the correct cluster, cluster 1, among the 150 vectors. No vector from either of the other two clusters appeared. At this size, a brute-force NumPy scan finishes in an instant. I measured Faiss at larger scales next.
3. Measure speed at larger scales
For 10,000, 100,000, and 1,000,000 32-dimensional vectors, I compared the time per query for IndexFlatL2 (exact search) with a basic brute-force NumPy scan. I averaged 100 queries under the same two-core CPU limit.
index = faiss.IndexFlatL2(dim)
index.add(vectors)
t0 = time.perf_counter()
index.search(queries, k) # faiss
faiss_ms = (time.perf_counter() - t0) * 1000 / n_queries
for q in queries: # numpy brute force
d = ((vectors - q) ** 2).sum(axis=1)
np.argpartition(d, k)[:k]
Use the section 2 command with bench_scale.py for this section and bench_index_types.py for the next.
vectors faiss (ms/query) numpy (ms/query)
10,000 0.305 1.175
100,000 1.395 11.308
1,000,000 13.732 205.562
With 1,000,000 vectors, Faiss took 13.7 milliseconds per query and the NumPy brute-force scan took 205.6 milliseconds, a gap of about 15x even with exact search. At 10,000 vectors, the gap was about 4x, so Faiss becomes more useful as the dataset grows. A 1,000,000 × 32 float32 array occupies 128 MB in memory, and this test completed inside a container capped at 2 GB of memory.
4. Measure the speed and recall tradeoff of approximate indexes
Alongside exact search, Faiss offers approximate indexes that trade some accuracy for speed. For 100,000 vectors, I treated the exact-search results as the ground truth and measured recall@5 for IVFFlat and HNSWFlat, the share of the top five results that matched the ground truth.
index type build ms/query recall@5
IndexFlatL2 (exact) 0.05s 1.582ms 100.0%
IVFFlat (nlist=100,nprobe=10) 5.35s 0.828ms 65.4%
HNSWFlat (M=32) 12.37s 0.072ms 63.6%
HNSW ran 22 times as fast as exact search at 0.072ms; recall fell to 63.6%. IVFFlat stayed in the 60% range. I used uniform random vectors with no structure, the least favorable input for approximate indexes. Recall tends to be higher on real embedding vectors because they have semantic cluster structure. I measured the tradeoff: approximate indexes deliver more speed at an accuracy cost, and the cost depends on the data. HNSW took 12.37s to build, versus 0.05s for exact search.
Looking inside the search engine
You see little of the internal work when you use tools such as txtai or Chroma. I ran Faiss on its own, measured scale and accuracy, and saw two core search-engine choices: exact or approximate search, and the balance between speed and recall. Run Faiss on its own for a single test, and you can verify the choices a higher-level tool abstracts away.
Sources
*1 Faiss (GitHub), https://github.com/facebookresearch/faiss, MIT License