Skip to content

Setting search parameters for one query

Matthijs Douze edited this page Oct 4, 2022 · 15 revisions

Faiss indexes have their search-time parameters as object fields.

However, it can be useful to set these parameters separately per query. For example, for an IndexIVF, one query vector may be run with nprobe=10 and another with nprobe=20. This is problematic when the searches are called from different threads.

To support this use case, a SearchParameter object can be provided as the last argument of the search() function.

SearchParameter instances

Index types that accept search parameters have a corresponding SearchParameter child object. For example, IndexIVFPQ has a SearchParameterIVFPQ object.

The search will look like:

D, I = index.search(xq, 10, params=faiss.SearchParametersIVFPQ(nprobe = 10) 

Note that the params= is mandatory, not to confuse the search parameters with possible I and D output buffers that can also be provided. In C++:

idx_t *I = ...
float *D = ...
faiss::SearchParametersIVFPQ params; 
params.nprobe = 10; 
index.search(nq, xq, 10, D, I, &params);

Searching in a subset of elements

Clone this wiki locally