django-elasticsearch-dsl icon indicating copy to clipboard operation
django-elasticsearch-dsl copied to clipboard

Handling Bulk Inserts

Open lamjohnson opened this issue 7 years ago • 3 comments

It'd be cool if it could keep the database and elasticsearch index in sync for bulk inserts. I noticed it wasn't a feature yet.

lamjohnson avatar Jun 29 '17 01:06 lamjohnson

@jlam17:

The only way it could work:

  • Create a custom Django Model manager, override the bulk_create method and handle updates there.
  • Use that custom Django Model manager with every model you would want to index.

And same applies to bulk update and delete actions.

@sabricot:

If it sounds convenient to you, I could implement that.

barseghyanartur avatar Jun 29 '17 09:06 barseghyanartur

@barseghyanartur Thanks for the reply, I didn't know that. I just started learning Django so I appreciate the help.

lamjohnson avatar Jun 29 '17 20:06 lamjohnson

Not sure if I'm being late for this, but what I do is perform the bulk_insert and then grab all the ids of the recently created objects, create a queryset and call the update method of the document.

For instance, let's say you have Blog model and BlogDocument:

blogs_to_create = [Blog(**data) for data in bulk_data]
blogs_created = Blog.objects.bulk_create(blogs_to_create)

blogs_id = [blog.id for blog in blogs_created]
new_blogs_qs = Blog.object.filter(id__in=blogs_id)
BlogDocument().update(new_blogs_qs)

rapkyt avatar Nov 30 '20 21:11 rapkyt