Elasticsearch bulk upsert with query on specific field

8.4k views Asked by At

There is an index with this mapping:

"data": {
   "type": "object"
},
"modified_date": {
   "type": "date"
},
"uid": {
   "type": "keyword"
}

Now I want to do a bulk operation for upsert (Update if exists and insert if not exists). The query is on uid field. I wrote this query but its not working.

URL : http://host/index/_bulk
METHOD : POST
DATA :
{"update":{"uid":"123"}}
{"doc":{"modified_date":"...", "data":{"array":[1,2,3]}}, "doc_as_upsert":true}
{"update":{"uid":"456"}}
{"doc":{"modified_date":"...", "data":{"array":[4,5,6]}}, "doc_as_upsert":true}
{"update":{"uid":"789"}}
{"doc":{"modified_date":"...", "data":{"array":[7,8,9]}}, "doc_as_upsert":true}

How can I do bulk upsert with query on uid field?

2

There are 2 answers

1
Evaldas Buinauskas On

If you change operation from update to insert, you'd be upserting.

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html

The index and create actions expect a source on the next line, and have the same semantics as the op_type parameter in the standard index API: create fails if a document with the same ID already exists in the target, index adds or replaces a document as necessary.

0
lk_vc On

You can consider querying the _id field directly instead of uid field.