Hi team, I am stuck on the below problem. Can somebody please help
I am using django elasticsearch DSL and have below model structure and corresponding document
class Token(AppModel):
# fields
f1, f2, f3
class TokeDocument(Document):
special_x = field
special_y = field
def prepare_special_x():
# some computation not related to model instance
def prepare_special_y():
#some computation not related to model instance
class DJango:
model = Token
fields = [f1, f2, f3]
I have a list of Token models say "model_instances".
I want to update all model_instances to elasticsearch for above document. (I currently use TokenDocument().update(model_instances, parallel=True, refresh=True))
PROBLEM:
Let's say I want to update special_x and special_y based on old values in the document (if it exists). How can we achieve that minimal latency?
Let's say new document be new_doc based on model_instance in model_instances and old_doc be the corresponding old document, I want to update
new_doc.special_x = new_doc.special_x if new_doc.special_x > 0 else old_doc.special_x
I tried fetching document in every prepare_special_* functions and then updating conditionally but that would be very unoptimal I guess (because we query elastic servers multiple times which we should be able to reuse)
I want to add the above functionality to all models in my project (will be creating a wrapper for it) for some globally specific fields