@@ -109,36 +109,35 @@ def _prepare_expressions_for_pipeline(self, expression, target, annotation_group
109
109
replacements [sub_expr ] = self ._get_replace_expr (sub_expr , group , alias )
110
110
return replacements , group
111
111
112
- def _prepare_search_expressions_for_pipeline (self , expression , target , search_idx ):
112
+ def _prepare_search_expressions_for_pipeline (
113
+ self , expression , target , search_idx , replacements
114
+ ):
113
115
searches = {}
114
- replacements = {}
115
116
for sub_expr in self ._get_search_expressions (expression ):
116
- alias = f"__search_expr.search{ next (search_idx )} "
117
- replacements [sub_expr ] = self ._get_replace_expr (sub_expr , searches , alias )
118
- return replacements , list (searches .values ())
117
+ if sub_expr not in replacements :
118
+ alias = f"__search_expr.search{ next (search_idx )} "
119
+ replacements [sub_expr ] = self ._get_replace_expr (sub_expr , searches , alias )
120
+ return list (searches .values ())
119
121
120
122
def _prepare_search_query_for_aggregation_pipeline (self , order_by ):
121
123
replacements = {}
122
124
searches = []
123
125
annotation_group_idx = itertools .count (start = 1 )
124
126
for target , expr in self .query .annotation_select .items ():
125
- new_replacements , expr_searches = self ._prepare_search_expressions_for_pipeline (
126
- expr , target , annotation_group_idx
127
+ expr_searches = self ._prepare_search_expressions_for_pipeline (
128
+ expr , target , annotation_group_idx , replacements
127
129
)
128
- replacements .update (new_replacements )
129
130
searches += expr_searches
130
131
131
132
for expr , _ in order_by :
132
- new_replacements , expr_searches = self ._prepare_search_expressions_for_pipeline (
133
- expr , None , annotation_group_idx
133
+ expr_searches = self ._prepare_search_expressions_for_pipeline (
134
+ expr , None , annotation_group_idx , replacements
134
135
)
135
- replacements .update (new_replacements )
136
136
searches += expr_searches
137
137
138
- having_replacements , having_group = self ._prepare_search_expressions_for_pipeline (
139
- self .having , None , annotation_group_idx
138
+ having_group = self ._prepare_search_expressions_for_pipeline (
139
+ self .having , None , annotation_group_idx , replacements
140
140
)
141
- replacements .update (having_replacements )
142
141
searches += having_group
143
142
return searches , replacements
144
143
0 commit comments