1
1
from django .db import NotSupportedError
2
2
from django .db .models import CharField , Expression , FloatField , TextField
3
- from django .db .models .expressions import F , Value
3
+ from django .db .models .expressions import Value
4
4
from django .db .models .lookups import Lookup
5
5
6
6
from ..query_utils import process_lhs , process_rhs
7
+ from .builtins import Path
7
8
8
9
9
- def cast_as_field (path ):
10
- return F (path ) if isinstance (path , str ) else path
10
+ def cast_as_path (path ):
11
+ return Path (path ) if isinstance (path , str ) else path
11
12
12
13
13
14
class Operator :
@@ -146,19 +147,19 @@ class SearchAutocomplete(SearchExpression):
146
147
"""
147
148
148
149
def __init__ (self , path , query , * , fuzzy = None , token_order = None , score = None ):
149
- self .path = cast_as_field (path )
150
+ self .path = cast_as_path (path )
150
151
self .query = query
151
152
self .fuzzy = fuzzy
152
153
self .token_order = token_order
153
154
self .score = score
154
155
super ().__init__ ()
155
156
156
157
def get_search_fields (self , compiler , connection ):
157
- return {self .path .as_mql (compiler , connection , as_path = True )}
158
+ return {self .path .as_mql (compiler , connection )}
158
159
159
160
def search_operator (self , compiler , connection ):
160
161
params = {
161
- "path" : self .path .as_mql (compiler , connection , as_path = True ),
162
+ "path" : self .path .as_mql (compiler , connection ),
162
163
"query" : self .query ,
163
164
}
164
165
if self .score :
@@ -186,17 +187,17 @@ class SearchEquals(SearchExpression):
186
187
"""
187
188
188
189
def __init__ (self , path , value , * , score = None ):
189
- self .path = cast_as_field (path )
190
+ self .path = cast_as_path (path )
190
191
self .value = value
191
192
self .score = score
192
193
super ().__init__ ()
193
194
194
195
def get_search_fields (self , compiler , connection ):
195
- return {self .path .as_mql (compiler , connection , as_path = True )}
196
+ return {self .path .as_mql (compiler , connection )}
196
197
197
198
def search_operator (self , compiler , connection ):
198
199
params = {
199
- "path" : self .path .as_mql (compiler , connection , as_path = True ),
200
+ "path" : self .path .as_mql (compiler , connection ),
200
201
"value" : self .value ,
201
202
}
202
203
if self .score :
@@ -223,16 +224,16 @@ class SearchExists(SearchExpression):
223
224
"""
224
225
225
226
def __init__ (self , path , * , score = None ):
226
- self .path = cast_as_field (path )
227
+ self .path = cast_as_path (path )
227
228
self .score = score
228
229
super ().__init__ ()
229
230
230
231
def get_search_fields (self , compiler , connection ):
231
- return {self .path .as_mql (compiler , connection , as_path = True )}
232
+ return {self .path .as_mql (compiler , connection )}
232
233
233
234
def search_operator (self , compiler , connection ):
234
235
params = {
235
- "path" : self .path .as_mql (compiler , connection , as_path = True ),
236
+ "path" : self .path .as_mql (compiler , connection ),
236
237
}
237
238
if self .score :
238
239
params ["score" ] = self .score .as_mql (compiler , connection )
@@ -255,17 +256,17 @@ class SearchIn(SearchExpression):
255
256
"""
256
257
257
258
def __init__ (self , path , value , * , score = None ):
258
- self .path = cast_as_field (path )
259
+ self .path = cast_as_path (path )
259
260
self .value = value
260
261
self .score = score
261
262
super ().__init__ ()
262
263
263
264
def get_search_fields (self , compiler , connection ):
264
- return {self .path .as_mql (compiler , connection , as_path = True )}
265
+ return {self .path .as_mql (compiler , connection )}
265
266
266
267
def search_operator (self , compiler , connection ):
267
268
params = {
268
- "path" : self .path .as_mql (compiler , connection , as_path = True ),
269
+ "path" : self .path .as_mql (compiler , connection ),
269
270
"value" : self .value ,
270
271
}
271
272
if self .score :
@@ -294,19 +295,19 @@ class SearchPhrase(SearchExpression):
294
295
"""
295
296
296
297
def __init__ (self , path , query , * , slop = None , synonyms = None , score = None ):
297
- self .path = cast_as_field (path )
298
+ self .path = cast_as_path (path )
298
299
self .query = query
299
300
self .slop = slop
300
301
self .synonyms = synonyms
301
302
self .score = score
302
303
super ().__init__ ()
303
304
304
305
def get_search_fields (self , compiler , connection ):
305
- return {self .path .as_mql (compiler , connection , as_path = True )}
306
+ return {self .path .as_mql (compiler , connection )}
306
307
307
308
def search_operator (self , compiler , connection ):
308
309
params = {
309
- "path" : self .path .as_mql (compiler , connection , as_path = True ),
310
+ "path" : self .path .as_mql (compiler , connection ),
310
311
"query" : self .query ,
311
312
}
312
313
if self .score :
@@ -338,17 +339,17 @@ class SearchQueryString(SearchExpression):
338
339
"""
339
340
340
341
def __init__ (self , path , query , * , score = None ):
341
- self .path = cast_as_field (path )
342
+ self .path = cast_as_path (path )
342
343
self .query = query
343
344
self .score = score
344
345
super ().__init__ ()
345
346
346
347
def get_search_fields (self , compiler , connection ):
347
- return {self .path .as_mql (compiler , connection , as_path = True )}
348
+ return {self .path .as_mql (compiler , connection )}
348
349
349
350
def search_operator (self , compiler , connection ):
350
351
params = {
351
- "defaultPath" : self .path .as_mql (compiler , connection , as_path = True ),
352
+ "defaultPath" : self .path .as_mql (compiler , connection ),
352
353
"query" : self .query ,
353
354
}
354
355
if self .score :
@@ -378,7 +379,7 @@ class SearchRange(SearchExpression):
378
379
"""
379
380
380
381
def __init__ (self , path , * , lt = None , lte = None , gt = None , gte = None , score = None ):
381
- self .path = cast_as_field (path )
382
+ self .path = cast_as_path (path )
382
383
self .lt = lt
383
384
self .lte = lte
384
385
self .gt = gt
@@ -387,11 +388,11 @@ def __init__(self, path, *, lt=None, lte=None, gt=None, gte=None, score=None):
387
388
super ().__init__ ()
388
389
389
390
def get_search_fields (self , compiler , connection ):
390
- return {self .path .as_mql (compiler , connection , as_path = True )}
391
+ return {self .path .as_mql (compiler , connection )}
391
392
392
393
def search_operator (self , compiler , connection ):
393
394
params = {
394
- "path" : self .path .as_mql (compiler , connection , as_path = True ),
395
+ "path" : self .path .as_mql (compiler , connection ),
395
396
}
396
397
if self .score :
397
398
params ["score" ] = self .score .as_mql (compiler , connection )
@@ -424,18 +425,18 @@ class SearchRegex(SearchExpression):
424
425
"""
425
426
426
427
def __init__ (self , path , query , * , allow_analyzed_field = None , score = None ):
427
- self .path = cast_as_field (path )
428
+ self .path = cast_as_path (path )
428
429
self .query = query
429
430
self .allow_analyzed_field = allow_analyzed_field
430
431
self .score = score
431
432
super ().__init__ ()
432
433
433
434
def get_search_fields (self , compiler , connection ):
434
- return {self .path .as_mql (compiler , connection , as_path = True )}
435
+ return {self .path .as_mql (compiler , connection )}
435
436
436
437
def search_operator (self , compiler , connection ):
437
438
params = {
438
- "path" : self .path .as_mql (compiler , connection , as_path = True ),
439
+ "path" : self .path .as_mql (compiler , connection ),
439
440
"query" : self .query ,
440
441
}
441
442
if self .score :
@@ -472,7 +473,7 @@ class SearchText(SearchExpression):
472
473
"""
473
474
474
475
def __init__ (self , path , query , * , fuzzy = None , match_criteria = None , synonyms = None , score = None ):
475
- self .path = cast_as_field (path )
476
+ self .path = cast_as_path (path )
476
477
self .query = query
477
478
self .fuzzy = fuzzy
478
479
self .match_criteria = match_criteria
@@ -481,11 +482,11 @@ def __init__(self, path, query, *, fuzzy=None, match_criteria=None, synonyms=Non
481
482
super ().__init__ ()
482
483
483
484
def get_search_fields (self , compiler , connection ):
484
- return {self .path .as_mql (compiler , connection , as_path = True )}
485
+ return {self .path .as_mql (compiler , connection )}
485
486
486
487
def search_operator (self , compiler , connection ):
487
488
params = {
488
- "path" : self .path .as_mql (compiler , connection , as_path = True ),
489
+ "path" : self .path .as_mql (compiler , connection ),
489
490
"query" : self .query ,
490
491
}
491
492
if self .score :
@@ -520,18 +521,18 @@ class SearchWildcard(SearchExpression):
520
521
"""
521
522
522
523
def __init__ (self , path , query , allow_analyzed_field = None , score = None ):
523
- self .path = cast_as_field (path )
524
+ self .path = cast_as_path (path )
524
525
self .query = query
525
526
self .allow_analyzed_field = allow_analyzed_field
526
527
self .score = score
527
528
super ().__init__ ()
528
529
529
530
def get_search_fields (self , compiler , connection ):
530
- return {self .path .as_mql (compiler , connection , as_path = True )}
531
+ return {self .path .as_mql (compiler , connection )}
531
532
532
533
def search_operator (self , compiler , connection ):
533
534
params = {
534
- "path" : self .path .as_mql (compiler , connection , as_path = True ),
535
+ "path" : self .path .as_mql (compiler , connection ),
535
536
"query" : self .query ,
536
537
}
537
538
if self .score :
@@ -566,18 +567,18 @@ class SearchGeoShape(SearchExpression):
566
567
"""
567
568
568
569
def __init__ (self , path , relation , geometry , * , score = None ):
569
- self .path = cast_as_field (path )
570
+ self .path = cast_as_path (path )
570
571
self .relation = relation
571
572
self .geometry = geometry
572
573
self .score = score
573
574
super ().__init__ ()
574
575
575
576
def get_search_fields (self , compiler , connection ):
576
- return {self .path .as_mql (compiler , connection , as_path = True )}
577
+ return {self .path .as_mql (compiler , connection )}
577
578
578
579
def search_operator (self , compiler , connection ):
579
580
params = {
580
- "path" : self .path .as_mql (compiler , connection , as_path = True ),
581
+ "path" : self .path .as_mql (compiler , connection ),
581
582
"relation" : self .relation ,
582
583
"geometry" : self .geometry ,
583
584
}
@@ -610,18 +611,18 @@ class SearchGeoWithin(SearchExpression):
610
611
"""
611
612
612
613
def __init__ (self , path , kind , geometry , * , score = None ):
613
- self .path = cast_as_field (path )
614
+ self .path = cast_as_path (path )
614
615
self .kind = kind
615
616
self .geometry = geometry
616
617
self .score = score
617
618
super ().__init__ ()
618
619
619
620
def get_search_fields (self , compiler , connection ):
620
- return {self .path .as_mql (compiler , connection , as_path = True )}
621
+ return {self .path .as_mql (compiler , connection )}
621
622
622
623
def search_operator (self , compiler , connection ):
623
624
params = {
624
- "path" : self .path .as_mql (compiler , connection , as_path = True ),
625
+ "path" : self .path .as_mql (compiler , connection ),
625
626
self .kind : self .geometry ,
626
627
}
627
628
if self .score :
@@ -855,7 +856,7 @@ def __init__(
855
856
exact = None ,
856
857
filter = None ,
857
858
):
858
- self .path = cast_as_field (path )
859
+ self .path = cast_as_path (path )
859
860
self .query_vector = query_vector
860
861
self .limit = limit
861
862
self .num_candidates = num_candidates
@@ -879,7 +880,7 @@ def __ror__(self, other):
879
880
raise NotSupportedError ("SearchVector cannot be combined" )
880
881
881
882
def get_search_fields (self , compiler , connection ):
882
- return {self .path .as_mql (compiler , connection , as_path = True )}
883
+ return {self .path .as_mql (compiler , connection )}
883
884
884
885
def _get_query_index (self , fields , compiler ):
885
886
for search_indexes in compiler .collection .list_search_indexes ():
@@ -894,7 +895,7 @@ def _get_query_index(self, fields, compiler):
894
895
def as_mql (self , compiler , connection ):
895
896
params = {
896
897
"index" : self ._get_query_index (self .get_search_fields (compiler , connection ), compiler ),
897
- "path" : self .path .as_mql (compiler , connection , as_path = True ),
898
+ "path" : self .path .as_mql (compiler , connection ),
898
899
"queryVector" : self .query_vector ,
899
900
"limit" : self .limit ,
900
901
}
@@ -924,6 +925,7 @@ class SearchTextLookup(Lookup):
924
925
925
926
def __init__ (self , lhs , rhs ):
926
927
super ().__init__ (lhs , rhs )
928
+ self .lhs ._as_path = True
927
929
self .lhs = SearchText (self .lhs , self .rhs )
928
930
self .rhs = Value (0 )
929
931
0 commit comments