IMPORTANT: No additional bug fixes or documentation updates
will be released for this version. For the latest information, see the
current release documentation.
Upsert
editUpsert
editThere is also support for upsert
. If the document already exists, the content of the upsert
element will be used to index the fresh doc:
IndexRequest indexRequest = new IndexRequest("index", "type", "1") .source(jsonBuilder() .startObject() .field("name", "Joe Smith") .field("gender", "male") .endObject()); UpdateRequest updateRequest = new UpdateRequest("index", "type", "1") .doc(jsonBuilder() .startObject() .field("gender", "male") .endObject()) .upsert(indexRequest); client.update(updateRequest).get();
If the document index/type/1
already exists, we will have after this operation a document like:
If it does not exist, we will have a new document:
{ "name" : "Joe Smith", "gender": "male" }