@@ -260,20 +260,23 @@ def _should_index(self, instance):
260260
261261 def _should_really_index (self , instance ):
262262 """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+
263266 if self ._should_index_is_method :
264267 is_method = inspect .ismethod (self .should_index )
265268 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
267270 except AttributeError :
268271 # noinspection PyDeprecation
269- count_args = len (inspect .getargspec (self .should_index ).args )
272+ count_args = len (inspect .getfullargspec (self .should_index ).args )
270273
271274 if is_method or count_args == 1 :
272275 # bound method, call with instance
273- return self .should_index (instance )
276+ return self .should_index (instance ) # pyright: ignore -- should_index_is_method
274277 else :
275278 # unbound method, simply call without arguments
276- return self .should_index ()
279+ return self .should_index () # pyright: ignore -- should_index_is_method
277280 else :
278281 # property/attribute/Field, evaluate as bool
279282 attr_type = type (self .should_index )
@@ -312,19 +315,19 @@ def save_record(self, instance, update_fields=None, **kwargs):
312315 self .delete_record (instance )
313316 return
314317
318+ obj = {}
315319 try :
316320 if update_fields :
317321 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
320324 )
321325 else :
322326 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
325329 )
326330 logger .info ("SAVE %s FROM %s" , obj ["objectID" ], self .model )
327- return result
328331 except AlgoliaException as e :
329332 if DEBUG :
330333 raise e
@@ -337,7 +340,9 @@ def delete_record(self, instance):
337340 """Deletes the record."""
338341 objectID = self .objectID (instance )
339342 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+ )
341346 logger .info ("DELETE %s FROM %s" , objectID , self .model )
342347 except AlgoliaException as e :
343348 if DEBUG :
@@ -373,11 +378,15 @@ def update_records(self, qs, batch_size=1000, **kwargs):
373378 batch .append (dict (tmp ))
374379
375380 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+ )
377384 batch = []
378385
379386 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+ )
381390
382391 def raw_search (self , query = "" , params = None ):
383392 """Performs a search query and returns the parsed JSON."""
@@ -387,7 +396,7 @@ def raw_search(self, query="", params=None):
387396 params .query = query
388397
389398 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 ()
391400 except AlgoliaException as e :
392401 if DEBUG :
393402 raise e
@@ -398,7 +407,7 @@ def get_settings(self):
398407 """Returns the settings of the index."""
399408 try :
400409 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 ()
402411 except AlgoliaException as e :
403412 if DEBUG :
404413 raise e
@@ -411,7 +420,8 @@ def set_settings(self):
411420 return
412421
413422 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 )
415425 logger .info ("APPLY SETTINGS ON %s" , self .index_name )
416426 except AlgoliaException as e :
417427 if DEBUG :
@@ -422,18 +432,15 @@ def set_settings(self):
422432 def clear_objects (self ):
423433 """Clears all objects of an index."""
424434 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 )
426437 logger .info ("CLEAR INDEX %s" , self .index_name )
427438 except AlgoliaException as e :
428439 if DEBUG :
429440 raise e
430441 else :
431442 logger .warning ("%s NOT CLEARED: %s" , self .model , e )
432443
433- def clear_index (self ):
434- # TODO: add deprecated warning
435- self .clear_objects ()
436-
437444 def wait_task (self , task_id ):
438445 try :
439446 self .__client .wait_for_task (self .index_name , task_id )
@@ -445,9 +452,11 @@ def wait_task(self, task_id):
445452 logger .warning ("%s NOT WAIT: %s" , self .model , e )
446453
447454 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 )
449457 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 )
451460
452461 def reindex_all (self , batch_size = 1000 ):
453462 """
@@ -494,38 +503,35 @@ def reindex_all(self, batch_size=1000):
494503 self .settings ["slaves" ] = []
495504 logger .debug ("REMOVE SLAVES FROM SETTINGS" )
496505
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 )
503508 logger .debug ("APPLY SETTINGS ON %s_tmp" , self .index_name )
504509
505510 rules = []
506511 self .__client .browse_rules (
507- self .index_name , lambda _resp : rules .append (_resp )
512+ self .index_name , lambda _resp : rules .extend (_resp . hits )
508513 )
509514 if len (rules ):
510515 logger .debug ("Got rules for index %s: %s" , self .index_name , rules )
511516 should_keep_rules = True
512517
513518 synonyms = []
514519 self .__client .browse_synonyms (
515- self .index_name , lambda _resp : synonyms .append (_resp )
520+ self .index_name , lambda _resp : synonyms .extend (_resp . hits )
516521 )
517522 if len (synonyms ):
518523 logger .debug ("Got synonyms for index %s: %s" , self .index_name , rules )
519524 should_keep_synonyms = True
520525
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 )
523529
524530 counts = 0
525531 batch = []
526532
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
529535 else :
530536 qs = self .model .objects .all ()
531537
@@ -535,22 +541,29 @@ def reindex_all(self, batch_size=1000):
535541
536542 batch .append (self .get_raw_record (instance ))
537543 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+ )
539549 logger .info (
540550 "SAVE %d OBJECTS TO %s" , len (batch ), self .tmp_index_name
541551 )
542552 batch = []
543553 counts += 1
544554 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+ )
546558 logger .info ("SAVE %d OBJECTS TO %s" , len (batch ), self .tmp_index_name )
547559
548- self .__client .operation_index (
560+ _resp = self .__client .operation_index (
549561 self .tmp_index_name ,
550562 OperationIndexParams (
551563 operation = OperationType .MOVE , destination = self .index_name
552564 ),
553565 )
566+ self .__client .wait_for_task (self .tmp_index_name , _resp .task_id )
554567 logger .info ("MOVE INDEX %s TO %s" , self .tmp_index_name , self .index_name )
555568
556569 if self .settings :
@@ -561,31 +574,20 @@ def reindex_all(self, batch_size=1000):
561574 self .settings ["slaves" ] = slaves
562575 logger .debug ("RESTORE SLAVES" )
563576 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 )
565579 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 )
572582 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 ),
576584 self .index_name ,
577585 )
578586 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 )
585589 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 ),
589591 self .index_name ,
590592 )
591593 return counts
0 commit comments