Skip to content

Commit ce5f697

Browse files
authored
[Fix #111] Adding API for filtering object while indexing (#301)
* [Fix #111] Adding API for filtering object while indexing * Update documents.py * updating docstring
1 parent 540ed35 commit ce5f697

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

django_elasticsearch_dsl/documents.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ def parallel_bulk(self, actions, **kwargs):
159159
@classmethod
160160
def generate_id(cls, object_instance):
161161
"""
162-
The default behavior is to use the Django object's pk (id) as the
163-
elasticseach index id (_id). If needed, this method can be overloaded
162+
The default behavior is to use the Django object's pk (id) as the
163+
elasticseach index id (_id). If needed, this method can be overloaded
164164
to change this default behavior.
165165
"""
166166
return object_instance.pk
@@ -177,7 +177,8 @@ def _prepare_action(self, object_instance, action):
177177

178178
def _get_actions(self, object_list, action):
179179
for object_instance in object_list:
180-
yield self._prepare_action(object_instance, action)
180+
if self.should_index_object(object_instance):
181+
yield self._prepare_action(object_instance, action)
181182

182183
def _bulk(self, *args, **kwargs):
183184
"""Helper for switching between normal and parallel bulk operation"""
@@ -187,6 +188,13 @@ def _bulk(self, *args, **kwargs):
187188
else:
188189
return self.bulk(*args, **kwargs)
189190

191+
def should_index_object(self, obj):
192+
"""
193+
Overwriting this method and returning a boolean value
194+
should determine whether the object should be indexed.
195+
"""
196+
return True
197+
190198
def update(self, thing, refresh=None, action='index', parallel=False, **kwargs):
191199
"""
192200
Update each document in ES for a model, iterable of models or queryset

0 commit comments

Comments
 (0)