From 9aeff0c73f69c22d3c8c20d3b4b50b2877695b38 Mon Sep 17 00:00:00 2001 From: Christian Winkler Date: Tue, 20 Feb 2024 20:21:50 +0100 Subject: [PATCH] Added scriptedUpsert and detectNoop options to UpdateOperation (#856) Signed-off-by: Christian Winkler --- CHANGELOG.md | 1 + .../opensearch/core/bulk/UpdateOperation.java | 24 +++++ .../core/bulk/UpdateOperationData.java | 40 ++++++++ .../opensearch/integTest/AbstractCrudIT.java | 94 +++++++++++++++++++ 4 files changed, 159 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 918b6a65a5..a16b41db33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ This section is for maintaining a changelog for all breaking changes for the cli ### Fixed - Fix version and build ([#254](https://github.com/opensearch-project/opensearch-java/pull/254)) +- Fix missing properties on UpdateOperation ([#744](https://github.com/opensearch-project/opensearch-java/pull/744)) ### Security diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/core/bulk/UpdateOperation.java b/java-client/src/main/java/org/opensearch/client/opensearch/core/bulk/UpdateOperation.java index 15d30fe0c4..c1fae6cb27 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/core/bulk/UpdateOperation.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/core/bulk/UpdateOperation.java @@ -144,6 +144,12 @@ public static class Builder extends BulkOperationBase.AbstractBuilder @Nullable private Boolean docAsUpsert; + @Nullable + private Boolean scriptedUpsert; + + @Nullable + private Boolean detectNoop; + @Nullable private TDocument upsert; @@ -166,6 +172,22 @@ public final Builder docAsUpsert(@Nullable Boolean value) { return this; } + /** + * API name: {@code scripted_upsert} + */ + public final Builder scriptedUpsert(@Nullable Boolean value) { + this.scriptedUpsert = value; + return this; + } + + /** + * API name: {@code detect_noop} + */ + public final Builder detectNoop(@Nullable Boolean value) { + this.detectNoop = value; + return this; + } + /** * API name: {@code upsert} */ @@ -223,6 +245,8 @@ public UpdateOperation build() { data = new UpdateOperationData.Builder().document(document) .docAsUpsert(docAsUpsert) + .scriptedUpsert(scriptedUpsert) + .detectNoop(detectNoop) .script(script) .upsert(upsert) .tDocumentSerializer(tDocumentSerializer) diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/core/bulk/UpdateOperationData.java b/java-client/src/main/java/org/opensearch/client/opensearch/core/bulk/UpdateOperationData.java index 572930d0fd..0caf7cd372 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/core/bulk/UpdateOperationData.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/core/bulk/UpdateOperationData.java @@ -24,6 +24,12 @@ public class UpdateOperationData implements JsonpSerializable { @Nullable private final Boolean docAsUpsert; + @Nullable + private final Boolean scriptedUpsert; + + @Nullable + private final Boolean detectNoop; + @Nullable private final TDocument upsert; @@ -36,6 +42,8 @@ public class UpdateOperationData implements JsonpSerializable { private UpdateOperationData(Builder builder) { this.document = builder.document; this.docAsUpsert = builder.docAsUpsert; + this.scriptedUpsert = builder.scriptedUpsert; + this.detectNoop = builder.detectNoop; this.script = builder.script; this.upsert = builder.upsert; this.tDocumentSerializer = builder.tDocumentSerializer; @@ -55,6 +63,16 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { generator.write(this.docAsUpsert); } + if (this.scriptedUpsert != null) { + generator.writeKey("scripted_upsert"); + generator.write(scriptedUpsert); + } + + if (this.detectNoop != null) { + generator.writeKey("detect_noop"); + generator.write(detectNoop); + } + if (this.document != null) { generator.writeKey("doc"); JsonpUtils.serialize(document, generator, tDocumentSerializer, mapper); @@ -87,6 +105,12 @@ public static class Builder extends BulkOperationBase.AbstractBuilder @Nullable private Boolean docAsUpsert; + @Nullable + private Boolean scriptedUpsert; + + @Nullable + private Boolean detectNoop; + @Nullable private TDocument upsert; @@ -109,6 +133,22 @@ public final Builder docAsUpsert(@Nullable Boolean value) { return this; } + /** + * API name: {@code scripted_upsert} + */ + public final Builder scriptedUpsert(@Nullable Boolean value) { + this.scriptedUpsert = value; + return this; + } + + /** + * API name: {@code detect_noop} + */ + public final Builder detectNoop(@Nullable Boolean value) { + this.detectNoop = value; + return this; + } + /** * API name: {@code upsert} */ diff --git a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java index 16a9d5ff57..ed6fedc024 100644 --- a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java +++ b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java @@ -393,6 +393,100 @@ public void testBulkUpdateScriptUpsert() throws IOException { assertEquals(1337, getResponse.source().getIntValue()); } + public void testBulkUpdateScriptedUpsertUpdate() throws IOException { + final String id = "777"; + + final AppData appData = new AppData(); + appData.setIntValue(1337); + appData.setMsg("foo"); + + assertEquals(Result.Created, javaClient().index(b -> b.index("index").id(id).document(appData)).result()); + + final BulkOperation op = new BulkOperation.Builder().update( + o -> o.index("index") + .id(id) + .scriptedUpsert(true) + .upsert(Collections.emptyMap()) + .script( + Script.of( + s -> s.inline( + new InlineScript.Builder().lang("painless") + .source("ctx._source.intValue = ctx?._source?.intValue == null ? 7777 : 9999") + .build() + ) + ) + ) + ).build(); + + BulkRequest bulkRequest = new BulkRequest.Builder().operations(op).build(); + BulkResponse bulkResponse = javaClient().bulk(bulkRequest); + + assertTrue(bulkResponse.took() > 0); + assertEquals(1, bulkResponse.items().size()); + + final GetResponse getResponse = javaClient().get(b -> b.index("index").id(id), AppData.class); + assertTrue(getResponse.found()); + assertEquals(9999, getResponse.source().getIntValue()); + } + + public void testBulkUpdateScriptedUpsertInsert() throws IOException { + final String id = "778"; + + final BulkOperation op = new BulkOperation.Builder().update( + o -> o.index("index") + .id(id) + .scriptedUpsert(true) + .upsert(Collections.emptyMap()) + .script( + Script.of( + s -> s.inline( + new InlineScript.Builder().lang("painless") + .source("ctx._source.intValue = ctx?._source?.intValue == null ? 7777 : 9999") + .build() + ) + ) + ) + ).build(); + + BulkRequest bulkRequest = new BulkRequest.Builder().operations(op).build(); + BulkResponse bulkResponse = javaClient().bulk(bulkRequest); + + assertTrue(bulkResponse.took() > 0); + assertEquals(1, bulkResponse.items().size()); + + final GetResponse getResponse = javaClient().get(b -> b.index("index").id(id), AppData.class); + assertTrue(getResponse.found()); + assertEquals(7777, getResponse.source().getIntValue()); + } + + public void testBulkUpdateDetectNoop() throws IOException { + final String id = "779"; + + final AppData appData = new AppData(); + appData.setIntValue(1337); + appData.setMsg("foo"); + + assertEquals(Result.Created, javaClient().index(b -> b.index("index").id(id).document(appData)).result()); + + BulkOperation op = new BulkOperation.Builder().update(o -> o.index("index").id(id).detectNoop(true).document(appData)).build(); + + BulkRequest bulkRequest = new BulkRequest.Builder().operations(op).build(); + BulkResponse bulkResponse = javaClient().bulk(bulkRequest); + + assertTrue(bulkResponse.took() > 0); + assertEquals(1, bulkResponse.items().size()); + assertEquals(Result.NoOp.jsonValue(), bulkResponse.items().get(0).result()); + + op = new BulkOperation.Builder().update(o -> o.index("index").id(id).detectNoop(false).document(appData)).build(); + + bulkRequest = new BulkRequest.Builder().operations(op).build(); + bulkResponse = javaClient().bulk(bulkRequest); + assertTrue(bulkResponse.took() > 0); + assertEquals(1, bulkResponse.items().size()); + assertEquals(Result.Updated.jsonValue(), bulkResponse.items().get(0).result()); + + } + public void testBulkUpdateUpsert() throws IOException { final String id = "100";