Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Incorrect Query Prefixing for Embedded Models in Query Construction #657

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
41 changes: 22 additions & 19 deletions aredis_om/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def score_field(self) -> str:
class ExpressionProxy:
def __init__(self, field: ModelField, parents: List[Tuple[str, "RedisModel"]]):
self.field = field
self.parents = parents
self.parents = parents.copy() # Ensure a copy is stored

def __eq__(self, other: Any) -> Expression: # type: ignore[override]
return Expression(
Expand Down Expand Up @@ -387,13 +387,14 @@ def __getattr__(self, item):
attr = getattr(embedded_cls, item)
else:
attr = getattr(outer_type, item)

if isinstance(attr, self.__class__):
# Clone the parents to ensure isolation
new_parents = self.parents.copy()
new_parent = (self.field.alias, outer_type)
if new_parent not in attr.parents:
attr.parents.append(new_parent)
new_parents = list(set(self.parents) - set(attr.parents))
if new_parents:
attr.parents = new_parents + attr.parents
if new_parent not in new_parents:
new_parents.append(new_parent)
attr.parents = new_parents
return attr


Expand Down Expand Up @@ -624,18 +625,19 @@ def expand_tag_value(value):

@classmethod
def resolve_value(
cls,
field_name: str,
field_type: RediSearchFieldTypes,
field_info: PydanticFieldInfo,
op: Operators,
value: Any,
parents: List[Tuple[str, "RedisModel"]],
cls,
field_name: str,
field_type: RediSearchFieldTypes,
field_info: PydanticFieldInfo,
op: Operators,
value: Any,
parents: List[Tuple[str, "RedisModel"]],
) -> str:
# The 'field_name' should already include the correct prefix
result = ""
if parents:
prefix = "_".join([p[0] for p in parents])
field_name = f"{prefix}_{field_name}"
result = ""
if field_type is RediSearchFieldTypes.TEXT:
result = f"@{field_name}_fts:"
if op is Operators.EQ:
Expand Down Expand Up @@ -792,15 +794,13 @@ def resolve_redisearch_query(cls, expression: ExpressionOrNegated) -> str:

if expression.op is Operators.ALL:
if encompassing_expression_is_negated:
# TODO: Is there a use case for this, perhaps for dynamic
# scoring purposes with full-text search?
raise QueryNotSupportedError(
"You cannot negate a query for all results."
)
return "*"

if isinstance(expression.left, Expression) or isinstance(
expression.left, NegatedExpression
expression.left, NegatedExpression
):
result += f"({cls.resolve_redisearch_query(expression.left)})"
elif isinstance(expression.left, ModelField):
Expand All @@ -827,6 +827,11 @@ def resolve_redisearch_query(cls, expression: ExpressionOrNegated) -> str:
f"or an expression enclosed in parentheses. Docs: {ERRORS_URL}#E7"
)

if isinstance(expression.left, ModelField) and expression.parents:
# Build field_name using the specific parents for this expression
prefix = "_".join([p[0] for p in expression.parents])
field_name = f"{prefix}_{field_name}"

right = expression.right

if isinstance(right, Expression) or isinstance(right, NegatedExpression):
Expand All @@ -842,8 +847,6 @@ def resolve_redisearch_query(cls, expression: ExpressionOrNegated) -> str:

if isinstance(right, NegatedExpression):
result += "-"
# We're handling the RediSearch operator in this call ("-"), so resolve the
# inner expression instead of the NegatedExpression.
right = right.expression

result += f"({cls.resolve_redisearch_query(right)})"
Expand Down