Skip to content

Commit ca73b3b

Browse files
authored
fix(reindex_all): Keep settings over reindex (#239)
* fix(reindex_all): Keep settings over reindex, fixes #238 * chore: Formatting * refactor(reindex_all): Use Exception.args
1 parent 7ce0caf commit ca73b3b

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

algoliasearch_django/models.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def __init__(self, model, client, settings):
8282

8383
try:
8484
all_model_fields = [f.name for f in model._meta.get_fields() if not f.is_relation]
85-
except AttributeError: # get_fields requires Django >= 1.8
85+
except AttributeError: # get_fields requires Django >= 1.8
8686
all_model_fields = [f.name for f in model._meta.local_fields]
8787

8888
if isinstance(self.fields, str):
@@ -358,7 +358,19 @@ def raw_search(self, query='', params=None):
358358
if DEBUG:
359359
raise e
360360
else:
361-
logger.warning('ERROR DURING SEARCH: %s', e)
361+
logger.warning('ERROR DURING SEARCH ON %s: %s', self.index_name, e)
362+
363+
def get_settings(self):
364+
"""Returns the settings of the index."""
365+
try:
366+
logger.info('GET SETTINGS ON %s', self.index_name)
367+
return self.__index.get_settings()
368+
except AlgoliaException as e:
369+
if DEBUG:
370+
raise e
371+
else:
372+
logger.warning('ERROR DURING GET_SETTINGS ON %s: %s',
373+
self.model, e)
362374

363375
def set_settings(self):
364376
"""Applies the settings to the index."""
@@ -404,6 +416,15 @@ def reindex_all(self, batch_size=1000):
404416
a method `get_queryset` in your subclass. This can be used to optimize
405417
the performance (for example with select_related or prefetch_related).
406418
"""
419+
try:
420+
if not self.settings:
421+
self.settings = self.get_settings()
422+
logger.debug('Got settings for index %s: %s', self.index_name, self.settings)
423+
except AlgoliaException as e:
424+
if any("Index does not exist" in arg for arg in e.args):
425+
pass # Expected, let's clear and recreate from scratch
426+
else:
427+
raise e # Unexpected error while getting settings
407428
try:
408429
if self.settings:
409430
replicas = self.settings.get('replicas', None)

tests/test_index.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# coding=utf-8
2+
import time
23
from django.conf import settings
34
from django.test import TestCase
45

@@ -79,6 +80,7 @@ def test_index_name_settings(self):
7980

8081
def test_reindex_with_replicas(self):
8182
index = AlgoliaIndex(Website, self.client, settings.ALGOLIA)
83+
8284
class WebsiteIndex(AlgoliaIndex):
8385
settings = {
8486
'replicas': [
@@ -97,6 +99,7 @@ def test_reindex_with_should_index_boolean(self):
9799
is_online=True
98100
)
99101
index = AlgoliaIndex(Website, self.client, settings.ALGOLIA)
102+
100103
class WebsiteIndex(AlgoliaIndex):
101104
settings = {
102105
'replicas': [
@@ -109,6 +112,28 @@ class WebsiteIndex(AlgoliaIndex):
109112
index = WebsiteIndex(Website, self.client, settings.ALGOLIA)
110113
index.reindex_all()
111114

115+
def test_reindex_no_settings(self):
116+
self.maxDiff = None
117+
118+
class WebsiteIndex(AlgoliaIndex):
119+
foo = {}
120+
121+
# Given an existing index with settings
122+
index = AlgoliaIndex(Website, self.client, settings.ALGOLIA)
123+
index.settings = {'hitsPerPage': 42}
124+
index.reindex_all()
125+
time.sleep(10) # FIXME: Refactor reindex_all to use waitTask
126+
index_settings = index.get_settings()
127+
128+
# When reindexing with no current settings
129+
index.settings = None
130+
index = WebsiteIndex(Website, self.client, settings.ALGOLIA)
131+
index.reindex_all()
132+
133+
# Expect the settings to be kept across reindex
134+
self.assertEqual(index.get_settings(), index_settings,
135+
"An index whose model has no settings should keep its settings after reindex")
136+
112137
def test_custom_objectID(self):
113138
class UserIndex(AlgoliaIndex):
114139
custom_objectID = 'username'
@@ -432,6 +457,7 @@ def test_save_record_should_index_boolean(self):
432457
is_online=True
433458
)
434459
index = AlgoliaIndex(Website, self.client, settings.ALGOLIA)
460+
435461
class WebsiteIndex(AlgoliaIndex):
436462
settings = {
437463
'replicas': [

0 commit comments

Comments
 (0)