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

Add missed fields to bulk's UpdateOperation: source #947

Merged
merged 1 commit into from
Apr 15, 2024
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 @@ -36,6 +36,7 @@ This section is for maintaining a changelog for all breaking changes for the cli

### Added
- Add missed fields to PhraseSuggestOption: collapseMatch ([#940](https://github.com/opensearch-project/opensearch-java/pull/940))
- Add missed fields to bulk's UpdateOperation: source ([#947](https://github.com/opensearch-project/opensearch-java/pull/947))

### Dependencies

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.opensearch.client.json.JsonpSerializer;
import org.opensearch.client.json.NdJsonpSerializable;
import org.opensearch.client.opensearch._types.Script;
import org.opensearch.client.opensearch.core.search.SourceConfig;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;

Expand Down Expand Up @@ -156,6 +157,9 @@ public static class Builder<TDocument> extends BulkOperationBase.AbstractBuilder
@Nullable
private Script script;

@Nullable
private SourceConfig source;

/**
* API name: {@code document}
*/
Expand Down Expand Up @@ -220,6 +224,21 @@ public final Builder<TDocument> retryOnConflict(@Nullable Integer value) {
return this;
}

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

/**
* API name: {@code _source}
*/
public final Builder<TDocument> source(Function<SourceConfig.Builder, ObjectBuilder<SourceConfig>> fn) {
return this.source(fn.apply(new SourceConfig.Builder()).build());
}

/**
* Serializer for TDocument. If not set, an attempt will be made to find a
* serializer from the JSON context.
Expand Down Expand Up @@ -249,6 +268,7 @@ public UpdateOperation<TDocument> build() {
.detectNoop(detectNoop)
.script(script)
.upsert(upsert)
.source(source)
.tDocumentSerializer(tDocumentSerializer)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
package org.opensearch.client.opensearch.core.bulk;

import jakarta.json.stream.JsonGenerator;
import java.util.function.Function;
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.opensearch.core.search.SourceConfig;
import org.opensearch.client.util.ObjectBuilder;

public class UpdateOperationData<TDocument> implements JsonpSerializable {
Expand All @@ -39,6 +41,9 @@ public class UpdateOperationData<TDocument> implements JsonpSerializable {
@Nullable
private final JsonpSerializer<TDocument> tDocumentSerializer;

@Nullable
private final SourceConfig source;

private UpdateOperationData(Builder<TDocument> builder) {
this.document = builder.document;
this.docAsUpsert = builder.docAsUpsert;
Expand All @@ -47,7 +52,7 @@ private UpdateOperationData(Builder<TDocument> builder) {
this.script = builder.script;
this.upsert = builder.upsert;
this.tDocumentSerializer = builder.tDocumentSerializer;

this.source = builder.source;
}

@Override
Expand Down Expand Up @@ -87,6 +92,11 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
generator.writeKey("script");
this.script.serialize(generator, mapper);
}

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

/**
Expand Down Expand Up @@ -117,6 +127,9 @@ public static class Builder<TDocument> extends BulkOperationBase.AbstractBuilder
@Nullable
private Script script;

@Nullable
private SourceConfig source;

/**
* API name: {@code document}
*/
Expand Down Expand Up @@ -165,6 +178,21 @@ public final Builder<TDocument> script(@Nullable Script value) {
return this;
}

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

/**
* API name: {@code _source}
*/
public final Builder<TDocument> source(Function<SourceConfig.Builder, ObjectBuilder<SourceConfig>> fn) {
return this.source(fn.apply(new SourceConfig.Builder()).build());
}

/**
* Serializer for TDocument. If not set, an attempt will be made to find a
* serializer from the JSON context.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public void testUpdate() throws Exception {
UpdateRequest<AppData, AppData> updateRequest = new UpdateRequest.Builder<AppData, AppData>().index("index")
.id("does_not_exist")
.doc(appData)
.source(s -> s.fetch(randomBoolean()))
.build();
try {
javaClient().update(updateRequest, AppData.class);
Expand Down
Loading