6
6
import logging
7
7
8
8
from 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
9
12
from django .db .models .query_utils import DeferredAttribute
10
13
11
14
from .settings import DEBUG
@@ -71,7 +74,7 @@ class AlgoliaIndex(object):
71
74
72
75
def __init__ (self , model , client , settings ):
73
76
"""Initializes the index."""
74
- self .__init_index (client , model , settings )
77
+ self .__init_index (model , settings )
75
78
76
79
self .model = model
77
80
self .__client = client
@@ -170,7 +173,7 @@ def __init__(self, model, client, settings):
170
173
)
171
174
)
172
175
173
- def __init_index (self , client , model , settings ):
176
+ def __init_index (self , model , settings ):
174
177
if not self .index_name :
175
178
self .index_name = model .__name__
176
179
@@ -189,9 +192,6 @@ def __init_index(self, client, model, settings):
189
192
190
193
self .tmp_index_name = tmp_index_name
191
194
192
- self .__index = client .init_index (self .index_name )
193
- self .__tmp_index = client .init_index (self .tmp_index_name )
194
-
195
195
@staticmethod
196
196
def _validate_geolocation (geolocation ):
197
197
"""
@@ -315,10 +315,14 @@ def save_record(self, instance, update_fields=None, **kwargs):
315
315
try :
316
316
if update_fields :
317
317
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
+ )
319
321
else :
320
322
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
+ )
322
326
logger .info ("SAVE %s FROM %s" , obj ["objectID" ], self .model )
323
327
return result
324
328
except AlgoliaException as e :
@@ -333,7 +337,7 @@ def delete_record(self, instance):
333
337
"""Deletes the record."""
334
338
objectID = self .objectID (instance )
335
339
try :
336
- self .__index .delete_object (objectID )
340
+ self .__client .delete_object (self . index_name , objectID )
337
341
logger .info ("DELETE %s FROM %s" , objectID , self .model )
338
342
except AlgoliaException as e :
339
343
if DEBUG :
@@ -369,19 +373,21 @@ def update_records(self, qs, batch_size=1000, **kwargs):
369
373
batch .append (dict (tmp ))
370
374
371
375
if len (batch ) >= batch_size :
372
- self .__index .partial_update_objects (batch )
376
+ self .__client .partial_update_objects (self . index_name , batch )
373
377
batch = []
374
378
375
379
if len (batch ) > 0 :
376
- self .__index .partial_update_objects (batch )
380
+ self .__client .partial_update_objects (self . index_name , batch )
377
381
378
382
def raw_search (self , query = "" , params = None ):
379
383
"""Performs a search query and returns the parsed JSON."""
380
384
if params is None :
381
- params = {}
385
+ params = SearchParamsObject ()
386
+
387
+ params .query = query
382
388
383
389
try :
384
- return self .__index . search ( query , params )
390
+ return self .__client . search_single_index ( self . index_name , params )
385
391
except AlgoliaException as e :
386
392
if DEBUG :
387
393
raise e
@@ -392,7 +398,7 @@ def get_settings(self):
392
398
"""Returns the settings of the index."""
393
399
try :
394
400
logger .info ("GET SETTINGS ON %s" , self .index_name )
395
- return self .__index .get_settings ()
401
+ return self .__client .get_settings (self . index_name )
396
402
except AlgoliaException as e :
397
403
if DEBUG :
398
404
raise e
@@ -405,7 +411,7 @@ def set_settings(self):
405
411
return
406
412
407
413
try :
408
- self .__index .set_settings (self .settings )
414
+ self .__client .set_settings (self . index_name , self .settings )
409
415
logger .info ("APPLY SETTINGS ON %s" , self .index_name )
410
416
except AlgoliaException as e :
411
417
if DEBUG :
@@ -416,7 +422,7 @@ def set_settings(self):
416
422
def clear_objects (self ):
417
423
"""Clears all objects of an index."""
418
424
try :
419
- self .__index .clear_objects ()
425
+ self .__client .clear_objects (self . index_name )
420
426
logger .info ("CLEAR INDEX %s" , self .index_name )
421
427
except AlgoliaException as e :
422
428
if DEBUG :
@@ -430,7 +436,7 @@ def clear_index(self):
430
436
431
437
def wait_task (self , task_id ):
432
438
try :
433
- self .__index . wait_task ( task_id )
439
+ self .__client . wait_for_task ( self . index_name , task_id )
434
440
logger .info ("WAIT TASK %s" , self .index_name )
435
441
except AlgoliaException as e :
436
442
if DEBUG :
@@ -439,9 +445,9 @@ def wait_task(self, task_id):
439
445
logger .warning ("%s NOT WAIT: %s" , self .model , e )
440
446
441
447
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 )
445
451
446
452
def reindex_all (self , batch_size = 1000 ):
447
453
"""
@@ -469,6 +475,11 @@ def reindex_all(self, batch_size=1000):
469
475
else :
470
476
raise e # Unexpected error while getting settings
471
477
try :
478
+ should_keep_replicas = False
479
+ should_keep_slaves = False
480
+ replicas = None
481
+ slaves = None
482
+
472
483
if self .settings :
473
484
replicas = self .settings .get ("replicas" , None )
474
485
slaves = self .settings .get ("slaves" , None )
@@ -483,22 +494,31 @@ def reindex_all(self, batch_size=1000):
483
494
self .settings ["slaves" ] = []
484
495
logger .debug ("REMOVE SLAVES FROM SETTINGS" )
485
496
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
+ )
487
503
logger .debug ("APPLY SETTINGS ON %s_tmp" , self .index_name )
504
+
488
505
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
+ )
494
509
if len (rules ):
495
510
logger .debug ("Got rules for index %s: %s" , self .index_name , rules )
496
511
should_keep_rules = True
512
+
513
+ synonyms = []
514
+ self .__client .browse_synonyms (
515
+ self .index_name , lambda _resp : synonyms .append (_resp )
516
+ )
497
517
if len (synonyms ):
498
518
logger .debug ("Got synonyms for index %s: %s" , self .index_name , rules )
499
519
should_keep_synonyms = True
500
520
501
- self .__tmp_index .clear_objects ()
521
+ self .__client .clear_objects (self . tmp_index_name )
502
522
logger .debug ("CLEAR INDEX %s_tmp" , self .index_name )
503
523
504
524
counts = 0
@@ -515,18 +535,23 @@ def reindex_all(self, batch_size=1000):
515
535
516
536
batch .append (self .get_raw_record (instance ))
517
537
if len (batch ) >= batch_size :
518
- self .__tmp_index .save_objects (batch )
538
+ self .__client .save_objects (self . tmp_index_name , batch , True )
519
539
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
521
541
)
522
542
batch = []
523
543
counts += 1
524
544
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 )
530
555
531
556
if self .settings :
532
557
if should_keep_replicas :
@@ -536,24 +561,30 @@ def reindex_all(self, batch_size=1000):
536
561
self .settings ["slaves" ] = slaves
537
562
logger .debug ("RESTORE SLAVES" )
538
563
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 )
540
565
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
543
571
)
544
- response .wait ()
545
572
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
+ ),
547
576
self .index_name ,
548
577
)
549
578
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
552
584
)
553
- response .wait ()
554
585
logger .info (
555
586
"Saved synonyms for index %s with response: {}" .format (
556
- response
587
+ save_synonyms_response
557
588
),
558
589
self .index_name ,
559
590
)
0 commit comments