66import logging
77
88from algoliasearch .http .exceptions import AlgoliaException
9+ from algoliasearch .search .models .operation_index_params import OperationIndexParams
10+ from algoliasearch .search .models .operation_type import OperationType
11+ from algoliasearch .search .models .search_params_object import SearchParamsObject
912from django .db .models .query_utils import DeferredAttribute
1013
1114from .settings import DEBUG
@@ -71,7 +74,7 @@ class AlgoliaIndex(object):
7174
7275 def __init__ (self , model , client , settings ):
7376 """Initializes the index."""
74- self .__init_index (client , model , settings )
77+ self .__init_index (model , settings )
7578
7679 self .model = model
7780 self .__client = client
@@ -170,7 +173,7 @@ def __init__(self, model, client, settings):
170173 )
171174 )
172175
173- def __init_index (self , client , model , settings ):
176+ def __init_index (self , model , settings ):
174177 if not self .index_name :
175178 self .index_name = model .__name__
176179
@@ -189,9 +192,6 @@ def __init_index(self, client, model, settings):
189192
190193 self .tmp_index_name = tmp_index_name
191194
192- self .__index = client .init_index (self .index_name )
193- self .__tmp_index = client .init_index (self .tmp_index_name )
194-
195195 @staticmethod
196196 def _validate_geolocation (geolocation ):
197197 """
@@ -315,10 +315,14 @@ def save_record(self, instance, update_fields=None, **kwargs):
315315 try :
316316 if update_fields :
317317 obj = self .get_raw_record (instance , update_fields = update_fields )
318- result = self .__index .partial_update_object (obj )
318+ result = self .__client .partial_update_object (
319+ self .index_name , obj .get ("objectID" ), obj
320+ )
319321 else :
320322 obj = self .get_raw_record (instance )
321- result = self .__index .save_object (obj )
323+ result = self .__client .save_object (
324+ self .index_name , obj .get ("objectID" ), obj
325+ )
322326 logger .info ("SAVE %s FROM %s" , obj ["objectID" ], self .model )
323327 return result
324328 except AlgoliaException as e :
@@ -333,7 +337,7 @@ def delete_record(self, instance):
333337 """Deletes the record."""
334338 objectID = self .objectID (instance )
335339 try :
336- self .__index .delete_object (objectID )
340+ self .__client .delete_object (self . index_name , objectID )
337341 logger .info ("DELETE %s FROM %s" , objectID , self .model )
338342 except AlgoliaException as e :
339343 if DEBUG :
@@ -369,19 +373,21 @@ def update_records(self, qs, batch_size=1000, **kwargs):
369373 batch .append (dict (tmp ))
370374
371375 if len (batch ) >= batch_size :
372- self .__index .partial_update_objects (batch )
376+ self .__client .partial_update_objects (self . index_name , batch )
373377 batch = []
374378
375379 if len (batch ) > 0 :
376- self .__index .partial_update_objects (batch )
380+ self .__client .partial_update_objects (self . index_name , batch )
377381
378382 def raw_search (self , query = "" , params = None ):
379383 """Performs a search query and returns the parsed JSON."""
380384 if params is None :
381- params = {}
385+ params = SearchParamsObject ()
386+
387+ params .query = query
382388
383389 try :
384- return self .__index . search ( query , params )
390+ return self .__client . search_single_index ( self . index_name , params )
385391 except AlgoliaException as e :
386392 if DEBUG :
387393 raise e
@@ -392,7 +398,7 @@ def get_settings(self):
392398 """Returns the settings of the index."""
393399 try :
394400 logger .info ("GET SETTINGS ON %s" , self .index_name )
395- return self .__index .get_settings ()
401+ return self .__client .get_settings (self . index_name )
396402 except AlgoliaException as e :
397403 if DEBUG :
398404 raise e
@@ -405,7 +411,7 @@ def set_settings(self):
405411 return
406412
407413 try :
408- self .__index .set_settings (self .settings )
414+ self .__client .set_settings (self . index_name , self .settings )
409415 logger .info ("APPLY SETTINGS ON %s" , self .index_name )
410416 except AlgoliaException as e :
411417 if DEBUG :
@@ -416,7 +422,7 @@ def set_settings(self):
416422 def clear_objects (self ):
417423 """Clears all objects of an index."""
418424 try :
419- self .__index .clear_objects ()
425+ self .__client .clear_objects (self . index_name )
420426 logger .info ("CLEAR INDEX %s" , self .index_name )
421427 except AlgoliaException as e :
422428 if DEBUG :
@@ -430,7 +436,7 @@ def clear_index(self):
430436
431437 def wait_task (self , task_id ):
432438 try :
433- self .__index . wait_task ( task_id )
439+ self .__client . wait_for_task ( self . index_name , task_id )
434440 logger .info ("WAIT TASK %s" , self .index_name )
435441 except AlgoliaException as e :
436442 if DEBUG :
@@ -439,9 +445,9 @@ def wait_task(self, task_id):
439445 logger .warning ("%s NOT WAIT: %s" , self .model , e )
440446
441447 def delete (self ):
442- self .__index . delete ( )
443- if self .__tmp_index :
444- self .__tmp_index . delete ( )
448+ self .__client . delete_index ( self . index_name )
449+ if self .tmp_index_name :
450+ self .__client . delete_index ( self . tmp_index_name )
445451
446452 def reindex_all (self , batch_size = 1000 ):
447453 """
@@ -469,6 +475,11 @@ def reindex_all(self, batch_size=1000):
469475 else :
470476 raise e # Unexpected error while getting settings
471477 try :
478+ should_keep_replicas = False
479+ should_keep_slaves = False
480+ replicas = None
481+ slaves = None
482+
472483 if self .settings :
473484 replicas = self .settings .get ("replicas" , None )
474485 slaves = self .settings .get ("slaves" , None )
@@ -483,22 +494,31 @@ def reindex_all(self, batch_size=1000):
483494 self .settings ["slaves" ] = []
484495 logger .debug ("REMOVE SLAVES FROM SETTINGS" )
485496
486- self .__tmp_index .set_settings (self .settings ).wait ()
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+ )
487503 logger .debug ("APPLY SETTINGS ON %s_tmp" , self .index_name )
504+
488505 rules = []
489- synonyms = []
490- for r in self .__index .browse_rules ():
491- rules .append (r )
492- for s in self .__index .browse_synonyms ():
493- synonyms .append (s )
506+ self .__client .browse_rules (
507+ self .index_name , lambda _resp : rules .append (_resp )
508+ )
494509 if len (rules ):
495510 logger .debug ("Got rules for index %s: %s" , self .index_name , rules )
496511 should_keep_rules = True
512+
513+ synonyms = []
514+ self .__client .browse_synonyms (
515+ self .index_name , lambda _resp : synonyms .append (_resp )
516+ )
497517 if len (synonyms ):
498518 logger .debug ("Got synonyms for index %s: %s" , self .index_name , rules )
499519 should_keep_synonyms = True
500520
501- self .__tmp_index .clear_objects ()
521+ self .__client .clear_objects (self . tmp_index_name )
502522 logger .debug ("CLEAR INDEX %s_tmp" , self .index_name )
503523
504524 counts = 0
@@ -515,18 +535,23 @@ def reindex_all(self, batch_size=1000):
515535
516536 batch .append (self .get_raw_record (instance ))
517537 if len (batch ) >= batch_size :
518- self .__tmp_index .save_objects (batch )
538+ self .__client .save_objects (self . tmp_index_name , batch , True )
519539 logger .info (
520- "SAVE %d OBJECTS TO %s_tmp " , len (batch ), self .index_name
540+ "SAVE %d OBJECTS TO %s " , len (batch ), self .tmp_index_name
521541 )
522542 batch = []
523543 counts += 1
524544 if len (batch ) > 0 :
525- self .__tmp_index .save_objects (batch )
526- logger .info ("SAVE %d OBJECTS TO %s_tmp" , len (batch ), self .index_name )
527-
528- self .__client .move_index (self .tmp_index_name , self .index_name )
529- logger .info ("MOVE INDEX %s_tmp TO %s" , self .index_name , self .index_name )
545+ self .__client .save_objects (self .tmp_index_name , batch , True )
546+ logger .info ("SAVE %d OBJECTS TO %s" , len (batch ), self .tmp_index_name )
547+
548+ self .__client .operation_index (
549+ self .tmp_index_name ,
550+ OperationIndexParams (
551+ operation = OperationType .MOVE , destination = self .index_name
552+ ),
553+ )
554+ logger .info ("MOVE INDEX %s TO %s" , self .tmp_index_name , self .index_name )
530555
531556 if self .settings :
532557 if should_keep_replicas :
@@ -536,24 +561,30 @@ def reindex_all(self, batch_size=1000):
536561 self .settings ["slaves" ] = slaves
537562 logger .debug ("RESTORE SLAVES" )
538563 if should_keep_replicas or should_keep_slaves :
539- self .__index .set_settings (self .settings )
564+ self .__client .set_settings (self . index_name , self .settings )
540565 if should_keep_rules :
541- response = self .__index .save_rules (
542- rules , {"forwardToReplicas" : True }
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
543571 )
544- response .wait ()
545572 logger .info (
546- "Saved rules for index %s with response: {}" .format (response ),
573+ "Saved rules for index %s with response: {}" .format (
574+ save_rules_response
575+ ),
547576 self .index_name ,
548577 )
549578 if should_keep_synonyms :
550- response = self .__index .save_synonyms (
551- synonyms , {"forwardToReplicas" : True }
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
552584 )
553- response .wait ()
554585 logger .info (
555586 "Saved synonyms for index %s with response: {}" .format (
556- response
587+ save_synonyms_response
557588 ),
558589 self .index_name ,
559590 )
0 commit comments