facebookresearch / faiss

A library for efficient similarity search and clustering of dense vectors.
https://faiss.ai
MIT License
29.68k stars 3.5k forks source link

Documentation/Instructions on L1 metric #3458

Open ZeyuSun opened 2 months ago

ZeyuSun commented 2 months ago

I am using Python and I haven't been able to figure out how to use L1 metric to search. The closest document I found is introducing MetricType. Would anyone point me to the documentation on how to change to other metrics?

mlomeli1 commented 2 months ago

hi @ZeyuSun , thank you for flagging I think we should add this to the wiki, so I am marking this issue as documentation. I have created a toy example as well for your reference:

import faiss
import numpy as np

dimension = 128
n = 10000
db_vectors = np.random.random((n, dimension)).astype("float32")
k = 3
dimension = 128
query_vectors = np.random.random((n, dimension)).astype("float32")
code_size = 64
index_pq = faiss.index_factory(dimension, f"PQ{code_size}",faiss.METRIC_L1)
index_pq.train(db_vectors)
index_pq.add(db_vectors)
distances_pq, indices_pq = index_pq.search(query_vectors, k)