Fix query filter so that the default operator between different query operations on the same parameter is AND instead of OR

This commit is contained in:
allegroai 2024-01-10 15:24:37 +02:00
parent 11b7a384af
commit a5c3ef6385
2 changed files with 8 additions and 13 deletions

View File

@ -1125,11 +1125,7 @@ class ProjectBLL:
helper = GetMixin.NewListFieldBucketHelper( helper = GetMixin.NewListFieldBucketHelper(
field, data=field_filter, legacy=True field, data=field_filter, legacy=True
) )
op = ( op = helper.global_operator
Q.OR
if helper.explicit_operator and helper.global_operator == Q.OR
else Q.AND
)
db_query = {op: helper.actions} db_query = {op: helper.actions}
else: else:
helper = GetMixin.ListQueryFilter.from_data(field, field_filter) helper = GetMixin.ListQueryFilter.from_data(field, field_filter)

View File

@ -146,9 +146,10 @@ class GetMixin(PropsMixin):
"__$any": Q.OR, "__$any": Q.OR,
"__$or": Q.OR, "__$or": Q.OR,
} }
default_operator = Q.OR default_global_operator = Q.AND
mongo_modifiers = { default_context = Q.OR
# not_all modifier currently not supported due to the backwards compatibility # not_all modifier currently not supported due to the backwards compatibility
mongo_modifiers = {
Q.AND: {True: "all", False: "nin"}, Q.AND: {True: "all", False: "nin"},
Q.OR: {True: "in", False: "nin"}, Q.OR: {True: "in", False: "nin"},
} }
@ -165,24 +166,22 @@ class GetMixin(PropsMixin):
self.allow_empty = False self.allow_empty = False
self.global_operator = None self.global_operator = None
self.actions = defaultdict(list) self.actions = defaultdict(list)
self.explicit_operator = False
self._support_legacy = legacy self._support_legacy = legacy
current_context = self.default_operator current_context = self.default_context
for d in self._get_next_term(data): for d in self._get_next_term(data):
if d.operator is not None: if d.operator is not None:
current_context = d.operator current_context = d.operator
self._support_legacy = False self._support_legacy = False
if self.global_operator is None: if self.global_operator is None:
self.global_operator = d.operator self.global_operator = d.operator
self.explicit_operator = True
continue continue
if self.global_operator is None: if self.global_operator is None:
self.global_operator = self.default_operator self.global_operator = self.default_global_operator
if d.reset: if d.reset:
current_context = self.default_operator current_context = self.default_context
self._support_legacy = legacy self._support_legacy = legacy
continue continue
@ -195,7 +194,7 @@ class GetMixin(PropsMixin):
) )
if self.global_operator is None: if self.global_operator is None:
self.global_operator = self.default_operator self.global_operator = self.default_global_operator
def _get_next_term(self, data: Sequence[str]) -> Generator[Term, None, None]: def _get_next_term(self, data: Sequence[str]) -> Generator[Term, None, None]:
unary_operator = None unary_operator = None