@@ -34,7 +34,7 @@ def __init__(self, *args, **kwargs):
34
34
# A list of OrderBy objects for this query.
35
35
self .order_by_objs = None
36
36
self .subqueries = []
37
- # Atlas search calls
37
+ # Atlas search stage.
38
38
self .search_pipeline = []
39
39
40
40
def _get_group_alias_column (self , expr , annotation_group_idx ):
@@ -65,8 +65,8 @@ def _get_replace_expr(self, sub_expr, group, alias):
65
65
column_target .set_attributes_from_name (alias )
66
66
inner_column = Col (self .collection_name , column_target )
67
67
if getattr (sub_expr , "distinct" , False ):
68
- # If the expression should return distinct values, use
69
- # $addToSet to deduplicate.
68
+ # If the expression should return distinct values, use $addToSet to
69
+ # deduplicate.
70
70
rhs = sub_expr .as_mql (self , self .connection , resolve_inner_expression = True )
71
71
group [alias ] = {"$addToSet" : rhs }
72
72
replacing_expr = sub_expr .copy ()
@@ -112,9 +112,10 @@ def _prepare_search_expressions_for_pipeline(self, expression, search_idx, repla
112
112
"""
113
113
Collect and prepare unique search expressions for inclusion in an
114
114
aggregation pipeline.
115
- Iterates over all search sub-expressions of the given expression,
116
- assigns a unique alias to each, and maps them to their
117
- replacement expressions.
115
+
116
+ Iterate over all search sub-expressions of the given expression.
117
+ Assigning a unique alias to each and map them to their replacement
118
+ expressions.
118
119
"""
119
120
searches = {}
120
121
for sub_expr in self ._get_search_expressions (expression ):
@@ -126,23 +127,21 @@ def _prepare_search_query_for_aggregation_pipeline(self, order_by):
126
127
"""
127
128
Prepare expressions for the search pipeline.
128
129
129
- Handle the computation of search functions used by various
130
- expressions. Separate and create intermediate columns, and replace
131
- nodes to simulate a search operation.
130
+ Handle the computation of search functions used by various expressions.
131
+ Separate and create intermediate columns, and replace nodes to simulate
132
+ a search operation.
132
133
133
- MongoDB's $search or $searchVector are stages. To apply operations over them,
134
- compute the $search or $vectorSearch first, then apply additional operations in a subsequent
135
- stage by replacing the aggregate expressions with new document field prefixed
136
- by `__search_expr.search#`.
134
+ To apply operations over the $search or $searchVector stages, compute
135
+ the $search or $vectorSearch first, then apply additional operations in
136
+ a subsequent stage by replacing the aggregate expressions with a new
137
+ document field prefixed by `__search_expr.search#`.
137
138
"""
138
139
replacements = {}
139
140
annotation_group_idx = itertools .count (start = 1 )
140
141
for expr in self .query .annotation_select .values ():
141
142
self ._prepare_search_expressions_for_pipeline (expr , annotation_group_idx , replacements )
142
-
143
143
for expr , _ in order_by :
144
144
self ._prepare_search_expressions_for_pipeline (expr , annotation_group_idx , replacements )
145
-
146
145
self ._prepare_search_expressions_for_pipeline (
147
146
self .having , annotation_group_idx , replacements
148
147
)
@@ -257,13 +256,14 @@ def _build_aggregation_pipeline(self, ids, group):
257
256
258
257
def _compound_searches_queries (self , search_replacements ):
259
258
"""
260
- Builds a query pipeline from a mapping of search expressions to result columns.
259
+ Build a query pipeline from a mapping of search expressions to result
260
+ columns.
261
261
262
- Currently, only a single ` $search` or ` $vectorSearch` expression is supported.
263
- Combining multiple search expressions is not yet allowed and will raise a ValueError.
262
+ Currently only a single $search or $vectorSearch expression is
263
+ supported. Combining multiple search expressions raises ValueError.
264
264
265
- This method will eventually support hybrid search by allowing the combination of
266
- ` $search` and ` $vectorSearch` operations.
265
+ This method will eventually support hybrid search by allowing the
266
+ combination of $search and $vectorSearch operations.
267
267
"""
268
268
if not search_replacements :
269
269
return []
0 commit comments