@@ -20,16 +20,18 @@ class AlgoliaSearchClient:
20
20
ALGOLIA_APPLICATION_ID = settings .ALGOLIA .get ('APPLICATION_ID' )
21
21
ALGOLIA_API_KEY = settings .ALGOLIA .get ('API_KEY' )
22
22
ALGOLIA_INDEX_NAME = settings .ALGOLIA .get ('INDEX_NAME' )
23
+ ALGOLIA_REPLICA_INDEX_NAME = settings .ALGOLIA .get ('REPLICA_INDEX_NAME' )
23
24
24
25
def __init__ (self ):
25
26
self ._client = None
26
27
self .algolia_index = None
28
+ self .replica_index = None
27
29
28
30
def init_index (self ):
29
31
"""
30
32
Initializes an index within Algolia. Initializing an index will create it if it doesn't exist.
31
33
"""
32
- if not self .ALGOLIA_INDEX_NAME :
34
+ if not self .ALGOLIA_INDEX_NAME or not self . ALGOLIA_REPLICA_INDEX_NAME :
33
35
logger .error ('Could not initialize Algolia index due to missing index name.' )
34
36
return
35
37
@@ -44,14 +46,15 @@ def init_index(self):
44
46
self ._client = SearchClient .create (self .ALGOLIA_APPLICATION_ID , self .ALGOLIA_API_KEY )
45
47
try :
46
48
self .algolia_index = self ._client .init_index (self .ALGOLIA_INDEX_NAME )
49
+ self .replica_index = self ._client .init_index (self .ALGOLIA_REPLICA_INDEX_NAME )
47
50
except AlgoliaException as exc :
48
51
logger .exception (
49
52
'Could not initialize %s index in Algolia due to an exception.' ,
50
53
self .ALGOLIA_INDEX_NAME ,
51
54
)
52
55
raise exc
53
56
54
- def set_index_settings (self , index_settings ):
57
+ def set_index_settings (self , index_settings , primary_index = True ):
55
58
"""
56
59
Set default settings to use for the Algolia index.
57
60
@@ -66,7 +69,10 @@ def set_index_settings(self, index_settings):
66
69
return
67
70
68
71
try :
69
- self .algolia_index .set_settings (index_settings )
72
+ if primary_index :
73
+ self .algolia_index .set_settings (index_settings )
74
+ else :
75
+ self .replica_index .set_settings (index_settings )
70
76
except AlgoliaException as exc :
71
77
logger .exception (
72
78
'Unable to set settings for Algolia\' s %s index due to an exception.' ,
@@ -78,18 +84,24 @@ def index_exists(self):
78
84
"""
79
85
Returns whether the index exists in Algolia.
80
86
"""
81
- if not self .algolia_index :
87
+ if not self .algolia_index or not self . replica_index :
82
88
logger .error ('Algolia index does not exist. Did you initialize it?' )
83
89
return False
84
90
85
- exists = self .algolia_index .exists ()
86
- if not exists :
91
+ primary_exists = self .algolia_index .exists ()
92
+ replica_exists = self .replica_index .exists ()
93
+ if not primary_exists :
87
94
logger .warning (
88
95
'Index with name %s does not exist in Algolia.' ,
89
96
self .ALGOLIA_INDEX_NAME ,
90
97
)
98
+ if not replica_exists :
99
+ logger .warning (
100
+ 'Index with name %s does not exist in Algolia.' ,
101
+ self .ALGOLIA_REPLICA_INDEX_NAME ,
102
+ )
91
103
92
- return exists
104
+ return primary_exists and replica_exists
93
105
94
106
def replace_all_objects (self , algolia_objects ): # pragma: no cover
95
107
"""
0 commit comments