I solved this problem with IDSelectorBatch
.
I have a dictionary where fields that I use in conditions are mapped to item Ids. Ids from this dictionary are used in 'IDSelectorBatch' to make queries work as queries with conditions.
Example with location
field:
1.Create IDSelectorBatch
with required ids:
def _get_location_filter(self, request_locations):
try:
same_location_ids = []
# use locations from request
for loc in request_locations:
same_location_ids.extend(self._locationsToId[loc])
return faiss.IDSelectorBatch(list(same_location_ids))
except KeyError:
return faiss.IDSelectorAll()
1.Use it to search in faiss index:
sel = self._get_location_filter(request['locations'])
params = (
faiss.SearchParametersIVF(sel=sel)
)
indices = self._classifier.predict(features, params=params, k=k)
return list(indices[0][:])