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