0

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?

Evaldas Buinauskas
  • 13,739
  • 11
  • 55
  • 107
pedram
  • 335
  • 1
  • 4
  • 19

2 Answers2

0

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.

Evaldas Buinauskas
  • 13,739
  • 11
  • 55
  • 107
0

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

lk_vc
  • 1,136
  • 20
  • 26