Skip to content

Conversation

@sanikolaev
Copy link
Collaborator

@sanikolaev sanikolaev commented Jan 12, 2026

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:

  • Bumped LIB_VERSION from 9 to 10 to reflect API changes
  • Added FilterCallback_fn type alias (std::function<bool(uint32_t)>) as a public API for filter callbacks
  • Added forward declaration for hnswlib::BaseFilterFunctor in global namespace to avoid including hnswlib headers in public API
  • Created FilterCallbackWrapper_c class that adapts FilterCallback_fn to hnswlib::BaseFilterFunctor internally
  • Updated KNN_i::CreateIterator() to accept both:
    • ::hnswlib::BaseFilterFunctor * pFilter (for internal use)
    • FilterCallback_fn fnFilter (for public API)
  • Updated KNNIndex_i::Search() to accept both filter types and use the wrapper when fnFilter is provided but pFilter is null
  • Updated CreateIterator() function to accept and pass both filter types
  • Changed all hnswlib:: namespace references to ::hnswlib:: for explicit global namespace qualification

Architecture:
External code creates a function/lambda implementing the filter logic and passes it as FilterCallback_fn to the KNN library. The KNN library then wraps this in FilterCallbackWrapper_c which inherits from hnswlib::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.

@sanikolaev sanikolaev requested a review from glookka January 12, 2026 11:45
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.

Key changes:
- Bumped LIB_VERSION from 9 to 10 to reflect API changes
- Added `FilterCallback_fn` type alias (`std::function<bool(uint32_t)>`)
  as a public API for filter callbacks
- Added forward declaration for `hnswlib::BaseFilterFunctor` in global
  namespace to avoid including hnswlib headers in public API
- Created `FilterCallbackWrapper_c` class that adapts `FilterCallback_fn`
  to `hnswlib::BaseFilterFunctor` internally
- Updated `KNN_i::CreateIterator()` to accept both:
  - `::hnswlib::BaseFilterFunctor * pFilter` (for internal use)
  - `FilterCallback_fn fnFilter` (for public API)
- Updated `KNNIndex_i::Search()` to accept both filter types and use
  the wrapper when `fnFilter` is provided but `pFilter` is null
- Updated `CreateIterator()` function to accept and pass both filter types
- Changed all `hnswlib::` namespace references to `::hnswlib::` for
  explicit global namespace qualification

Architecture:
External code creates a function/lambda implementing the filter logic
and passes it as `FilterCallback_fn` to the KNN library. The KNN library
then wraps this in `FilterCallbackWrapper_c` which inherits from
`hnswlib::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.
@github-actions
Copy link

Windows test results

  5 files    5 suites   18m 6s ⏱️
482 tests 467 ✅ 14 💤 1 ❌
490 runs  475 ✅ 14 💤 1 ❌

For more details on these failures, see this check.

Results for commit 920f181.

@github-actions
Copy link

Linux debug test results

  8 files    8 suites   13m 1s ⏱️
501 tests 480 ✅ 21 💤 0 ❌
515 runs  494 ✅ 21 💤 0 ❌

Results for commit 920f181.

@github-actions
Copy link

Linux release test results

  8 files    8 suites   6m 56s ⏱️
501 tests 486 ✅ 14 💤 1 ❌
515 runs  500 ✅ 14 💤 1 ❌

For more details on these failures, see this check.

Results for commit 920f181.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants