@@ -214,8 +214,7 @@ def value(self, compiler, connection): # noqa: ARG001
214
214
215
215
216
216
class SearchExpression (Expression ):
217
- def __init__ (self ):
218
- super ().__init__ (output_field = FloatField ())
217
+ output_field = FloatField ()
219
218
220
219
def get_source_expressions (self ):
221
220
return []
@@ -240,34 +239,37 @@ def _get_query_index(self, fields, compiler):
240
239
241
240
242
241
class SearchAutocomplete (SearchExpression ):
243
- def __init__ (self , path , query , score = None ):
244
- self .path = F (path )
245
- self .query = Value (query )
242
+ def __init__ (self , path , query , fuzzy = None , score = None ):
243
+ self .path = path
244
+ self .query = query
245
+ self .fuzzy = fuzzy
246
246
self .score = score
247
247
super ().__init__ ()
248
248
249
249
def as_mql (self , compiler , connection ):
250
250
params = {
251
- "path" : self .path . as_mql ( compiler , connection )[ 1 :] ,
252
- "query" : self .query . as_mql ( compiler , connection ) ,
251
+ "path" : self .path ,
252
+ "query" : self .query ,
253
253
}
254
254
if self .score is not None :
255
255
params ["score" ] = self .score
256
+ if self .fuzzy is not None :
257
+ params ["fuzzy" ] = self .fuzzy
256
258
index = self ._get_query_index ([self .path ], compiler )
257
259
return {"$search" : {"autocomplete" : params , "index" : index }}
258
260
259
261
260
262
class SearchEquals (SearchExpression ):
261
263
def __init__ (self , path , value , score = None ):
262
- self .path = F ( path )
263
- self .value = Value ( query )
264
+ self .path = path
265
+ self .value = value
264
266
self .score = score
265
267
super ().__init__ ()
266
268
267
269
def as_mql (self , compiler , connection ):
268
270
params = {
269
- "path" : self .path . as_mql ( compiler , connection )[ 1 :] ,
270
- "value" : self .value . as_mql ( compiler , connection ) ,
271
+ "path" : self .path ,
272
+ "value" : self .value ,
271
273
}
272
274
if self .score is not None :
273
275
params ["score" ] = self .score
@@ -277,13 +279,13 @@ def as_mql(self, compiler, connection):
277
279
278
280
class SearchExists (SearchExpression ):
279
281
def __init__ (self , path , score = None ):
280
- self .path = F ( path )
282
+ self .path = path
281
283
self .score = score
282
284
super ().__init__ ()
283
285
284
286
def as_mql (self , compiler , connection ):
285
287
params = {
286
- "path" : self .path . as_mql ( compiler , connection )[ 1 :] ,
288
+ "path" : self .path ,
287
289
}
288
290
if self .score is not None :
289
291
params ["score" ] = self .score
@@ -293,15 +295,15 @@ def as_mql(self, compiler, connection):
293
295
294
296
class SearchIn (SearchExpression ):
295
297
def __init__ (self , path , value , score = None ):
296
- self .path = F ( path )
297
- self .value = Value ( value )
298
+ self .path = path
299
+ self .value = value
298
300
self .score = score
299
301
super ().__init__ ()
300
302
301
303
def as_mql (self , compiler , connection ):
302
304
params = {
303
- "path" : self .path . as_mql ( compiler , connection )[ 1 :] ,
304
- "value" : self .value . as_mql ( compiler , connection ) ,
305
+ "path" : self .path ,
306
+ "value" : self .value ,
305
307
}
306
308
if self .score is not None :
307
309
params ["score" ] = self .score
@@ -310,18 +312,18 @@ def as_mql(self, compiler, connection):
310
312
311
313
312
314
class SearchPhrase (SearchExpression ):
313
- def __init__ (self , path , value , slop = None , synonyms = None , score = None ):
314
- self .path = F ( path )
315
- self .value = Value ( value )
315
+ def __init__ (self , path , query , slop = None , synonyms = None , score = None ):
316
+ self .path = path
317
+ self .query = query
316
318
self .score = score
317
319
self .slop = slop
318
320
self .synonyms = synonyms
319
321
super ().__init__ ()
320
322
321
323
def as_mql (self , compiler , connection ):
322
324
params = {
323
- "path" : self .path . as_mql ( compiler , connection )[ 1 :] ,
324
- "value " : self .value . as_mql ( compiler , connection ) ,
325
+ "path" : self .path ,
326
+ "query " : self .query ,
325
327
}
326
328
if self .score is not None :
327
329
params ["score" ] = self .score
@@ -335,15 +337,15 @@ def as_mql(self, compiler, connection):
335
337
336
338
class SearchQueryString (SearchExpression ):
337
339
def __init__ (self , path , query , score = None ):
338
- self .path = F ( path )
339
- self .query = Value ( query )
340
+ self .path = path
341
+ self .query = query
340
342
self .score = score
341
343
super ().__init__ ()
342
344
343
345
def as_mql (self , compiler , connection ):
344
346
params = {
345
- "defaultPath" : self .path . as_mql ( compiler , connection )[ 1 :] ,
346
- "query" : self .query . as_mql ( compiler , connection ) ,
347
+ "defaultPath" : self .path ,
348
+ "query" : self .query ,
347
349
}
348
350
if self .score is not None :
349
351
params ["score" ] = self .score
@@ -353,132 +355,138 @@ def as_mql(self, compiler, connection):
353
355
354
356
class SearchRange (SearchExpression ):
355
357
def __init__ (self , path , lt = None , lte = None , gt = None , gte = None , score = None ):
356
- self .path = F ( path )
357
- self .lt = Value ( lt )
358
- self .lte = Value ( lte )
359
- self .gt = Value ( gt )
360
- self .gte = Value ( gte )
358
+ self .path = path
359
+ self .lt = lt
360
+ self .lte = lte
361
+ self .gt = gt
362
+ self .gte = gte
361
363
self .score = score
362
364
super ().__init__ ()
363
365
364
366
def as_mql (self , compiler , connection ):
365
367
params = {
366
- "path" : self .path . as_mql ( compiler , connection )[ 1 :] ,
368
+ "path" : self .path ,
367
369
}
368
370
if self .score is not None :
369
371
params ["score" ] = self .score
370
372
if self .lt is not None :
371
- params ["lt" ] = self .lt . as_mql ( compiler , connection )
373
+ params ["lt" ] = self .lt
372
374
if self .lte is not None :
373
- params ["lte" ] = self .lte . as_mql ( compiler , connection )
375
+ params ["lte" ] = self .lte
374
376
if self .gt is not None :
375
- params ["gt" ] = self .gt . as_mql ( compiler , connection )
377
+ params ["gt" ] = self .gt
376
378
if self .gte is not None :
377
- params ["gte" ] = self .gte . as_mql ( compiler , connection )
379
+ params ["gte" ] = self .gte
378
380
index = self ._get_query_index ([self .path ], compiler )
379
381
return {"$search" : {"range" : params , "index" : index }}
380
382
381
383
382
384
class SearchRegex (SearchExpression ):
383
385
def __init__ (self , path , query , allow_analyzed_field = None , score = None ):
384
- self .path = F (path )
385
- self .allow_analyzed_field = Value (allow_analyzed_field )
386
+ self .path = path
387
+ self .query = query
388
+ self .allow_analyzed_field = allow_analyzed_field
386
389
self .score = score
387
390
super ().__init__ ()
388
391
389
392
def as_mql (self , compiler , connection ):
390
393
params = {
391
- "path" : self .path .as_mql (compiler , connection )[1 :],
394
+ "path" : self .path ,
395
+ "query" : self .query ,
392
396
}
393
397
if self .score :
394
398
params ["score" ] = self .score
395
399
if self .allow_analyzed_field is not None :
396
- params ["allowAnalyzedField" ] = self .allow_analyzed_field . as_mql ( compiler , connection )
400
+ params ["allowAnalyzedField" ] = self .allow_analyzed_field
397
401
index = self ._get_query_index ([self .path ], compiler )
398
402
return {"$search" : {"regex" : params , "index" : index }}
399
403
400
404
401
405
class SearchText (SearchExpression ):
402
406
def __init__ (self , path , query , fuzzy = None , match_criteria = None , synonyms = None , score = None ):
403
- self .path = F (path )
404
- self .fuzzy = Value (fuzzy )
405
- self .match_criteria = Value (match_criteria )
406
- self .synonyms = Value (synonyms )
407
+ self .path = path
408
+ self .query = query
409
+ self .fuzzy = fuzzy
410
+ self .match_criteria = match_criteria
411
+ self .synonyms = synonyms
407
412
self .score = score
408
413
super ().__init__ ()
409
414
410
415
def as_mql (self , compiler , connection ):
411
416
params = {
412
- "path" : self .path .as_mql (compiler , connection )[1 :],
417
+ "path" : self .path ,
418
+ "query" : self .query ,
413
419
}
414
420
if self .score :
415
421
params ["score" ] = self .score
416
422
if self .fuzzy is not None :
417
- params ["fuzzy" ] = self .fuzzy . as_mql ( compiler , connection )
423
+ params ["fuzzy" ] = self .fuzzy
418
424
if self .match_criteria is not None :
419
- params ["matchCriteria" ] = self .match_criteria . as_mql ( compiler , connection )
425
+ params ["matchCriteria" ] = self .match_criteria
420
426
if self .synonyms is not None :
421
- params ["synonyms" ] = self .synonyms . as_mql ( compiler , connection )
427
+ params ["synonyms" ] = self .synonyms
422
428
index = self ._get_query_index ([self .path ], compiler )
423
429
return {"$search" : {"text" : params , "index" : index }}
424
430
425
431
426
432
class SearchWildcard (SearchExpression ):
427
433
def __init__ (self , path , query , allow_analyzed_field = None , score = None ):
428
- self .path = F (path )
429
- self .allow_analyzed_field = Value (allow_analyzed_field )
434
+ self .path = path
435
+ self .query = query
436
+ self .allow_analyzed_field = allow_analyzed_field
430
437
self .score = score
431
438
super ().__init__ ()
432
439
433
440
def as_mql (self , compiler , connection ):
434
441
params = {
435
- "path" : self .path .as_mql (compiler , connection )[1 :],
442
+ "path" : self .path ,
443
+ "query" : self .query ,
436
444
}
437
445
if self .score :
438
446
params ["score" ] = self .score
439
447
if self .allow_analyzed_field is not None :
440
- params ["allowAnalyzedField" ] = self .allow_analyzed_field . as_mql ( compiler , connection )
448
+ params ["allowAnalyzedField" ] = self .allow_analyzed_field
441
449
index = self ._get_query_index ([self .path ], compiler )
442
450
return {"$search" : {"wildcard" : params , "index" : index }}
443
451
444
452
445
453
class SearchGeoShape (SearchExpression ):
446
454
def __init__ (self , path , relation , geometry , score = None ):
447
- self .path = F ( path )
455
+ self .path = path
448
456
self .relation = relation
449
457
self .geometry = geometry
450
458
self .score = score
451
459
super ().__init__ ()
452
460
453
461
def as_mql (self , compiler , connection ):
454
462
params = {
455
- "path" : self .path . as_mql ( compiler , connection )[ 1 :] ,
463
+ "path" : self .path ,
456
464
"relation" : self .relation ,
457
465
"geometry" : self .geometry ,
458
466
}
459
467
if self .score :
460
468
params ["score" ] = self .score
461
469
index = self ._get_query_index ([self .path ], compiler )
462
- return {"$search" : {"wildcard " : params , "index" : index }}
470
+ return {"$search" : {"geoShape " : params , "index" : index }}
463
471
464
472
465
473
class SearchGeoWithin (SearchExpression ):
466
- def __init__ (self , path , kind , geo_object , geometry , score = None ):
467
- self .path = F ( path )
474
+ def __init__ (self , path , kind , geo_object , score = None ):
475
+ self .path = path
468
476
self .kind = kind
469
477
self .geo_object = geo_object
470
478
self .score = score
471
479
super ().__init__ ()
472
480
473
481
def as_mql (self , compiler , connection ):
474
482
params = {
475
- "path" : self .path . as_mql ( compiler , connection )[ 1 :] ,
483
+ "path" : self .path ,
476
484
self .kind : self .geo_object ,
477
485
}
478
486
if self .score :
479
487
params ["score" ] = self .score
480
488
index = self ._get_query_index ([self .path ], compiler )
481
- return {"$search" : {"wildcard " : params , "index" : index }}
489
+ return {"$search" : {"geoWithin " : params , "index" : index }}
482
490
483
491
484
492
class SearchMoreLikeThis (SearchExpression ):
@@ -497,7 +505,22 @@ def as_mql(self, compiler, connection):
497
505
for doc in self .documents :
498
506
needed_fields += list (doc .keys ())
499
507
index = self ._get_query_index (needed_fields , compiler )
500
- return {"$search" : {"wildcard" : params , "index" : index }}
508
+ return {"$search" : {"moreLikeThis" : params , "index" : index }}
509
+
510
+
511
+ class SearchScoreOption :
512
+ """Class to mutate scoring on a search operation"""
513
+
514
+ def __init__ (self , definitions = None ):
515
+ self .definitions = definitions
516
+
517
+
518
+ class CombinedSearchExpression (SearchExpression ):
519
+ def __init__ (self , lhs , connector , rhs , output_field = None ):
520
+ super ().__init__ (output_field = output_field )
521
+ self .connector = connector
522
+ self .lhs = lhs
523
+ self .rhs = rhs
501
524
502
525
503
526
def register_expressions ():
0 commit comments