Skip to content

Commit 9aeff0c

Browse files
authored
Added scriptedUpsert and detectNoop options to UpdateOperation (#856)
Signed-off-by: Christian Winkler <[email protected]>
1 parent 821dae6 commit 9aeff0c

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ This section is for maintaining a changelog for all breaking changes for the cli
3131

3232
### Fixed
3333
- Fix version and build ([#254](https://github.com/opensearch-project/opensearch-java/pull/254))
34+
- Fix missing properties on UpdateOperation ([#744](https://github.com/opensearch-project/opensearch-java/pull/744))
3435

3536
### Security
3637

java-client/src/main/java/org/opensearch/client/opensearch/core/bulk/UpdateOperation.java

+24
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ public static class Builder<TDocument> extends BulkOperationBase.AbstractBuilder
144144
@Nullable
145145
private Boolean docAsUpsert;
146146

147+
@Nullable
148+
private Boolean scriptedUpsert;
149+
150+
@Nullable
151+
private Boolean detectNoop;
152+
147153
@Nullable
148154
private TDocument upsert;
149155

@@ -166,6 +172,22 @@ public final Builder<TDocument> docAsUpsert(@Nullable Boolean value) {
166172
return this;
167173
}
168174

175+
/**
176+
* API name: {@code scripted_upsert}
177+
*/
178+
public final Builder<TDocument> scriptedUpsert(@Nullable Boolean value) {
179+
this.scriptedUpsert = value;
180+
return this;
181+
}
182+
183+
/**
184+
* API name: {@code detect_noop}
185+
*/
186+
public final Builder<TDocument> detectNoop(@Nullable Boolean value) {
187+
this.detectNoop = value;
188+
return this;
189+
}
190+
169191
/**
170192
* API name: {@code upsert}
171193
*/
@@ -223,6 +245,8 @@ public UpdateOperation<TDocument> build() {
223245

224246
data = new UpdateOperationData.Builder<TDocument>().document(document)
225247
.docAsUpsert(docAsUpsert)
248+
.scriptedUpsert(scriptedUpsert)
249+
.detectNoop(detectNoop)
226250
.script(script)
227251
.upsert(upsert)
228252
.tDocumentSerializer(tDocumentSerializer)

java-client/src/main/java/org/opensearch/client/opensearch/core/bulk/UpdateOperationData.java

+40
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public class UpdateOperationData<TDocument> implements JsonpSerializable {
2424
@Nullable
2525
private final Boolean docAsUpsert;
2626

27+
@Nullable
28+
private final Boolean scriptedUpsert;
29+
30+
@Nullable
31+
private final Boolean detectNoop;
32+
2733
@Nullable
2834
private final TDocument upsert;
2935

@@ -36,6 +42,8 @@ public class UpdateOperationData<TDocument> implements JsonpSerializable {
3642
private UpdateOperationData(Builder<TDocument> builder) {
3743
this.document = builder.document;
3844
this.docAsUpsert = builder.docAsUpsert;
45+
this.scriptedUpsert = builder.scriptedUpsert;
46+
this.detectNoop = builder.detectNoop;
3947
this.script = builder.script;
4048
this.upsert = builder.upsert;
4149
this.tDocumentSerializer = builder.tDocumentSerializer;
@@ -55,6 +63,16 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
5563
generator.write(this.docAsUpsert);
5664
}
5765

66+
if (this.scriptedUpsert != null) {
67+
generator.writeKey("scripted_upsert");
68+
generator.write(scriptedUpsert);
69+
}
70+
71+
if (this.detectNoop != null) {
72+
generator.writeKey("detect_noop");
73+
generator.write(detectNoop);
74+
}
75+
5876
if (this.document != null) {
5977
generator.writeKey("doc");
6078
JsonpUtils.serialize(document, generator, tDocumentSerializer, mapper);
@@ -87,6 +105,12 @@ public static class Builder<TDocument> extends BulkOperationBase.AbstractBuilder
87105
@Nullable
88106
private Boolean docAsUpsert;
89107

108+
@Nullable
109+
private Boolean scriptedUpsert;
110+
111+
@Nullable
112+
private Boolean detectNoop;
113+
90114
@Nullable
91115
private TDocument upsert;
92116

@@ -109,6 +133,22 @@ public final Builder<TDocument> docAsUpsert(@Nullable Boolean value) {
109133
return this;
110134
}
111135

136+
/**
137+
* API name: {@code scripted_upsert}
138+
*/
139+
public final Builder<TDocument> scriptedUpsert(@Nullable Boolean value) {
140+
this.scriptedUpsert = value;
141+
return this;
142+
}
143+
144+
/**
145+
* API name: {@code detect_noop}
146+
*/
147+
public final Builder<TDocument> detectNoop(@Nullable Boolean value) {
148+
this.detectNoop = value;
149+
return this;
150+
}
151+
112152
/**
113153
* API name: {@code upsert}
114154
*/

java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java

+94
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,100 @@ public void testBulkUpdateScriptUpsert() throws IOException {
393393
assertEquals(1337, getResponse.source().getIntValue());
394394
}
395395

396+
public void testBulkUpdateScriptedUpsertUpdate() throws IOException {
397+
final String id = "777";
398+
399+
final AppData appData = new AppData();
400+
appData.setIntValue(1337);
401+
appData.setMsg("foo");
402+
403+
assertEquals(Result.Created, javaClient().index(b -> b.index("index").id(id).document(appData)).result());
404+
405+
final BulkOperation op = new BulkOperation.Builder().update(
406+
o -> o.index("index")
407+
.id(id)
408+
.scriptedUpsert(true)
409+
.upsert(Collections.emptyMap())
410+
.script(
411+
Script.of(
412+
s -> s.inline(
413+
new InlineScript.Builder().lang("painless")
414+
.source("ctx._source.intValue = ctx?._source?.intValue == null ? 7777 : 9999")
415+
.build()
416+
)
417+
)
418+
)
419+
).build();
420+
421+
BulkRequest bulkRequest = new BulkRequest.Builder().operations(op).build();
422+
BulkResponse bulkResponse = javaClient().bulk(bulkRequest);
423+
424+
assertTrue(bulkResponse.took() > 0);
425+
assertEquals(1, bulkResponse.items().size());
426+
427+
final GetResponse<AppData> getResponse = javaClient().get(b -> b.index("index").id(id), AppData.class);
428+
assertTrue(getResponse.found());
429+
assertEquals(9999, getResponse.source().getIntValue());
430+
}
431+
432+
public void testBulkUpdateScriptedUpsertInsert() throws IOException {
433+
final String id = "778";
434+
435+
final BulkOperation op = new BulkOperation.Builder().update(
436+
o -> o.index("index")
437+
.id(id)
438+
.scriptedUpsert(true)
439+
.upsert(Collections.emptyMap())
440+
.script(
441+
Script.of(
442+
s -> s.inline(
443+
new InlineScript.Builder().lang("painless")
444+
.source("ctx._source.intValue = ctx?._source?.intValue == null ? 7777 : 9999")
445+
.build()
446+
)
447+
)
448+
)
449+
).build();
450+
451+
BulkRequest bulkRequest = new BulkRequest.Builder().operations(op).build();
452+
BulkResponse bulkResponse = javaClient().bulk(bulkRequest);
453+
454+
assertTrue(bulkResponse.took() > 0);
455+
assertEquals(1, bulkResponse.items().size());
456+
457+
final GetResponse<AppData> getResponse = javaClient().get(b -> b.index("index").id(id), AppData.class);
458+
assertTrue(getResponse.found());
459+
assertEquals(7777, getResponse.source().getIntValue());
460+
}
461+
462+
public void testBulkUpdateDetectNoop() throws IOException {
463+
final String id = "779";
464+
465+
final AppData appData = new AppData();
466+
appData.setIntValue(1337);
467+
appData.setMsg("foo");
468+
469+
assertEquals(Result.Created, javaClient().index(b -> b.index("index").id(id).document(appData)).result());
470+
471+
BulkOperation op = new BulkOperation.Builder().update(o -> o.index("index").id(id).detectNoop(true).document(appData)).build();
472+
473+
BulkRequest bulkRequest = new BulkRequest.Builder().operations(op).build();
474+
BulkResponse bulkResponse = javaClient().bulk(bulkRequest);
475+
476+
assertTrue(bulkResponse.took() > 0);
477+
assertEquals(1, bulkResponse.items().size());
478+
assertEquals(Result.NoOp.jsonValue(), bulkResponse.items().get(0).result());
479+
480+
op = new BulkOperation.Builder().update(o -> o.index("index").id(id).detectNoop(false).document(appData)).build();
481+
482+
bulkRequest = new BulkRequest.Builder().operations(op).build();
483+
bulkResponse = javaClient().bulk(bulkRequest);
484+
assertTrue(bulkResponse.took() > 0);
485+
assertEquals(1, bulkResponse.items().size());
486+
assertEquals(Result.Updated.jsonValue(), bulkResponse.items().get(0).result());
487+
488+
}
489+
396490
public void testBulkUpdateUpsert() throws IOException {
397491
final String id = "100";
398492

0 commit comments

Comments
 (0)