简体   繁体   中英

Elasticsearch java bulk upsert exception

I am trying to update an existing index in bulk, using the bulk API, so some records are existing which need to be updated and some are new, which need to be indexed since they are not there. I used the following piece of code

BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
bulkRequestBuilder.add(client.prepareUpdate(InvokeMain.indexName, type, docId).setUpsert(finalMap));

However, with this, I am getting the following exception

org.elasticsearch.action.ActionRequestValidationException: Validation      
Failed: script or doc is missing;

I am guessing new records/new docIds is where it fails. Any idea how to achieve upsert (update if exists, insert if not) in bulk?

BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
bulkRequestBuilder.add(client.prepareUpdate(InvokeMain.indexName, type, docId).setDoc(finalMap).setUpsert(finalMap));

When using upsert (ie setUpsert ), your finalMap needs to be slightly different as described in the official docs , namely it must contain a doc (your case) or a script (hence the validation error stating that script or doc is missing):

...
Map<String, Object> docMap = ... your current map containing the fields...;
Map<String, Object> finalMap = new HashMap<String, Object>();
finalMap.put("doc", docMap);
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM