What is FAISS? (original) (raw)

Last Updated : 15 Apr, 2026

FAISS (Facebook AI Similarity Search) is an open-source library developed by Meta for efficient similarity search and clustering of dense vectors. It is designed to handle datasets ranging from a few million to over a billion high-dimensional vectors, making it a backbone for modern recommendation systems, search engines and AI applications.

search_query

Working of FAISS

With the explosion of data in fields like e-commerce, computer vision and NLP, the need to quickly find similar items in massive, high-dimensional datasets is crucial. FAISS addresses this challenge by providing highly optimized algorithms and data structures for nearest neighbor search and clustering. It uses both CPUs and GPUs for maximum performance.

IndexFlatL2 is the main Indexing Approaches in FAISS. It stores all vectors in memory and performs a brute-force search to find the nearest neighbors using the Euclidean distance. It is best for small to medium datasets where exact results are required and computational resources are sufficient. It helps in:

But it is not scalable for very large datasets like hundreds of millions or billions of vectors due to high memory and computation requirements.

Now lets see how to use FAISS for our projects.

1. Installing FAISS and using Dataset

You can install FAISS library using following command:

Python `

!pip install faiss-cpu

`

Now lets create a database of 100,000 random 128-dimensional vectors and builds a FAISS index for efficient similarity search. It then finds the top 5 nearest neighbors from the database for each of 10 random query vectors using Euclidean distance where:

import faiss import numpy as np

d = 128 nb = 100000 nq = 10

xb = np.random.random((nb, d)).astype('float32') xq = np.random.random((nq, d)).astype('float32')

`

2. Building Index

index = faiss.IndexFlatL2(d)

`

3. Add Vectors to the Index

index.add(xb)

`

4. Search for Nearest Neighbors

D, I = index.search(xq, k=5)

`

5. Displaying Search Results

Python `

print("Distances (D):") display(D) print("\nIndices (I):") display(I)

`

**Output:

FAISS-Output

FAISS OUTPUT