Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bulk Partial Upsert in Elasticseach with python

I want to send n upsert partial requests to ES, is such a thing possible? So if the document doesn't exist, insert my partial doc. If it already exists, update it with the partial doc.

Using the bulk helpers, I've tried a ton of variations, but they all wipe out existing values in favour of the new ones.

data = [{
    "_index": 'my_index',
    "_type": 'my_type',
    "_id": 12345,
    "doc": {"newkey": 'newvalue'}
}]
helpers.bulk(es, data, index='my_index', doc_type='my_type')

or

data = [{
    "_index": 'my_index',
    "_type": 'my_type',
    "_id": 12345,
    "_source": {"newkey": 'newvalue'}
}]
helpers.bulk(es, data, index='my_index', doc_type='my_type')

Does not work either.

like image 531
L-R Avatar asked Dec 23 '15 19:12

L-R


1 Answers

As J. Ku answered, he has given the right answer but the code provided is incomplete. So posting the complete answer.

data = [{
    "_op_type": 'update',
    "_index": 'my_index',
    "_type": 'my_type',
    "_id": 12345,
    "doc": {"newkey": 'newvalue'},
    "doc_as_upsert":True
}]

helpers.bulk(es, data, index='my_index', doc_type='my_type')
like image 125
jhandei Avatar answered Sep 20 '22 22:09

jhandei