Skip to content
Open
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 @@ -23,6 +23,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Add document lifecycle guide and runnable sample ([#2017](https://github.com/opensearch-project/opensearch-java/pull/2017))

### Fixed
- Allow bulk shard failures without shard ids ([#2023](https://github.com/opensearch-project/opensearch-java/pull/2023))

## [Unreleased 3.x]
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,16 @@ public class ShardSearchFailure implements PlainJsonSerializable, ToCopyableBuil
@Nonnull
private final ErrorCause reason;

private final int shard;
@Nullable
private final Integer shard;

// ---------------------------------------------------------------------------------------------

private ShardSearchFailure(Builder builder) {
this.index = builder.index;
this.node = builder.node;
this.reason = ApiTypeHelper.requireNonNull(builder.reason, this, "reason");
this.shard = ApiTypeHelper.requireNonNull(builder.shard, this, "shard");
this.shard = builder.shard;
}

public static ShardSearchFailure of(Function<ShardSearchFailure.Builder, ObjectBuilder<ShardSearchFailure>> fn) {
Expand Down Expand Up @@ -118,12 +119,13 @@ public final ErrorCause reason() {
}

/**
* Required - The shard id where the failure occurred.
* The shard id where the failure occurred.
* <p>
* API name: {@code shard}
* </p>
*/
public final int shard() {
@Nullable
public final Integer shard() {
return this.shard;
}

Expand Down Expand Up @@ -151,8 +153,10 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
generator.writeKey("reason");
this.reason.serialize(generator, mapper);

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

// ---------------------------------------------------------------------------------------------
Expand All @@ -177,6 +181,7 @@ public static class Builder extends ObjectBuilderBase implements CopyableBuilder
@Nullable
private String node;
private ErrorCause reason;
@Nullable
private Integer shard;

public Builder() {}
Expand Down Expand Up @@ -243,13 +248,13 @@ public final Builder reason(Function<ErrorCause.Builder, ObjectBuilder<ErrorCaus
}

/**
* Required - The shard id where the failure occurred.
* The shard id where the failure occurred.
* <p>
* API name: {@code shard}
* </p>
*/
@Nonnull
public final Builder shard(int value) {
public final Builder shard(@Nullable Integer value) {
this.shard = value;
return this;
}
Expand Down Expand Up @@ -291,7 +296,7 @@ public int hashCode() {
result = 31 * result + Objects.hashCode(this.index);
result = 31 * result + Objects.hashCode(this.node);
result = 31 * result + this.reason.hashCode();
result = 31 * result + Integer.hashCode(this.shard);
result = 31 * result + Objects.hashCode(this.shard);
return result;
}

Expand All @@ -303,6 +308,6 @@ public boolean equals(Object o) {
return Objects.equals(this.index, other.index)
&& Objects.equals(this.node, other.node)
&& this.reason.equals(other.reason)
&& this.shard == other.shard;
&& Objects.equals(this.shard, other.shard);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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;

import org.junit.Test;
import org.opensearch.client.opensearch._types.ShardSearchFailure;
import org.opensearch.client.opensearch.core.bulk.BulkResponseItem;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class BulkResponseTest extends ModelTestCase {

@Test
public void shouldDeserializeBulkResponseShardFailureWithoutShard() {
String json = "{\"took\":1,\"errors\":true,\"items\":[{\"index\":{\"_index\":\"products\",\"status\":500,"
+ "\"error\":{\"type\":\"illegal_argument_exception\",\"reason\":\"boom\"},"
+ "\"_shards\":{\"total\":2,\"successful\":1,\"failed\":1,\"failures\":[{\"index\":\"products\","
+ "\"node\":\"node-1\",\"reason\":{\"type\":\"illegal_argument_exception\",\"reason\":\"boom\"}}]}}}]}";

BulkResponse response = fromJson(json, BulkResponse._DESERIALIZER);

BulkResponseItem item = response.items().get(0);
assertEquals(500, item.status());
assertNotNull(item.shards());
ShardSearchFailure failure = item.shards().failures().get(0);
assertEquals("products", failure.index());
assertNull(failure.shard());
assertEquals("illegal_argument_exception", failure.reason().type());
}

@Test
public void shouldDeserializeBulkResponseShardFailureWithShard() {
String json = "{\"took\":1,\"errors\":true,\"items\":[{\"index\":{\"_index\":\"products\",\"status\":500,"
+ "\"error\":{\"type\":\"illegal_argument_exception\",\"reason\":\"boom\"},"
+ "\"_shards\":{\"total\":2,\"successful\":1,\"failed\":1,\"failures\":[{\"index\":\"products\","
+ "\"node\":\"node-1\",\"shard\":3,\"reason\":{\"type\":\"illegal_argument_exception\",\"reason\":\"boom\"}}]}}}]}";

BulkResponse response = fromJson(json, BulkResponse._DESERIALIZER);

BulkResponseItem item = response.items().get(0);
assertNotNull(item.shards());
ShardSearchFailure failure = item.shards().failures().get(0);
assertEquals("products", failure.index());
assertEquals(Integer.valueOf(3), failure.shard());
assertEquals("illegal_argument_exception", failure.reason().type());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ private static JsonPointer schema(String namespace, String name) {
.with(schema("_common", "StringifiedVersionNumber"), so -> so.withMappedType(Types.Primitive.Long))
.with(schema("_common", "Void"), so -> so.withMappedType(Types.Primitive.Void))

.with(schema("_common", "ShardSearchFailure"), so -> so.withProperties(p -> p.with("shard", po -> po.withRequired(false))))

.with(schema("_common", "ScriptSort"), so -> so.withShouldGenerate(ShouldGenerate.Always))
.with(
schema("_common", "SortOptions"),
Expand Down
Loading