Skip to content

Commit 885c9f1

Browse files
authored
Fix #238: reindex deletes settings (again) (#241)
* chore(reindex_all): log settings if already there * tests: Add test for reindex_all settings conservation * fix(AlgoliaIndex): settings set for each instance, initialized if undefined * fix(apply_some_settings): Reset to former value after setting * refactor(test_reindex_with_settings): with various settings * fix(reindex_all): Keep rules and synonyms over reindex, fixes #238 * refactor(test_index): Store index to delete in tearDown * fix(test_index): Unique replicas for test_with_settings
1 parent 8c55826 commit 885c9f1

File tree

2 files changed

+247
-93
lines changed

2 files changed

+247
-93
lines changed

algoliasearch_django/models.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class AlgoliaIndex(object):
5959
index_name = None
6060

6161
# Use to specify the settings of the index.
62-
settings = {}
62+
settings = None
6363

6464
# Used to specify if the instance should be indexed.
6565
# The attribute should be either:
@@ -80,6 +80,9 @@ def __init__(self, model, client, settings):
8080
self.__named_fields = {}
8181
self.__translate_fields = {}
8282

83+
if self.settings is None: # Only set settings if the actual index class does not define some
84+
self.settings = {}
85+
8386
try:
8487
all_model_fields = [f.name for f in model._meta.get_fields() if not f.is_relation]
8588
except AttributeError: # get_fields requires Django >= 1.8
@@ -416,10 +419,14 @@ def reindex_all(self, batch_size=1000):
416419
a method `get_queryset` in your subclass. This can be used to optimize
417420
the performance (for example with select_related or prefetch_related).
418421
"""
422+
should_keep_synonyms = False
423+
should_keep_rules = False
419424
try:
420425
if not self.settings:
421426
self.settings = self.get_settings()
422427
logger.debug('Got settings for index %s: %s', self.index_name, self.settings)
428+
else:
429+
logger.debug("index %s already has settings: %s", self.index_name, self.settings)
423430
except AlgoliaException as e:
424431
if any("Index does not exist" in arg for arg in e.args):
425432
pass # Expected, let's clear and recreate from scratch
@@ -442,6 +449,19 @@ def reindex_all(self, batch_size=1000):
442449

443450
self.__tmp_index.wait_task(self.__tmp_index.set_settings(self.settings)['taskID'])
444451
logger.debug('APPLY SETTINGS ON %s_tmp', self.index_name)
452+
rules = []
453+
synonyms = []
454+
for r in self.__index.iter_rules():
455+
rules.append(r)
456+
for s in self.__index.iter_synonyms():
457+
synonyms.append(s)
458+
if len(rules):
459+
logger.debug('Got rules for index %s: %s', self.index_name, rules)
460+
should_keep_rules = True
461+
if len(synonyms):
462+
logger.debug('Got synonyms for index %s: %s', self.index_name, rules)
463+
should_keep_synonyms = True
464+
445465
self.__tmp_index.clear_index()
446466
logger.debug('CLEAR INDEX %s_tmp', self.index_name)
447467

@@ -483,6 +503,14 @@ def reindex_all(self, batch_size=1000):
483503
logger.debug("RESTORE SLAVES")
484504
if should_keep_replicas or should_keep_slaves:
485505
self.__index.set_settings(self.settings)
506+
if should_keep_rules:
507+
response = self.__index.batch_rules(rules, forward_to_replicas=True)
508+
self.__index.wait_task(response['taskID'])
509+
logger.info("Saved rules for index %s with response: {}".format(response), self.index_name)
510+
if should_keep_synonyms:
511+
response = self.__index.batch_synonyms(synonyms, forward_to_replicas=True)
512+
self.__index.wait_task(response['taskID'])
513+
logger.info("Saved synonyms for index %s with response: {}".format(response), self.index_name)
486514
return counts
487515
except AlgoliaException as e:
488516
if DEBUG:

0 commit comments

Comments
 (0)