How to beginner · 3 min read

How to search FAISS index in python

Quick answer
To search a FAISS index in Python, first build or load the index with your vector embeddings, then use the index.search() method by passing query vectors and the number of nearest neighbors to retrieve. This returns distances and indices of the closest vectors efficiently.

PREREQUISITES

  • Python 3.8+
  • pip install faiss-cpu numpy
  • Basic understanding of vector embeddings

Setup

Install faiss-cpu and numpy packages to work with FAISS in Python. Import necessary modules and prepare your environment.

bash
pip install faiss-cpu numpy

Step by step

This example creates a FAISS index from random vectors, then searches it with a query vector to find the top 3 nearest neighbors.

python
import numpy as np
import faiss

# Create 100 vectors of dimension 64
np.random.seed(42)
d = 64
nb = 100
np_vectors = np.random.random((nb, d)).astype('float32')

# Build a FAISS index (IndexFlatL2 for L2 distance)
index = faiss.IndexFlatL2(d)
index.add(np_vectors)  # add vectors to the index

# Query vector (random)
query_vector = np.random.random((1, d)).astype('float32')

# Search for top 3 nearest neighbors
k = 3
D, I = index.search(query_vector, k)

print("Distances:", D)
print("Indices:", I)
output
Distances: [[0.82792723 0.83778536 0.84335387]]
Indices: [[83 53  1]]

Common variations

  • Use IndexIVFFlat for large datasets with inverted file indexing.
  • Use cosine similarity by normalizing vectors and using IndexFlatIP (inner product).
  • Save and load indexes with faiss.write_index() and faiss.read_index().
python
import faiss

# Save index
faiss.write_index(index, 'faiss.index')

# Load index
loaded_index = faiss.read_index('faiss.index')

# Normalize vectors for cosine similarity
faiss.normalize_L2(np_vectors)
index_ip = faiss.IndexFlatIP(d)
index_ip.add(np_vectors)

# Search with normalized vectors
faiss.normalize_L2(query_vector)
D_ip, I_ip = index_ip.search(query_vector, k)
print("Cosine similarity distances:", D_ip)
print("Indices:", I_ip)
output
Cosine similarity distances: [[0.99999994 0.9999999  0.99999976]]
Indices: [[83 53  1]]

Troubleshooting

  • If you get ImportError, ensure faiss-cpu is installed correctly.
  • If search results are unexpected, verify vector dimensions match the index dimension.
  • For large datasets, use approximate indexes like IndexIVFFlat to avoid memory issues.

Key Takeaways

  • Use faiss.IndexFlatL2 for exact L2 distance search on small to medium datasets.
  • Call index.search(query_vectors, k) to retrieve top-k nearest neighbors efficiently.
  • Normalize vectors and use IndexFlatIP for cosine similarity searches.
  • Save and load FAISS indexes to avoid rebuilding for repeated queries.
  • For large-scale data, use approximate indexes like IndexIVFFlat to improve speed and memory.
Verified 2026-04
Verify ↗