Reputation: 143
I'm trying to insert a attribute into a doc called 'users' using bulk on python.
Like this:
actions = [ { '_op_type': 'update', "_index": index, "_type": "users", "_id": s['id_user'], "doc": { "purchases": [] }, } for i, s in users.iterrows() ] success, failure = self.db.bulk(actions, stats_only=True)
How can I add purchases just if it does not exist? I do not want a upsert in this case. It's forbidden to change the value if it has already been indexed.
Upvotes: 0
Views: 59
Reputation: 217504
You can try to do it using a scripted upsert, like this:
actions = [
{
'_op_type': 'update',
"_index": index,
"_type": "users",
"_id": s['id_user'],
"script": {
"source": "ctx._source.purchases = ctx._source.purchases ?: params.purchases",
"params": {
"purchases": []
}
},
"scripted_upsert": true,
"upsert": {}
}
for i, s in users.iterrows()
]
success, failure = self.db.bulk(actions, stats_only=True)
The purchases
array will only get assigned with the array in params
if not already defined in the source.
Upvotes: 1