Skip to content

[Fix #111] Adding API for filtering object while indexing #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions django_elasticsearch_dsl/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"""
Expand All @@ -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
Expand Down