import com.yahoo.document.datatypes.StringFieldValue;// Increment weight for a specific keyField tagsField = musicType.getField("tags");StringFieldValue tag = new StringFieldValue("rock");FieldUpdate update = FieldUpdate.createIncrement(tagsField, tag, 10);docUpdate.addFieldUpdate(update);
import com.yahoo.document.datatypes.IntegerFieldValue;import com.yahoo.document.update.ValueUpdate;// Update element at index 2FieldUpdate mapUpdate = FieldUpdate.createMap( categoriesField, new IntegerFieldValue(2), // Array index ValueUpdate.createAssign(new StringFieldValue("jazz")));docUpdate.addFieldUpdate(mapUpdate);
// Increment weight for "rock" by 10FieldUpdate incWeight = FieldUpdate.createIncrement( tagsField, new StringFieldValue("rock"), 10);docUpdate.addFieldUpdate(incWeight);
Apply updates only when a condition is met using test-and-set:
import com.yahoo.document.DocumentUpdate;import com.yahoo.document.select.parser.ParseException;DocumentUpdate update = new DocumentUpdate(musicType, "id:mynamespace:music::song1");// Set conditionupdate.setCondition(new TestAndSetCondition("music.year > 1970"));// Add field updatesFieldUpdate yearUpdate = FieldUpdate.createAssign( musicType.getField("year"), 1975);update.addFieldUpdate(yearUpdate);// Update will only apply if condition matchessession.update(update);
import com.yahoo.document.update.FieldUpdate;import com.yahoo.document.update.ValueUpdate;FieldUpdate fieldUpdate = FieldUpdate.create(musicType.getField("tags"));// Add multiple value updatesfieldUpdate.addValueUpdate(ValueUpdate.createAdd( new StringFieldValue("rock"), 100));fieldUpdate.addValueUpdate(ValueUpdate.createAdd( new StringFieldValue("classic"), 80));fieldUpdate.addValueUpdate(ValueUpdate.createRemove( new StringFieldValue("pop")));docUpdate.addFieldUpdate(fieldUpdate);
import com.yahoo.document.DocumentId;DocumentId id = new DocumentId("id:mynamespace:music::song1");Result result = session.remove(id);if (result.isSuccess()) { System.out.println("Document removed");}
# Remove all music documents from the 1960scurl -X DELETE \ 'http://localhost:8080/document/v1/mynamespace/music/docid?selection=music.year<1970&cluster=music'
import com.yahoo.documentapi.DocumentUpdateResponse;responseHandler.handleResponse(response -> { if (response instanceof DocumentUpdateResponse) { DocumentUpdateResponse updateResp = (DocumentUpdateResponse) response; if (!updateResp.isSuccess()) { if (updateResp.wasFound()) { // Document exists but condition failed System.err.println("Condition not met"); } else { // Document not found System.err.println("Document does not exist"); } } }});
Combine multiple field updates into a single DocumentUpdate:
6
// Good: Single update with multiple fieldsupdate.addFieldUpdate(yearUpdate);update.addFieldUpdate(titleUpdate);update.addFieldUpdate(artistUpdate);session.update(update);// Avoid: Multiple separate updates// session.update(yearUpdate);// session.update(titleUpdate);// session.update(artistUpdate);
7
Use Arithmetic Operations
8
Leverage built-in arithmetic instead of read-modify-write:
9
// Good: Atomic incrementFieldUpdate.createIncrement(field, 1);// Avoid: Read-modify-write// int current = (int) doc.getFieldValue("count");// doc.setFieldValue("count", current + 1);
10
Set Appropriate Conditions
11
Use test-and-set to prevent race conditions:
12
// Ensure year is not already setupdate.setCondition(new TestAndSetCondition("music.year == null"));update.addFieldUpdate(FieldUpdate.createAssign( musicType.getField("year"), 1975));