Fix: Incorrect Query Prefixing for Embedded Models in Query Construction #657
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background:
In the current implementation, constructing queries involving embedded models results in incorrect field prefixing. Specifically, when combining multiple conditions using logical operators (e.g., OR |), the parents list—which tracks the hierarchy of embedded fields—is unintentionally merged. This leads to improperly concatenated field names, causing queries like
@player1_player2_username:{username}
instead of the intended@player1_username:{username} | @player2_username:{username}.
as mentioned in #636 .Problem:
When executing queries with embedded models, the shared parents list across different expressions causes incorrect prefix concatenation. This results in invalid RediSearch queries that do not accurately target the intended fields within embedded models.
Solution:
To resolve this issue, the management of the parents list within the
ExpressionProxy
andFindQuery
classes has been revised to ensure that each expression maintains its own isolated parents hierarchy. This prevents the unintended merging of prefixes when combining multiple query conditions.Key Changes:
Cloning the parents List in
ExpressionProxy
:__init__ Method
:Cloned the parents list to ensure each
ExpressionProxy
instance operates with its own copy, preventing shared state across expressions.__getattr__ Method
:Cloned the parents list before appending a new parent, maintaining isolation between different query expressions.
Adjusting
FindQuery.resolve_redisearch_query
:Ensured that the parents list specific to each expression is used independently when constructing the query string.
Prevented the merging of prefixes by correctly prefixing field names based on their respective parents hierarchies.
Ensuring Correct Prefixing in resolve_value:
Confirmed that the
resolve_value
method receives and utilizes the correctly prefixedfield_name
, eliminating the possibility of incorrect query segments.Impact:
Correct Query Construction: Queries involving embedded models will now correctly reflect the intended field hierarchies without erroneous prefix concatenations.
Enhanced Reliability: Prevents future issues related to query inaccuracies when dealing with complex embedded model structures.
Backward Compatibility: Existing functionalities remain unaffected except for the correction of the query construction logic.
This resolves #636 and
also #542