Skip to content

Commit c442e3e

Browse files
committed
conditional indexing, close #3
1 parent 928fd70 commit c442e3e

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ CHANGELOG
44
* Remove algolia_buildindex command. Use algolia_reindex instead.
55
* Change settings format. Last format is still supported.
66
* Add unit test.
7+
* Add tag capacity
8+
* Add Conditional indexing

src/models.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class AlgoliaIndex(object):
2525
fields = ()
2626

2727
# Use to specify the geo-fields that should be used for location search.
28-
# The field should be a callable that returns a tuple.
28+
# The attribute should be a callable that returns a tuple.
2929
geo_field = None
3030

3131
# Use to specify the field that should be used for filtering by tag.
@@ -37,6 +37,10 @@ class AlgoliaIndex(object):
3737
# Use to specify the settings of the index.
3838
settings = {}
3939

40+
# Use to specify a callable that say if the instance should be indexed.
41+
# The attribute should be a callable that returns a booleen.
42+
should_index = None
43+
4044
# Instance of the index from algoliasearch client
4145
__index = None
4246

@@ -87,6 +91,16 @@ def __init__(self, model, client):
8791
raise AlgoliaIndexError('{} is not an attribute of {}.'.format(
8892
self.geo_field, model))
8993

94+
if self.should_index:
95+
if hasattr(model, self.should_index):
96+
attr = getattr(model, self.should_index)
97+
if not callable(attr):
98+
raise AlgoliaIndexError('{} should be a callable.'.format(
99+
self.should_index))
100+
else:
101+
raise AlgoliaIndexError('{} is not an attribute of {}.'.format(
102+
self.should_index, model))
103+
90104
def __set_index(self, client):
91105
'''Get an instance of Algolia Index'''
92106
params = getattr(settings, 'ALGOLIA', None)
@@ -154,6 +168,15 @@ def _build_object(self, instance):
154168

155169
def update_obj_index(self, instance):
156170
'''Update the object.'''
171+
if self.should_index:
172+
attr = getattr(instance, self.should_index)
173+
if not attr():
174+
# Should not index, but since we don't now the state of the
175+
# instance, we need to send a DELETE request to ensure that if
176+
# the instance was previously indexed, it will be removed.
177+
self.delete_obj_index(self, instance)
178+
return
179+
157180
obj = self._build_object(instance)
158181
self.__index.save_object(obj)
159182
logger.debug('UPDATE %s FROM %s', obj['objectID'], self.model)
@@ -187,6 +210,11 @@ def reindex_all(self, batch_size=1000):
187210
counts = 0
188211
batch = []
189212
for instance in self.model.objects.all():
213+
if self.should_index:
214+
attr = getattr(instance, self.should_index)
215+
if not attr():
216+
continue # should not index
217+
190218
batch.append(self._build_object(instance))
191219
if len(batch) >= batch_size:
192220
result = self.__tmp_index.save_objects(batch)

tests/test_engine.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from django.test import TestCase
2-
from django.db import models
32

43
from django.contrib.algoliasearch import AlgoliaIndex
54
from django.contrib.algoliasearch.registration import AlgoliaEngine
@@ -12,6 +11,12 @@ class EngineTestCase(TestCase):
1211
def setUp(self):
1312
self.engine = AlgoliaEngine()
1413

14+
def tearDown(self):
15+
try:
16+
self.engine.unregister(Example)
17+
except RegistrationError:
18+
pass
19+
1520
def test_is_register(self):
1621
self.assertFalse(self.engine.is_registered(Example))
1722
self.engine.register(Example)

0 commit comments

Comments
 (0)