Last active
July 10, 2019 12:12
-
-
Save harshvb7/4f45a4aa16f5b3e83351a0851d40c52b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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