From 37db7cc21126397d1fbc94e3925d6635a712151c Mon Sep 17 00:00:00 2001 From: Shaun Patterson Date: Sat, 21 Nov 2026 11:24:46 -0500 Subject: [PATCH] fix(#423): find_nearest(allowed_rids=[]) returns nothing, and a numpy array of RIDs works An empty allowed_rids was read as no filter: the JVM wheel dropped it, the native wheel dropped a list but sent an iterator as an empty filter. A numpy array raised on its truth value. None alone means no filter now, every other collection is read as a list, and an empty one returns an empty result. --- docs/modules/ROOT/pages/how-to/vectors.adoc | 2 +- src/arcadedb/_native/schema_objects.py | 6 +++ src/arcadedb/vector.py | 7 +++- tests/test_vector.py | 45 +++++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/docs/modules/ROOT/pages/how-to/vectors.adoc b/docs/modules/ROOT/pages/how-to/vectors.adoc index 828a51ee..fbec1d1f 100644 --- a/docs/modules/ROOT/pages/how-to/vectors.adoc +++ b/docs/modules/ROOT/pages/how-to/vectors.adoc @@ -379,7 +379,7 @@ python === Filtered search -`allowed_rids` restricts `find_nearest` to the records whose ids you list, as strings. The search considers only those records, so it still returns `k` of them. +`allowed_rids` restricts `find_nearest` to the records whose ids you list, as strings. The search considers only those records, so it still returns `k` of them. `allowed_rids=None` (the default) is no filter. An empty list allows no record, so the search returns an empty list. [source,python] ---- diff --git a/src/arcadedb/_native/schema_objects.py b/src/arcadedb/_native/schema_objects.py index 5f69e0d1..f6bedbb6 100644 --- a/src/arcadedb/_native/schema_objects.py +++ b/src/arcadedb/_native/schema_objects.py @@ -231,6 +231,12 @@ def _run_search( ef_search=None, include_vector=True, ): + if allowed_rids is not None: + allowed_rids = list(allowed_rids) + if not allowed_rids: + return ( + [] + ) # allowed_rids=None is no filter; an empty collection allows no record if approximate: return self._run_approximate(query_vector, k, allowed_rids) if not include_vector: diff --git a/src/arcadedb/vector.py b/src/arcadedb/vector.py index 3c407068..a4fcc58f 100644 --- a/src/arcadedb/vector.py +++ b/src/arcadedb/vector.py @@ -424,7 +424,8 @@ def _lookup_query_vector_by_key(self, key): return query_vector def _build_allowed_rids_set(self, allowed_rids): - if not allowed_rids: + """The Java set of the RIDs a search may return; None for ``allowed_rids=None`` (no filter). An empty collection is an empty set.""" + if allowed_rids is None: return None from .graph import _graph_calls @@ -570,6 +571,10 @@ def _run_search( ): java_vector = to_java_float_array(query_vector) allowed_rids_set = self._build_allowed_rids_set(allowed_rids) + if allowed_rids_set is not None and allowed_rids_set.isEmpty(): + return ( + [] + ) # no record is allowed; an empty set must not reach the engine as "no filter" return self._collect_search_results( java_vector=java_vector, diff --git a/tests/test_vector.py b/tests/test_vector.py index 6f49f272..bb805d9c 100644 --- a/tests/test_vector.py +++ b/tests/test_vector.py @@ -6,6 +6,7 @@ - LSMVectorIndex operations (find_nearest) """ +import numpy as np import pytest import arcadedb @@ -695,6 +696,50 @@ def test_lsm_vector_search_with_filter(self, test_db): assert len(results) == 1 assert str(results[0][0].get_identity()) == rids[3] + @pytest.mark.parametrize( + "make_empty", + [list, tuple, lambda: iter(()), lambda: np.array([], dtype=object)], + ids=["list", "tuple", "iterator", "ndarray"], + ) + def test_an_empty_allowed_rids_returns_nothing(self, test_db, make_empty): + """``allowed_rids=None`` is no filter; an empty collection allows no record, so the search + returns nothing. It used to be read as "no filter" and return the nearest records of the + whole index (and a numpy array raised "truth value of an array is ambiguous"). + """ + test_db.command("sql", "CREATE VERTEX TYPE Doc") + test_db.command("sql", "CREATE PROPERTY Doc.embedding ARRAY_OF_FLOATS") + index = test_db.create_vector_index("Doc", "embedding", dimensions=3) + with test_db.transaction(): + for vec in ([1.0, 0.0, 0.0], [0.9, 0.1, 0.0], [0.0, 1.0, 0.0]): + test_db.command( + "sql", + "INSERT INTO Doc SET embedding = ?", + arcadedb.to_java_float_array(vec), + ) + query = [1.0, 0.0, 0.0] + assert len(index.find_nearest(query, k=3)) == 3 + assert index.find_nearest(query, k=3, allowed_rids=make_empty()) == [] + + def test_allowed_rids_accepts_a_numpy_array(self, test_db): + """A truth test of ``allowed_rids`` raised "truth value of an array is ambiguous" for an array of two or more RIDs.""" + test_db.command("sql", "CREATE VERTEX TYPE Doc") + test_db.command("sql", "CREATE PROPERTY Doc.embedding ARRAY_OF_FLOATS") + index = test_db.create_vector_index("Doc", "embedding", dimensions=3) + with test_db.transaction(): + for vec in ([1.0, 0.0, 0.0], [0.9, 0.1, 0.0], [0.0, 1.0, 0.0]): + test_db.command( + "sql", + "INSERT INTO Doc SET embedding = ?", + arcadedb.to_java_float_array(vec), + ) + rids = [str(r.get_rid()) for r in test_db.query("sql", "SELECT FROM Doc")] + hits = index.find_nearest( + [1.0, 0.0, 0.0], k=3, allowed_rids=np.array(rids[1:], dtype=object) + ) + assert sorted(str(record.get_identity()) for record, _ in hits) == sorted( + rids[1:] + ) + def test_lsm_vector_search_looks_up_all_hits_in_one_bridge_call( self, test_db, monkeypatch ):