Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
from elasticsearch_dsl import connections, UpdateByQuery
def bulk_update_docs(ids, instance, fields_changed=[]):
ids: {list} -- list of ids of documents we want to update
instance {Model instance} -- The updated user model object
fields_changed {list} -- list of the model fields changed
body = generate_sub_dictionary(instance, fields_changed)
client = connections.create_connection()
qry = UpdateByQuery(using=client, index='tweets').params(conflicts='proceed').query('terms', _id=ids)
# update_by_query uses scripts, so we need to generate the formatted source string that matches our body structure
source = generate_source_string(body)
qry = qry.script(source=source, params=body)
def generate_source_string(body):
Recursive algorithm to generate source strings for creating the bulk update call to ES.
'user': {
'city': {
'name': 'New Cologne',
'slug': 'new-cologne',
=> ['', '']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment