diff --git a/django_elasticsearch_dsl/documents.py b/django_elasticsearch_dsl/documents.py index 4defa8b7..d2027c24 100644 --- a/django_elasticsearch_dsl/documents.py +++ b/django_elasticsearch_dsl/documents.py @@ -159,8 +159,8 @@ def parallel_bulk(self, actions, **kwargs): @classmethod def generate_id(cls, object_instance): """ - The default behavior is to use the Django object's pk (id) as the - elasticseach index id (_id). If needed, this method can be overloaded + The default behavior is to use the Django object's pk (id) as the + elasticseach index id (_id). If needed, this method can be overloaded to change this default behavior. """ return object_instance.pk @@ -177,7 +177,8 @@ def _prepare_action(self, object_instance, action): def _get_actions(self, object_list, action): for object_instance in object_list: - yield self._prepare_action(object_instance, action) + if self.should_index_object(object_instance): + yield self._prepare_action(object_instance, action) def _bulk(self, *args, **kwargs): """Helper for switching between normal and parallel bulk operation""" @@ -187,6 +188,13 @@ def _bulk(self, *args, **kwargs): else: return self.bulk(*args, **kwargs) + def should_index_object(self, obj): + """ + Overwriting this method and returning a boolean value + should determine whether the object should be indexed. + """ + return True + def update(self, thing, refresh=None, action='index', parallel=False, **kwargs): """ Update each document in ES for a model, iterable of models or queryset