Skip to content

chore: refactor filter in hnswlib #1728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions docarray/index/backends/hnswlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,21 +619,17 @@ def _search_and_filter(

index = self._hnsw_indices[search_field]

def accept_all(id):
"""Accepts all IDs."""
return True

def accept_hashed_ids(id):
"""Accepts IDs that are in hashed_ids."""
return id in hashed_ids # type: ignore[operator]

# Choose the appropriate filter function based on whether hashed_ids was provided
filter_function = accept_hashed_ids if hashed_ids else accept_all
extra_kwargs = {'filter': accept_hashed_ids} if hashed_ids else {}

# If hashed_ids is provided, k is the minimum of limit and the length of hashed_ids; else it is limit
k = min(limit, len(hashed_ids)) if hashed_ids else limit

labels, distances = index.knn_query(queries, k=k, filter=filter_function)
labels, distances = index.knn_query(queries, k=k, **extra_kwargs)

result_das = [
self._get_docs_sqlite_hashed_id(
Expand Down