Home > Mobile >  How can I make sure that an existing value is not overwritten when doing an ElasticSearch update in
How can I make sure that an existing value is not overwritten when doing an ElasticSearch update in

Time:12-19

I have an ElasticSearch entry with the ID myId in the index myIndex. This entry has the field someExistingField.

Now I want to update this entry. For technical reasons, the update request does not contain someExistingField data.

I want to make sure that in scope of this update

  • only those values are changed that are specified in the update request and
  • values not mentioned in the update request are not changed.

Currently I am using this code:

final Map<String, Object> esDocument = ...;

final UpdateRequest request = new UpdateRequest(myIndex, myId);
request.doc(esDocument, XContentType.JSON);

esClient.update(request, RequestOptions.DEFAULT);

How do I need to change this code in order for it to not override existing values which are not specified in request?

CodePudding user response:

This seems to work:

final UpdateRequest request = new UpdateRequest(myIndex, myId);
...
request.fetchSource(new FetchSourceContext(true, 
  new String[]{"someExistingField"}, Strings.EMPTY_ARRAY));
...
  • Related