Mastering Similarity Searches: Building a Faiss Index with Cosine Similarity

Gülsüm Budakoğlu
11 min readOct 28, 2023

Learn how to create a faiss index and use the strength of cosine similarity to find cosine similarity score.

Photo by Markus Winkler on Unsplash

Faiss is a library for efficient similarity search which was released by Facebook AI. For more technically details about faiss, you can check the article here .

In Faiss, there are different metrics to create similarity score. There’s a handy tool called the ‘MetricType’ that helps you measure the similarity between things. Think of it like a ruler, but for comparing data. You have options like ‘METRIC_INNER_PRODUCT’ for finding the cosine similarity, ‘METRIC_L2’ for measuring euclidian distance, and ‘METRIC_L1’ for a different kind of distance. There’s also ‘METRIC_Linf’ for a specific type of distance and ‘METRIC_Lp,’ where you can pick the ‘p’ value yourself. In addition, Faiss offers some more special rulers like ‘METRIC_Canberra,’ ‘METRIC_BrayCurtis,’ ‘METRIC_JensenShannon,’ and ‘METRIC_Jaccard’ for unique situations. So, these different rulers give you the power to find similarities or differences in your data the way you need, making your searches more accurate and efficient. Faiss’s ‘MetricType’ is like a toolbox of measuring tools to help you with your data. For more detailed information about metric types, you can visit the faiss GitHub page.

--

--