From 31250909dffc5ea16afc87e9093086c7c2e643b6 Mon Sep 17 00:00:00 2001 From: Leo <6londe@gmail.com> Date: Mon, 2 Nov 2026 12:23:47 +0900 Subject: [PATCH] fix(filters): serialize reference counts for GraphQL aggregation --- test/collection/test_filter.py | 24 +++++++++++++++++++++++- weaviate/collections/filters.py | 2 ++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/test/collection/test_filter.py b/test/collection/test_filter.py index 9331c493d..ae24336b5 100644 --- a/test/collection/test_filter.py +++ b/test/collection/test_filter.py @@ -12,10 +12,32 @@ _FilterValue, _Operator, ) -from weaviate.collections.filters import _FilterToGRPC +from weaviate.collections.filters import _FilterToGRPC, _FilterToREST from weaviate.proto.v1 import base_pb2 +@pytest.mark.parametrize("compound", [False, True]) +def test_graphql_reference_count_filter(compound: bool) -> None: + """Serialize reference-count targets for direct and compound GraphQL filters.""" + ref_filter = wvc.query.Filter.by_ref_count("ref").greater_than(0) + expected_ref = { + "operator": "GreaterThan", + "path": ["ref"], + "valueInt": 0, + } + if compound: + combined = ref_filter & wvc.query.Filter.by_property("name").equal("Berlin") + assert _FilterToREST.convert(combined) == { + "operator": "And", + "operands": [ + expected_ref, + {"operator": "Equal", "path": ["name"], "valueText": "Berlin"}, + ], + } + else: + assert _FilterToREST.convert(ref_filter) == expected_ref + + def test_empty_input_contains_any() -> None: with pytest.raises(weaviate.exceptions.WeaviateInvalidInputError): wvc.query.Filter.by_id().contains_any([]) diff --git a/weaviate/collections/filters.py b/weaviate/collections/filters.py index a489c7d55..a632e7ac8 100644 --- a/weaviate/collections/filters.py +++ b/weaviate/collections/filters.py @@ -205,6 +205,8 @@ def __value_filter(weav_filter: _FilterValue) -> Dict[str, Any]: def __to_path(target: _FilterTargets) -> List[str]: if isinstance(target, str): return [target] + elif isinstance(target, _CountRef): + return [target.link_on] elif isinstance(target, _SingleTargetRef): raise WeaviateInvalidInputError( "Cannot use Filter.by_ref() in the aggregate API currently. Instead use Filter.by_ref_multi_target() and specify the target collection explicitly."