Skip to content

Commit

Permalink
Merge pull request #223 from m-vdb/mvdb/fix-reindex-all-should-index-…
Browse files Browse the repository at this point in the history
…not-callable

Fix reindex_all() and save_record() when should_index is not callable
  • Loading branch information
PLNech authored Oct 31, 2017
2 parents 0f70028 + 1eac710 commit da47117
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
18 changes: 8 additions & 10 deletions algoliasearch_django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,12 @@ def save_record(self, instance, update_fields=None, **kwargs):
For more information about partial_update_object:
https://github.com/algolia/algoliasearch-client-python#update-an-existing-object-in-the-index
"""
if self.should_index:
if not self.should_index(instance):
# Should not index, but since we don't now the state of the
# instance, we need to send a DELETE request to ensure that if
# the instance was previously indexed, it will be removed.
self.delete_record(instance)
return
if not self._should_index(instance):
# Should not index, but since we don't now the state of the
# instance, we need to send a DELETE request to ensure that if
# the instance was previously indexed, it will be removed.
self.delete_record(instance)
return

try:
if update_fields:
Expand Down Expand Up @@ -424,9 +423,8 @@ def reindex_all(self, batch_size=1000):
qs = self.model.objects.all()

for instance in qs:
if self.should_index:
if not self.should_index(instance):
continue # should not index
if not self._should_index(instance):
continue # should not index

batch.append(self.get_raw_record(instance))
if len(batch) >= batch_size:
Expand Down
1 change: 1 addition & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def permissions(self):
class Website(models.Model):
name = models.CharField(max_length=100)
url = models.URLField()
is_online = models.BooleanField(default=False)


class Example(models.Model):
Expand Down
38 changes: 38 additions & 0 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ class WebsiteIndex(AlgoliaIndex):
index = WebsiteIndex(Website, self.client, settings.ALGOLIA)
index.reindex_all()

def test_reindex_with_should_index_boolean(self):
Website.objects.create(
name='Algolia',
url='https://algolia.com',
is_online=True
)
index = AlgoliaIndex(Website, self.client, settings.ALGOLIA)
class WebsiteIndex(AlgoliaIndex):
settings = {
'replicas': [
index.index_name + '_name_asc',
index.index_name + '_name_desc'
]
}
should_index = 'is_online'

index = WebsiteIndex(Website, self.client, settings.ALGOLIA)
index.reindex_all()

def test_custom_objectID(self):
class UserIndex(AlgoliaIndex):
custom_objectID = 'username'
Expand Down Expand Up @@ -382,3 +401,22 @@ class ExampleIndex(AlgoliaIndex):
index = ExampleIndex(Example, self.client, settings.ALGOLIA)
with self.assertRaises(AlgoliaIndexError, msg="We should raise when the should_index property is not boolean"):
index._should_index(self.example)

def test_save_record_should_index_boolean(self):
website = Website.objects.create(
name='Algolia',
url='https://algolia.com',
is_online=True
)
index = AlgoliaIndex(Website, self.client, settings.ALGOLIA)
class WebsiteIndex(AlgoliaIndex):
settings = {
'replicas': [
index.index_name + '_name_asc',
index.index_name + '_name_desc'
]
}
should_index = 'is_online'

index = WebsiteIndex(Website, self.client, settings.ALGOLIA)
index.save_record(website)

0 comments on commit da47117

Please sign in to comment.