I have Nearest Neighbors model, built with sklearn sklearn.neighbors.NearestNeighbors, which I use to make content based recommendations.

Sometimes I need to retrieve recommendations based on query conditions. For example, I want to get items within concrete location.

How can I add location filter to Nearest Neighbors query?


1 Answer 1


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):
        same_location_ids = []
        # use locations from request
        for loc in request_locations:

        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 = (

indices = self._classifier.predict(features, params=params, k=k)

return list(indices[0][:])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.