Skip to content

Commit

Permalink
Added scriptedUpsert option to UpdateOperation (opensearch-project#744)
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Winkler <[email protected]>
  • Loading branch information
fs-chris committed Nov 29, 2023
1 parent 5edb45a commit 0e7929f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@

package org.opensearch.client.opensearch.core.bulk;

import jakarta.json.stream.JsonGenerator;
import java.util.Arrays;
import java.util.Iterator;
import java.util.function.Function;

import javax.annotation.Nullable;

import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializer;
import org.opensearch.client.json.NdJsonpSerializable;
import org.opensearch.client.opensearch._types.Script;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;

import jakarta.json.stream.JsonGenerator;

// typedef: _global.bulk.UpdateOperation

public class UpdateOperation<TDocument> extends BulkOperationBase implements NdJsonpSerializable, BulkOperationVariant {
Expand All @@ -69,7 +72,8 @@ private UpdateOperation(Builder<TDocument> builder) {

}

public static <TDocument> UpdateOperation<TDocument> of(Function<Builder<TDocument>, ObjectBuilder<UpdateOperation<TDocument>>> fn) {
public static <TDocument> UpdateOperation<TDocument>
of(Function<Builder<TDocument>, ObjectBuilder<UpdateOperation<TDocument>>> fn) {
return fn.apply(new Builder<>()).build();
}

Expand Down Expand Up @@ -102,6 +106,7 @@ public final Integer retryOnConflict() {
return this.retryOnConflict;
}

@Override
protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

super.serializeInternal(generator, mapper);
Expand All @@ -125,7 +130,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

public static class Builder<TDocument> extends BulkOperationBase.AbstractBuilder<Builder<TDocument>>
implements
ObjectBuilder<UpdateOperation<TDocument>> {
ObjectBuilder<UpdateOperation<TDocument>> {

private UpdateOperationData<TDocument> data;

Expand All @@ -144,6 +149,9 @@ public static class Builder<TDocument> extends BulkOperationBase.AbstractBuilder
@Nullable
private Boolean docAsUpsert;

@Nullable
private Boolean scriptedUpsert;

@Nullable
private TDocument upsert;

Expand All @@ -166,6 +174,14 @@ 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 upsert}
*/
Expand Down Expand Up @@ -218,17 +234,19 @@ protected Builder<TDocument> self() {
* @throws NullPointerException
* if some of the required fields are null.
*/
@Override
public UpdateOperation<TDocument> build() {
_checkSingleUse();

data = new UpdateOperationData.Builder<TDocument>().document(document)
.docAsUpsert(docAsUpsert)
.scriptedUpsert(scriptedUpsert)
.script(script)
.upsert(upsert)
.tDocumentSerializer(tDocumentSerializer)
.build();

return new UpdateOperation<TDocument>(this);
return new UpdateOperation<>(this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class UpdateOperationData<TDocument> implements JsonpSerializable {
@Nullable
private final Boolean docAsUpsert;

@Nullable
private final Boolean scriptedUpsert;

@Nullable
private final TDocument upsert;

Expand All @@ -36,6 +39,7 @@ public class UpdateOperationData<TDocument> implements JsonpSerializable {
private UpdateOperationData(Builder<TDocument> builder) {
this.document = builder.document;
this.docAsUpsert = builder.docAsUpsert;
this.scriptedUpsert = builder.scriptedUpsert;
this.script = builder.script;
this.upsert = builder.upsert;
this.tDocumentSerializer = builder.tDocumentSerializer;
Expand All @@ -55,6 +59,11 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
generator.write(this.docAsUpsert);
}

if (this.scriptedUpsert != null) {
generator.writeKey("scripted_upsert");
generator.write(scriptedUpsert);
}

if (this.document != null) {
generator.writeKey("doc");
JsonpUtils.serialize(document, generator, tDocumentSerializer, mapper);
Expand Down Expand Up @@ -87,6 +96,9 @@ public static class Builder<TDocument> extends BulkOperationBase.AbstractBuilder
@Nullable
private Boolean docAsUpsert;

@Nullable
private Boolean scriptedUpsert;

@Nullable
private TDocument upsert;

Expand All @@ -109,6 +121,14 @@ 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 upsert}
*/
Expand Down

0 comments on commit 0e7929f

Please sign in to comment.