Skip to content

Setting search parameters for one query

Matthijs Douze edited this page Oct 5, 2022 · 12 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);

SearchParameters can be nested, for example

params=faiss.SearchParametersIVFPQ(
    nprobe=10, 
    quantizer_params=faiss.SearchParameterHNSW(efSearch=123)
)

is valid for an IVF1234_HNSW,... index.

Searching in a subset of elements

It is possible to select a subset of vectors, based on their ids, to search from. Similar to the remove_ids function, the subset is defined by an IDSelector object. The IDSelector::is_member(i) returns true if vector i should be included in the search.

IDSelectorRange

IDSelectorArray

IDSelectorBatch and IDSelectorBitmap

PyCallbackIDSelector

Clone this wiki locally