Skip to content

Commit dbdc990

Browse files
author
Joan Fontanals Martinez
committed
Merge branch 'main' of https://github.com/docarray/docarray into fix-num-docs
Signed-off-by: Joan Fontanals Martinez <[email protected]>
2 parents 3ca5780 + 24143a1 commit dbdc990

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

docarray/index/backends/hnswlib.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -618,21 +618,20 @@ def _search_and_filter(
618618

619619
index = self._hnsw_indices[search_field]
620620

621-
def accept_all(id):
622-
"""Accepts all IDs."""
623-
return True
624-
625621
def accept_hashed_ids(id):
626622
"""Accepts IDs that are in hashed_ids."""
627623
return id in hashed_ids # type: ignore[operator]
628624

629625
# Choose the appropriate filter function based on whether hashed_ids was provided
630-
filter_function = accept_hashed_ids if hashed_ids else accept_all
626+
extra_kwargs = {'filter': accept_hashed_ids} if hashed_ids else {}
631627

632628
# If hashed_ids is provided, k is the minimum of limit and the length of hashed_ids; else it is limit
633629
k = min(limit, len(hashed_ids)) if hashed_ids else limit
634-
635-
labels, distances = index.knn_query(queries, k=k, filter=filter_function)
630+
try:
631+
labels, distances = index.knn_query(queries, k=k, **extra_kwargs)
632+
except RuntimeError:
633+
k = min(k, self.num_docs())
634+
labels, distances = index.knn_query(queries, k=k, **extra_kwargs)
636635

637636
result_das = [
638637
self._get_docs_sqlite_hashed_id(

0 commit comments

Comments
 (0)