3
3
from django .db .models .expressions import F , Value
4
4
5
5
6
+ def cast_as_value (value ):
7
+ if value is None :
8
+ return None
9
+ return Value (value ) if not hasattr (value , "resolve_expression" ) else value
10
+
11
+
12
+ def cast_as_field (path ):
13
+ return F (path ) if isinstance (path , str ) else path
14
+
15
+
6
16
class Operator :
7
17
AND = "AND"
8
18
OR = "OR"
@@ -87,16 +97,6 @@ def as_sql(self, compiler, connection):
87
97
def get_source_expressions (self ):
88
98
return []
89
99
90
- @staticmethod
91
- def cast_as_value (value ):
92
- if value is None :
93
- return None
94
- return Value (value ) if not hasattr (value , "resolve_expression" ) else value
95
-
96
- @staticmethod
97
- def cast_as_field (path ):
98
- return F (path ) if isinstance (path , str ) else path
99
-
100
100
def _get_indexed_fields (self , mappings ):
101
101
for field , definition in mappings .get ("fields" , {}).items ():
102
102
yield field
@@ -145,10 +145,10 @@ class SearchAutocomplete(SearchExpression):
145
145
"""
146
146
147
147
def __init__ (self , path , query , fuzzy = None , token_order = None , score = None ):
148
- self .path = self . cast_as_field (path )
149
- self .query = self . cast_as_value (query )
150
- self .fuzzy = self . cast_as_value (fuzzy )
151
- self .token_order = self . cast_as_value (token_order )
148
+ self .path = cast_as_field (path )
149
+ self .query = cast_as_value (query )
150
+ self .fuzzy = cast_as_value (fuzzy )
151
+ self .token_order = cast_as_value (token_order )
152
152
self .score = score
153
153
super ().__init__ ()
154
154
@@ -197,8 +197,8 @@ class SearchEquals(SearchExpression):
197
197
"""
198
198
199
199
def __init__ (self , path , value , score = None ):
200
- self .path = self . cast_as_field (path )
201
- self .value = self . cast_as_value (value )
200
+ self .path = cast_as_field (path )
201
+ self .value = cast_as_value (value )
202
202
self .score = score
203
203
super ().__init__ ()
204
204
@@ -242,7 +242,7 @@ class SearchExists(SearchExpression):
242
242
"""
243
243
244
244
def __init__ (self , path , score = None ):
245
- self .path = self . cast_as_field (path )
245
+ self .path = cast_as_field (path )
246
246
self .score = score
247
247
super ().__init__ ()
248
248
@@ -266,8 +266,8 @@ def search_operator(self, compiler, connection):
266
266
267
267
class SearchIn (SearchExpression ):
268
268
def __init__ (self , path , value , score = None ):
269
- self .path = self . cast_as_field (path )
270
- self .value = self . cast_as_value (value )
269
+ self .path = cast_as_field (path )
270
+ self .value = cast_as_value (value )
271
271
self .score = score
272
272
super ().__init__ ()
273
273
@@ -314,10 +314,10 @@ class SearchPhrase(SearchExpression):
314
314
"""
315
315
316
316
def __init__ (self , path , query , slop = None , synonyms = None , score = None ):
317
- self .path = self . cast_as_field (path )
318
- self .query = self . cast_as_value (query )
319
- self .slop = self . cast_as_value (slop )
320
- self .synonyms = self . cast_as_value (synonyms )
317
+ self .path = cast_as_field (path )
318
+ self .query = cast_as_value (query )
319
+ self .slop = cast_as_value (slop )
320
+ self .synonyms = cast_as_value (synonyms )
321
321
self .score = score
322
322
super ().__init__ ()
323
323
@@ -366,8 +366,8 @@ class SearchQueryString(SearchExpression):
366
366
"""
367
367
368
368
def __init__ (self , path , query , score = None ):
369
- self .path = self . cast_as_field (path )
370
- self .query = self . cast_as_value (query )
369
+ self .path = cast_as_field (path )
370
+ self .query = cast_as_value (query )
371
371
self .score = score
372
372
super ().__init__ ()
373
373
@@ -414,11 +414,11 @@ class SearchRange(SearchExpression):
414
414
"""
415
415
416
416
def __init__ (self , path , lt = None , lte = None , gt = None , gte = None , score = None ):
417
- self .path = self . cast_as_field (path )
418
- self .lt = self . cast_as_value (lt )
419
- self .lte = self . cast_as_value (lte )
420
- self .gt = self . cast_as_value (gt )
421
- self .gte = self . cast_as_value (gte )
417
+ self .path = cast_as_field (path )
418
+ self .lt = cast_as_value (lt )
419
+ self .lte = cast_as_value (lte )
420
+ self .gt = cast_as_value (gt )
421
+ self .gte = cast_as_value (gte )
422
422
self .score = score
423
423
super ().__init__ ()
424
424
@@ -471,9 +471,9 @@ class SearchRegex(SearchExpression):
471
471
"""
472
472
473
473
def __init__ (self , path , query , allow_analyzed_field = None , score = None ):
474
- self .path = self . cast_as_field (path )
475
- self .query = self . cast_as_value (query )
476
- self .allow_analyzed_field = self . cast_as_value (allow_analyzed_field )
474
+ self .path = cast_as_field (path )
475
+ self .query = cast_as_value (query )
476
+ self .allow_analyzed_field = cast_as_value (allow_analyzed_field )
477
477
self .score = score
478
478
super ().__init__ ()
479
479
@@ -522,11 +522,11 @@ class SearchText(SearchExpression):
522
522
"""
523
523
524
524
def __init__ (self , path , query , fuzzy = None , match_criteria = None , synonyms = None , score = None ):
525
- self .path = self . cast_as_field (path )
526
- self .query = self . cast_as_value (query )
527
- self .fuzzy = self . cast_as_value (fuzzy )
528
- self .match_criteria = self . cast_as_value (match_criteria )
529
- self .synonyms = self . cast_as_value (synonyms )
525
+ self .path = cast_as_field (path )
526
+ self .query = cast_as_value (query )
527
+ self .fuzzy = cast_as_value (fuzzy )
528
+ self .match_criteria = cast_as_value (match_criteria )
529
+ self .synonyms = cast_as_value (synonyms )
530
530
self .score = score
531
531
super ().__init__ ()
532
532
@@ -579,9 +579,9 @@ class SearchWildcard(SearchExpression):
579
579
"""
580
580
581
581
def __init__ (self , path , query , allow_analyzed_field = None , score = None ):
582
- self .path = self . cast_as_field (path )
583
- self .query = self . cast_as_value (query )
584
- self .allow_analyzed_field = self . cast_as_value (allow_analyzed_field )
582
+ self .path = cast_as_field (path )
583
+ self .query = cast_as_value (query )
584
+ self .allow_analyzed_field = cast_as_value (allow_analyzed_field )
585
585
self .score = score
586
586
super ().__init__ ()
587
587
@@ -628,9 +628,9 @@ class SearchGeoShape(SearchExpression):
628
628
"""
629
629
630
630
def __init__ (self , path , relation , geometry , score = None ):
631
- self .path = self . cast_as_field (path )
632
- self .relation = self . cast_as_value (relation )
633
- self .geometry = self . cast_as_value (geometry )
631
+ self .path = cast_as_field (path )
632
+ self .relation = cast_as_value (relation )
633
+ self .geometry = cast_as_value (geometry )
634
634
self .score = score
635
635
super ().__init__ ()
636
636
@@ -677,9 +677,9 @@ class SearchGeoWithin(SearchExpression):
677
677
"""
678
678
679
679
def __init__ (self , path , kind , geo_object , score = None ):
680
- self .path = self . cast_as_field (path )
681
- self .kind = self . cast_as_value (kind )
682
- self .geo_object = self . cast_as_value (geo_object )
680
+ self .path = cast_as_field (path )
681
+ self .kind = cast_as_value (kind )
682
+ self .geo_object = cast_as_value (geo_object )
683
683
self .score = score
684
684
super ().__init__ ()
685
685
@@ -722,7 +722,7 @@ class SearchMoreLikeThis(SearchExpression):
722
722
"""
723
723
724
724
def __init__ (self , documents , score = None ):
725
- self .documents = self . cast_as_value (documents )
725
+ self .documents = cast_as_value (documents )
726
726
self .score = score
727
727
super ().__init__ ()
728
728
@@ -929,12 +929,12 @@ def __init__(
929
929
exact = None ,
930
930
filter = None ,
931
931
):
932
- self .path = self . cast_as_field (path )
933
- self .query_vector = self . cast_as_value (query_vector )
934
- self .limit = self . cast_as_value (limit )
935
- self .num_candidates = self . cast_as_value (num_candidates )
936
- self .exact = self . cast_as_value (exact )
937
- self .filter = self . cast_as_value (filter )
932
+ self .path = cast_as_field (path )
933
+ self .query_vector = cast_as_value (query_vector )
934
+ self .limit = cast_as_value (limit )
935
+ self .num_candidates = cast_as_value (num_candidates )
936
+ self .exact = cast_as_value (exact )
937
+ self .filter = cast_as_value (filter )
938
938
super ().__init__ ()
939
939
940
940
def __invert__ (self ):
@@ -1001,8 +1001,11 @@ def as_mql(self, compiler, connection):
1001
1001
return {"$vectorSearch" : params }
1002
1002
1003
1003
1004
- class SearchScoreOption :
1004
+ class SearchScoreOption ( Expression ) :
1005
1005
"""Class to mutate scoring on a search operation"""
1006
1006
1007
1007
def __init__ (self , definitions = None ):
1008
1008
self .definitions = definitions
1009
+
1010
+ def as_mql (self , compiler , connection ):
1011
+ return self .definitions
0 commit comments