Filter callback interface for on-the-fly KNN filtering #128
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This commit introduces a filter callback mechanism that allows external code to pass filter callbacks to the KNN library without creating a direct dependency on hnswlib headers in the public API.
Related PRs:
Key changes:
FilterCallback_fntype alias (std::function<bool(uint32_t)>) as a public API for filter callbackshnswlib::BaseFilterFunctorin global namespace to avoid including hnswlib headers in public APIFilterCallbackWrapper_cclass that adaptsFilterCallback_fntohnswlib::BaseFilterFunctorinternallyKNN_i::CreateIterator()to accept both:::hnswlib::BaseFilterFunctor * pFilter(for internal use)FilterCallback_fn fnFilter(for public API)KNNIndex_i::Search()to accept both filter types and use the wrapper whenfnFilteris provided butpFilteris nullCreateIterator()function to accept and pass both filter typeshnswlib::namespace references to::hnswlib::for explicit global namespace qualificationArchitecture:
External code creates a function/lambda implementing the filter logic and passes it as
FilterCallback_fnto the KNN library. The KNN library then wraps this inFilterCallbackWrapper_cwhich inherits fromhnswlib::BaseFilterFunctor, allowing it to be passed to hnswlib's search algorithm. This maintains API separation while enabling on-the-fly filtering functionality.This change enables on-the-fly KNN filtering where the search algorithm continues exploring until k filtered candidates are found, rather than finding k total candidates and then filtering them post-search.
Notes
This PR includes a temporary change in
cmake/GetHNSW.cmake. It points to an updated version of hnswlib in a branch and will be updated later.