Skip to content

Commit

Permalink
fix(reindex_all): Keep settings over reindex, fixes #238
Browse files Browse the repository at this point in the history
  • Loading branch information
PLNech committed Jan 12, 2018
1 parent 3f3a785 commit 831eeec
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
21 changes: 21 additions & 0 deletions algoliasearch_django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,18 @@ def raw_search(self, query='', params=None):
else:
logger.warning('ERROR DURING SEARCH: %s', e)

def get_settings(self):
"""Returns the settings of the index."""
try:
logger.info('GET SETTINGS ON %s', self.index_name)
return self.__index.get_settings()
except AlgoliaException as e:
if DEBUG:
raise e
else:
logger.warning('ERROR DURING GET_SETTINGS ON %s: %s',
self.model, e)

def set_settings(self):
"""Applies the settings to the index."""
if not self.settings:
Expand Down Expand Up @@ -404,6 +416,15 @@ def reindex_all(self, batch_size=1000):
a method `get_queryset` in your subclass. This can be used to optimize
the performance (for example with select_related or prefetch_related).
"""
try:
if not self.settings:
self.settings = self.get_settings()
logger.debug('Got settings for index %s: %s', self.index_name, self.settings)
except AlgoliaException as e:
if "Index does not exist" in e.message:
pass # Expected, let's clear and recreate from scratch
else:
raise e # Unexpected error while getting settings
try:
if self.settings:
replicas = self.settings.get('replicas', None)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_index.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding=utf-8
import time
from django.conf import settings
from django.test import TestCase

Expand Down Expand Up @@ -97,6 +98,7 @@ def test_reindex_with_should_index_boolean(self):
is_online=True
)
index = AlgoliaIndex(Website, self.client, settings.ALGOLIA)

class WebsiteIndex(AlgoliaIndex):
settings = {
'replicas': [
Expand All @@ -109,6 +111,28 @@ class WebsiteIndex(AlgoliaIndex):
index = WebsiteIndex(Website, self.client, settings.ALGOLIA)
index.reindex_all()

def test_reindex_no_settings(self):
self.maxDiff = None

class WebsiteIndex(AlgoliaIndex):
foo = {}

# Given an existing index with settings
index = AlgoliaIndex(Website, self.client, settings.ALGOLIA)
index.settings = {'hitsPerPage': 42}
index.reindex_all()
time.sleep(10) # FIXME: Refactor reindex_all to use waitTask
index_settings = index.get_settings()

# When reindexing with no current settings
index.settings = None
index = WebsiteIndex(Website, self.client, settings.ALGOLIA)
index.reindex_all()

# Expect the settings to be kept across reindex
self.assertEqual(index.get_settings(), index_settings,
"An index whose model has no settings should keep its settings after reindex")

def test_custom_objectID(self):
class UserIndex(AlgoliaIndex):
custom_objectID = 'username'
Expand Down

0 comments on commit 831eeec

Please sign in to comment.