@@ -260,20 +260,23 @@ def _should_index(self, instance):
260
260
261
261
def _should_really_index (self , instance ):
262
262
"""Return True if according to should_index the object should be indexed."""
263
+ if self .should_index is None :
264
+ raise AlgoliaIndexError ("{} should be defined." .format (self .should_index ))
265
+
263
266
if self ._should_index_is_method :
264
267
is_method = inspect .ismethod (self .should_index )
265
268
try :
266
- count_args = len (inspect .signature (self .should_index ).parameters )
269
+ count_args = len (inspect .signature (self .should_index ).parameters ) # pyright: ignore -- should_index_is_method
267
270
except AttributeError :
268
271
# noinspection PyDeprecation
269
- count_args = len (inspect .getargspec (self .should_index ).args )
272
+ count_args = len (inspect .getfullargspec (self .should_index ).args )
270
273
271
274
if is_method or count_args == 1 :
272
275
# bound method, call with instance
273
- return self .should_index (instance )
276
+ return self .should_index (instance ) # pyright: ignore -- should_index_is_method
274
277
else :
275
278
# unbound method, simply call without arguments
276
- return self .should_index ()
279
+ return self .should_index () # pyright: ignore -- should_index_is_method
277
280
else :
278
281
# property/attribute/Field, evaluate as bool
279
282
attr_type = type (self .should_index )
@@ -312,19 +315,19 @@ def save_record(self, instance, update_fields=None, **kwargs):
312
315
self .delete_record (instance )
313
316
return
314
317
318
+ obj = {}
315
319
try :
316
320
if update_fields :
317
321
obj = self .get_raw_record (instance , update_fields = update_fields )
318
- result = self .__client .partial_update_object (
319
- self .index_name , obj . get ( "objectID" ), obj
322
+ self .__client .partial_update_objects (
323
+ index_name = self .index_name , objects = [ obj ], wait_for_tasks = True
320
324
)
321
325
else :
322
326
obj = self .get_raw_record (instance )
323
- result = self .__client .save_object (
324
- self .index_name , obj . get ( "objectID" ), obj
327
+ self .__client .save_objects (
328
+ index_name = self .index_name , objects = [ obj ], wait_for_tasks = True
325
329
)
326
330
logger .info ("SAVE %s FROM %s" , obj ["objectID" ], self .model )
327
- return result
328
331
except AlgoliaException as e :
329
332
if DEBUG :
330
333
raise e
@@ -337,7 +340,9 @@ def delete_record(self, instance):
337
340
"""Deletes the record."""
338
341
objectID = self .objectID (instance )
339
342
try :
340
- self .__client .delete_object (self .index_name , objectID )
343
+ self .__client .delete_objects (
344
+ index_name = self .index_name , object_ids = [objectID ], wait_for_tasks = True
345
+ )
341
346
logger .info ("DELETE %s FROM %s" , objectID , self .model )
342
347
except AlgoliaException as e :
343
348
if DEBUG :
@@ -373,11 +378,15 @@ def update_records(self, qs, batch_size=1000, **kwargs):
373
378
batch .append (dict (tmp ))
374
379
375
380
if len (batch ) >= batch_size :
376
- self .__client .partial_update_objects (self .index_name , batch )
381
+ self .__client .partial_update_objects (
382
+ index_name = self .index_name , objects = batch , wait_for_tasks = True
383
+ )
377
384
batch = []
378
385
379
386
if len (batch ) > 0 :
380
- self .__client .partial_update_objects (self .index_name , batch )
387
+ self .__client .partial_update_objects (
388
+ index_name = self .index_name , objects = batch , wait_for_tasks = True
389
+ )
381
390
382
391
def raw_search (self , query = "" , params = None ):
383
392
"""Performs a search query and returns the parsed JSON."""
@@ -387,7 +396,7 @@ def raw_search(self, query="", params=None):
387
396
params .query = query
388
397
389
398
try :
390
- return self .__client .search_single_index (self .index_name , params )
399
+ return self .__client .search_single_index (self .index_name , params ). to_dict ()
391
400
except AlgoliaException as e :
392
401
if DEBUG :
393
402
raise e
@@ -398,7 +407,7 @@ def get_settings(self):
398
407
"""Returns the settings of the index."""
399
408
try :
400
409
logger .info ("GET SETTINGS ON %s" , self .index_name )
401
- return self .__client .get_settings (self .index_name )
410
+ return self .__client .get_settings (self .index_name ). to_dict ()
402
411
except AlgoliaException as e :
403
412
if DEBUG :
404
413
raise e
@@ -411,7 +420,8 @@ def set_settings(self):
411
420
return
412
421
413
422
try :
414
- self .__client .set_settings (self .index_name , self .settings )
423
+ _resp = self .__client .set_settings (self .index_name , self .settings )
424
+ self .__client .wait_for_task (self .index_name , _resp .task_id )
415
425
logger .info ("APPLY SETTINGS ON %s" , self .index_name )
416
426
except AlgoliaException as e :
417
427
if DEBUG :
@@ -422,18 +432,15 @@ def set_settings(self):
422
432
def clear_objects (self ):
423
433
"""Clears all objects of an index."""
424
434
try :
425
- self .__client .clear_objects (self .index_name )
435
+ _resp = self .__client .clear_objects (self .index_name )
436
+ self .__client .wait_for_task (self .index_name , _resp .task_id )
426
437
logger .info ("CLEAR INDEX %s" , self .index_name )
427
438
except AlgoliaException as e :
428
439
if DEBUG :
429
440
raise e
430
441
else :
431
442
logger .warning ("%s NOT CLEARED: %s" , self .model , e )
432
443
433
- def clear_index (self ):
434
- # TODO: add deprecated warning
435
- self .clear_objects ()
436
-
437
444
def wait_task (self , task_id ):
438
445
try :
439
446
self .__client .wait_for_task (self .index_name , task_id )
@@ -445,9 +452,11 @@ def wait_task(self, task_id):
445
452
logger .warning ("%s NOT WAIT: %s" , self .model , e )
446
453
447
454
def delete (self ):
448
- self .__client .delete_index (self .index_name )
455
+ _resp = self .__client .delete_index (self .index_name )
456
+ self .__client .wait_for_task (self .index_name , _resp .task_id )
449
457
if self .tmp_index_name :
450
- self .__client .delete_index (self .tmp_index_name )
458
+ _resp = self .__client .delete_index (self .tmp_index_name )
459
+ self .__client .wait_for_task (self .tmp_index_name , _resp .task_id )
451
460
452
461
def reindex_all (self , batch_size = 1000 ):
453
462
"""
@@ -494,38 +503,35 @@ def reindex_all(self, batch_size=1000):
494
503
self .settings ["slaves" ] = []
495
504
logger .debug ("REMOVE SLAVES FROM SETTINGS" )
496
505
497
- set_settings_response = self .__client .set_settings (
498
- self .tmp_index_name , self .settings
499
- )
500
- self .__client .wait_for_task (
501
- self .tmp_index_name , set_settings_response .task_id
502
- )
506
+ _resp = self .__client .set_settings (self .tmp_index_name , self .settings )
507
+ self .__client .wait_for_task (self .tmp_index_name , _resp .task_id )
503
508
logger .debug ("APPLY SETTINGS ON %s_tmp" , self .index_name )
504
509
505
510
rules = []
506
511
self .__client .browse_rules (
507
- self .index_name , lambda _resp : rules .append (_resp )
512
+ self .index_name , lambda _resp : rules .extend (_resp . hits )
508
513
)
509
514
if len (rules ):
510
515
logger .debug ("Got rules for index %s: %s" , self .index_name , rules )
511
516
should_keep_rules = True
512
517
513
518
synonyms = []
514
519
self .__client .browse_synonyms (
515
- self .index_name , lambda _resp : synonyms .append (_resp )
520
+ self .index_name , lambda _resp : synonyms .extend (_resp . hits )
516
521
)
517
522
if len (synonyms ):
518
523
logger .debug ("Got synonyms for index %s: %s" , self .index_name , rules )
519
524
should_keep_synonyms = True
520
525
521
- self .__client .clear_objects (self .tmp_index_name )
522
- logger .debug ("CLEAR INDEX %s_tmp" , self .index_name )
526
+ _resp = self .__client .clear_objects (self .tmp_index_name )
527
+ self .__client .wait_for_task (self .tmp_index_name , _resp .task_id )
528
+ logger .debug ("CLEAR INDEX %s" , self .tmp_index_name )
523
529
524
530
counts = 0
525
531
batch = []
526
532
527
- if hasattr (self , "get_queryset" ):
528
- qs = self .get_queryset ()
533
+ if hasattr (self , "get_queryset" ) and callable ( self . get_queryset ): # pyright: ignore
534
+ qs = self .get_queryset () # pyright: ignore
529
535
else :
530
536
qs = self .model .objects .all ()
531
537
@@ -535,22 +541,29 @@ def reindex_all(self, batch_size=1000):
535
541
536
542
batch .append (self .get_raw_record (instance ))
537
543
if len (batch ) >= batch_size :
538
- self .__client .save_objects (self .tmp_index_name , batch , True )
544
+ self .__client .save_objects (
545
+ index_name = self .tmp_index_name ,
546
+ objects = batch ,
547
+ wait_for_tasks = True ,
548
+ )
539
549
logger .info (
540
550
"SAVE %d OBJECTS TO %s" , len (batch ), self .tmp_index_name
541
551
)
542
552
batch = []
543
553
counts += 1
544
554
if len (batch ) > 0 :
545
- self .__client .save_objects (self .tmp_index_name , batch , True )
555
+ self .__client .save_objects (
556
+ index_name = self .tmp_index_name , objects = batch , wait_for_tasks = True
557
+ )
546
558
logger .info ("SAVE %d OBJECTS TO %s" , len (batch ), self .tmp_index_name )
547
559
548
- self .__client .operation_index (
560
+ _resp = self .__client .operation_index (
549
561
self .tmp_index_name ,
550
562
OperationIndexParams (
551
563
operation = OperationType .MOVE , destination = self .index_name
552
564
),
553
565
)
566
+ self .__client .wait_for_task (self .tmp_index_name , _resp .task_id )
554
567
logger .info ("MOVE INDEX %s TO %s" , self .tmp_index_name , self .index_name )
555
568
556
569
if self .settings :
@@ -561,31 +574,20 @@ def reindex_all(self, batch_size=1000):
561
574
self .settings ["slaves" ] = slaves
562
575
logger .debug ("RESTORE SLAVES" )
563
576
if should_keep_replicas or should_keep_slaves :
564
- self .__client .set_settings (self .index_name , self .settings )
577
+ _resp = self .__client .set_settings (self .index_name , self .settings )
578
+ self .__client .wait_for_task (self .tmp_index_name , _resp .task_id )
565
579
if should_keep_rules :
566
- save_rules_response = self .__client .save_rules (
567
- self .index_name , rules , True
568
- )
569
- self .__client .wait_for_task (
570
- self .index_name , save_rules_response .task_id
571
- )
580
+ _resp = self .__client .save_rules (self .index_name , rules , True )
581
+ self .__client .wait_for_task (self .index_name , _resp .task_id )
572
582
logger .info (
573
- "Saved rules for index %s with response: {}" .format (
574
- save_rules_response
575
- ),
583
+ "Saved rules for index %s with response: {}" .format (_resp ),
576
584
self .index_name ,
577
585
)
578
586
if should_keep_synonyms :
579
- save_synonyms_response = self .__client .save_synonyms (
580
- self .index_name , synonyms , True
581
- )
582
- self .__client .wait_for_task (
583
- self .index_name , save_synonyms_response .task_id
584
- )
587
+ _resp = self .__client .save_synonyms (self .index_name , synonyms , True )
588
+ self .__client .wait_for_task (self .index_name , _resp .task_id )
585
589
logger .info (
586
- "Saved synonyms for index %s with response: {}" .format (
587
- save_synonyms_response
588
- ),
590
+ "Saved synonyms for index %s with response: {}" .format (_resp ),
589
591
self .index_name ,
590
592
)
591
593
return counts
0 commit comments