Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/how-to/vectors.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
----
Expand Down
6 changes: 6 additions & 0 deletions src/arcadedb/_native/schema_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 6 additions & 1 deletion src/arcadedb/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
45 changes: 45 additions & 0 deletions tests/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- LSMVectorIndex operations (find_nearest)
"""

import numpy as np
import pytest

import arcadedb
Expand Down Expand Up @@ -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
):
Expand Down
Loading