Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk UpdateOperation misses upsert options #353

Merged
merged 2 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Fix /_nodes/stats, /_nodes/info throwing serialization error ([#315](https://github.com/opensearch-project/opensearch-java/pull/315))
- Do not double-wrap OpenSearchException on error ([#323](https://github.com/opensearch-project/opensearch-java/pull/323))
- Fix AwsSdk2TransportOptions.responseCompression ([#322](https://github.com/opensearch-project/opensearch-java/pull/322))
- Bulk UpdateOperation misses upsert options ([#353](https://github.com/opensearch-project/opensearch-java/pull/353))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
Expand All @@ -39,6 +39,7 @@
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;
Expand All @@ -52,7 +53,7 @@


public class UpdateOperation<TDocument> extends BulkOperationBase implements NdJsonpSerializable, BulkOperationVariant {
private final TDocument document;
private final UpdateOperationData<TDocument> data;

@Nullable
private final Boolean requireAlias;
Expand All @@ -67,8 +68,7 @@ public class UpdateOperation<TDocument> extends BulkOperationBase implements NdJ

private UpdateOperation(Builder<TDocument> builder) {
super(builder);
this.document = ApiTypeHelper.requireNonNull(builder.document, this, "document");

this.data = ApiTypeHelper.requireNonNull(builder.data, this, "data");
this.requireAlias = builder.requireAlias;
this.retryOnConflict = builder.retryOnConflict;
this.tDocumentSerializer = builder.tDocumentSerializer;
Expand All @@ -88,16 +88,9 @@ public BulkOperation.Kind _bulkOperationKind() {
return BulkOperation.Kind.Update;
}

/**
* Required - API name: {@code document}
*/
public final TDocument document() {
return this.document;
}

@Override
public Iterator<?> _serializables() {
return Arrays.asList(this, this.document).iterator();
return Arrays.asList(this, this.data).iterator();
}

/**
Expand Down Expand Up @@ -129,7 +122,6 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
generator.write(this.retryOnConflict);

}

}

// ---------------------------------------------------------------------------------------------
Expand All @@ -141,24 +133,61 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
public static class Builder<TDocument> extends BulkOperationBase.AbstractBuilder<Builder<TDocument>>
implements
ObjectBuilder<UpdateOperation<TDocument>> {

private UpdateOperationData<TDocument> data;

@Nullable
private TDocument document;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't document be required as non null?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the document or script could be specified, so it is nullable indeed


@Nullable
private Boolean requireAlias;

@Nullable
private Integer retryOnConflict;

@Nullable
private JsonpSerializer<TDocument> tDocumentSerializer;

@Nullable
private Boolean docAsUpsert;

@Nullable
private TDocument upsert;

@Nullable
private Script script;

/**
* Required - API name: {@code document}
* API name: {@code document}
*/
public final Builder<TDocument> document(TDocument value) {
this.document = value;
return this;
}

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

@Nullable
private Integer retryOnConflict;
/**
* API name: {@code upsert}
*/
public final Builder<TDocument> upsert(@Nullable TDocument value) {
this.upsert = value;
return this;
}

@Nullable
private JsonpSerializer<TDocument> tDocumentSerializer;
/**
* API name: {@code script}
*/
public final Builder<TDocument> script(@Nullable Script value) {
this.script = value;
return this;
}

/**
* API name: {@code require_alias}
Expand Down Expand Up @@ -194,11 +223,19 @@ protected Builder<TDocument> self() {
* Builds a {@link UpdateOperation}.
*
* @throws NullPointerException
* if some of the required fields are null.
* if some of the required fields are null.
*/
public UpdateOperation<TDocument> build() {
_checkSingleUse();

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

return new UpdateOperation<TDocument>(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

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

import javax.annotation.Nullable;

import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.JsonpSerializer;
import org.opensearch.client.json.JsonpUtils;
import org.opensearch.client.opensearch._types.Script;
import org.opensearch.client.util.ObjectBuilder;

import jakarta.json.stream.JsonGenerator;

public class UpdateOperationData<TDocument> implements JsonpSerializable {
@Nullable
private final TDocument document;

@Nullable
private final Boolean docAsUpsert;

@Nullable
private final TDocument upsert;

@Nullable
private final Script script;

@Nullable
private final JsonpSerializer<TDocument> tDocumentSerializer;

private UpdateOperationData(Builder<TDocument> builder) {
this.document = builder.document;
this.docAsUpsert = builder.docAsUpsert;
this.script = builder.script;
this.upsert = builder.upsert;
this.tDocumentSerializer = builder.tDocumentSerializer;

}

@Override
public void serialize(JsonGenerator generator, JsonpMapper mapper) {
generator.writeStartObject();
serializeInternal(generator, mapper);
generator.writeEnd();
}

protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
if (this.docAsUpsert != null) {
generator.writeKey("doc_as_upsert");
generator.write(this.docAsUpsert);
}

if (this.document != null) {
generator.writeKey("doc");
JsonpUtils.serialize(document, generator, tDocumentSerializer, mapper);
}

if (this.upsert != null) {
generator.writeKey("upsert");
JsonpUtils.serialize(upsert, generator, tDocumentSerializer, mapper);
}

if (this.script != null) {
generator.writeKey("script");
this.script.serialize(generator, mapper);
}
}

/**
* Builder for {@link UpdateOperationData}.
*/
public static class Builder<TDocument> extends BulkOperationBase.AbstractBuilder<Builder<TDocument>>
implements
ObjectBuilder<UpdateOperationData<TDocument>> {

@Nullable
private TDocument document;

@Nullable
private JsonpSerializer<TDocument> tDocumentSerializer;

@Nullable
private Boolean docAsUpsert;

@Nullable
private TDocument upsert;


@Nullable
private Script script;

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


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

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

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

/**
* Serializer for TDocument. If not set, an attempt will be made to find a
* serializer from the JSON context.
*/
public final Builder<TDocument> tDocumentSerializer(@Nullable JsonpSerializer<TDocument> value) {
this.tDocumentSerializer = value;
return this;
}

@Override
protected Builder<TDocument> self() {
return this;
}

/**
* Builds a {@link UpdateOperationData}.
*
* @throws NullPointerException
* if some of the required fields are null.
*/
public UpdateOperationData<TDocument> build() {
_checkSingleUse();

return new UpdateOperationData<TDocument>(this);
}
}
}
Loading