Skip to content

Commit

Permalink
Added scriptedUpsert and detectNoop options to UpdateOperation (opens…
Browse files Browse the repository at this point in the history
…earch-project#856)

Signed-off-by: Christian Winkler <cwinkler@integrationmatters.com>
  • Loading branch information
fs-chris authored Feb 20, 2024
1 parent 821dae6 commit 9aeff0c
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ public static class Builder<TDocument> extends BulkOperationBase.AbstractBuilder
@Nullable
private Boolean docAsUpsert;

@Nullable
private Boolean scriptedUpsert;

@Nullable
private Boolean detectNoop;

@Nullable
private TDocument upsert;

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

/**
* API name: {@code scripted_upsert}
*/
public final Builder<TDocument> scriptedUpsert(@Nullable Boolean value) {
this.scriptedUpsert = value;
return this;
}

/**
* API name: {@code detect_noop}
*/
public final Builder<TDocument> detectNoop(@Nullable Boolean value) {
this.detectNoop = value;
return this;
}

/**
* API name: {@code upsert}
*/
Expand Down Expand Up @@ -223,6 +245,8 @@ public UpdateOperation<TDocument> build() {

data = new UpdateOperationData.Builder<TDocument>().document(document)
.docAsUpsert(docAsUpsert)
.scriptedUpsert(scriptedUpsert)
.detectNoop(detectNoop)
.script(script)
.upsert(upsert)
.tDocumentSerializer(tDocumentSerializer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public class UpdateOperationData<TDocument> implements JsonpSerializable {
@Nullable
private final Boolean docAsUpsert;

@Nullable
private final Boolean scriptedUpsert;

@Nullable
private final Boolean detectNoop;

@Nullable
private final TDocument upsert;

Expand All @@ -36,6 +42,8 @@ public class UpdateOperationData<TDocument> implements JsonpSerializable {
private UpdateOperationData(Builder<TDocument> 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;
Expand All @@ -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);
Expand Down Expand Up @@ -87,6 +105,12 @@ public static class Builder<TDocument> extends BulkOperationBase.AbstractBuilder
@Nullable
private Boolean docAsUpsert;

@Nullable
private Boolean scriptedUpsert;

@Nullable
private Boolean detectNoop;

@Nullable
private TDocument upsert;

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

/**
* API name: {@code scripted_upsert}
*/
public final Builder<TDocument> scriptedUpsert(@Nullable Boolean value) {
this.scriptedUpsert = value;
return this;
}

/**
* API name: {@code detect_noop}
*/
public final Builder<TDocument> detectNoop(@Nullable Boolean value) {
this.detectNoop = value;
return this;
}

/**
* API name: {@code upsert}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppData> 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<AppData> 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";

Expand Down

0 comments on commit 9aeff0c

Please sign in to comment.