diff --git a/caluma/caluma_core/filters.py b/caluma/caluma_core/filters.py index 44382ad37..303262f42 100644 --- a/caluma/caluma_core/filters.py +++ b/caluma/caluma_core/filters.py @@ -6,6 +6,7 @@ from django.contrib.postgres.fields.hstore import KeyTransform from django.contrib.postgres.search import SearchVector from django.db import models +from django.db.models import Q from django.db.models.constants import LOOKUP_SEP from django.db.models.expressions import OrderBy from django.db.models.fields.json import KeyTextTransform @@ -378,6 +379,7 @@ class JSONLookupMode(Enum): CONTAINS = "contains" ICONTAINS = "icontains" IN = "in" + INTERSECTS = "intersects" GTE = "gte" GT = "gt" LTE = "lte" @@ -425,10 +427,19 @@ def filter(self, qs, value): ), ) lookup = {f"field_val__{lookup_expr}": expr["value"]} + elif lookup_expr == JSONLookupMode.INTERSECTS: + lookup = {} + exprs = [ + Q(**{f"{self.field_name}__{expr['key']}__contains": val}) + for val in expr["value"] + ] + # connect all expressions with OR + qs = qs.filter(reduce(lambda a, b: a | b, exprs)) else: lookup = { f"{self.field_name}__{expr['key']}__{lookup_expr}": expr["value"] } + qs = qs.filter(**lookup) return qs diff --git a/caluma/caluma_core/tests/test_meta_filters.py b/caluma/caluma_core/tests/test_meta_filters.py index 9459bb00e..319273a85 100644 --- a/caluma/caluma_core/tests/test_meta_filters.py +++ b/caluma/caluma_core/tests/test_meta_filters.py @@ -12,6 +12,7 @@ ("CONTAINS", "bar", ["contains", "starts", "exact"]), ("ICONTAINS", "bar", ["icontains", "contains", "starts", "exact"]), ("IN", [1, 2], ["in"]), + ("INTERSECTS", [1, 2], ["in", "intersects"]), (None, True, ["bool"]), (None, 123, ["int"]), (None, 123.456, ["float"]), @@ -30,6 +31,7 @@ def test_meta_value_filter( "bool": document_factory(meta={"foo": True}), "int": document_factory(meta={"foo": 123}), "float": document_factory(meta={"foo": 123.456}), + "intersects": document_factory(meta={"foo": [2, 3]}), } query = """ diff --git a/caluma/tests/__snapshots__/test_schema.ambr b/caluma/tests/__snapshots__/test_schema.ambr index 76db8fe6e..5870285bb 100644 --- a/caluma/tests/__snapshots__/test_schema.ambr +++ b/caluma/tests/__snapshots__/test_schema.ambr @@ -1671,6 +1671,7 @@ CONTAINS ICONTAINS IN + INTERSECTS GTE GT LTE