Skip to content

Instantly share code, notes, and snippets.

@harshvb7
Last active July 10, 2019 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harshvb7/4f45a4aa16f5b3e83351a0851d40c52b to your computer and use it in GitHub Desktop.
Save harshvb7/4f45a4aa16f5b3e83351a0851d40c52b to your computer and use it in GitHub Desktop.
from elasticsearch_dsl import connections, UpdateByQuery
def bulk_update_docs(ids, instance, fields_changed=[]):
"""
Arguments:
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.
Input:
{
'user': {
'city': {
'name': 'New Cologne',
'slug': 'new-cologne',
}
}
}
Result:
=> ['ctx._source.user.city.name=params.user.city.name', 'ctx._source.user.city.slug=params.user.city.slug']
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment