diff --git a/client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java b/client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java index acafcf96c2a..f0f33ae1e71 100644 --- a/client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java +++ b/client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java @@ -363,6 +363,7 @@ final class RequestConverters { parameters.withPipeline(indexRequest.getPipeline()); parameters.withRefreshPolicy(indexRequest.getRefreshPolicy()); parameters.withWaitForActiveShards(indexRequest.waitForActiveShards()); + parameters.withRequireAlias(indexRequest.isRequireAlias()); BytesRef source = indexRequest.source().toBytesRef(); ContentType contentType = createContentType(indexRequest.getContentType()); @@ -391,6 +392,7 @@ final class RequestConverters { parameters.withRetryOnConflict(updateRequest.retryOnConflict()); parameters.withVersion(updateRequest.version()); parameters.withVersionType(updateRequest.versionType()); + parameters.withRequireAlias(updateRequest.isRequireAlias()); // The Java API allows update requests with different content types // set for the partial document and the upsert document. This client @@ -618,6 +620,7 @@ final class RequestConverters { .withTimeout(reindexRequest.getTimeout()) .withWaitForActiveShards(reindexRequest.getWaitForActiveShards()) .withRequestsPerSecond(reindexRequest.getRequestsPerSecond()) + .withRequireAlias(reindexRequest.getDestination().isRequireAlias()) .withSlices(reindexRequest.getSlices()); if (reindexRequest.getScrollTime() != null) { @@ -964,6 +967,13 @@ final class RequestConverters { } } + Params withRequireAlias(boolean requireAlias) { + if (requireAlias) { + return putParam("require_alias", Boolean.TRUE.toString()); + } + return this; + } + Params withRetryOnConflict(int retryOnConflict) { if (retryOnConflict > 0) { return putParam("retry_on_conflict", String.valueOf(retryOnConflict)); diff --git a/client/rest-high-level/src/test/java/org/opensearch/client/CrudIT.java b/client/rest-high-level/src/test/java/org/opensearch/client/CrudIT.java index 56d46357726..69602bb766d 100644 --- a/client/rest-high-level/src/test/java/org/opensearch/client/CrudIT.java +++ b/client/rest-high-level/src/test/java/org/opensearch/client/CrudIT.java @@ -686,6 +686,21 @@ public class CrudIT extends OpenSearchRestHighLevelClientTestCase { exception.getMessage() ); } + { + OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class, () -> { + IndexRequest indexRequest = new IndexRequest("index").id("missing_alias").setRequireAlias(true); + indexRequest.source(XContentBuilder.builder(xContentType.xContent()).startObject().field("field", "test").endObject()); + + execute(indexRequest, highLevelClient()::index, highLevelClient()::indexAsync); + }); + + assertEquals(RestStatus.NOT_FOUND, exception.status()); + assertEquals( + "OpenSearch exception [type=index_not_found_exception, reason=no such index [index]" + + " and [require_alias] request flag is [true] and [index] is not an alias]", + exception.getMessage() + ); + } { IndexRequest indexRequest = new IndexRequest("index").id("external_version_type"); indexRequest.source(XContentBuilder.builder(xContentType.xContent()).startObject().field("field", "test").endObject()); @@ -926,6 +941,18 @@ public class CrudIT extends OpenSearchRestHighLevelClientTestCase { exception.getMessage() ); } + { + OpenSearchException exception = expectThrows(OpenSearchException.class, () -> { + UpdateRequest updateRequest = new UpdateRequest("index", "require_alias").setRequireAlias(true); + updateRequest.doc(new IndexRequest().source(Collections.singletonMap("field", "doc"), XContentType.JSON)); + execute(updateRequest, highLevelClient()::update, highLevelClient()::updateAsync); + }); + assertEquals(RestStatus.NOT_FOUND, exception.status()); + assertEquals( + "OpenSearch exception [type=index_not_found_exception, reason=no such index [index] and [require_alias] request flag is [true] and [index] is not an alias]", + exception.getMessage() + ); + } } public void testUpdateWithTypes() throws IOException { diff --git a/client/rest-high-level/src/test/java/org/opensearch/client/ReindexIT.java b/client/rest-high-level/src/test/java/org/opensearch/client/ReindexIT.java index 5576b9d5ef2..78fb2aef02e 100644 --- a/client/rest-high-level/src/test/java/org/opensearch/client/ReindexIT.java +++ b/client/rest-high-level/src/test/java/org/opensearch/client/ReindexIT.java @@ -32,6 +32,7 @@ package org.opensearch.client; +import org.opensearch.OpenSearchStatusException; import org.opensearch.action.ActionListener; import org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse; import org.opensearch.action.bulk.BulkItemResponse; @@ -102,6 +103,25 @@ public class ReindexIT extends OpenSearchRestHighLevelClientTestCase { assertEquals(0, bulkResponse.getBulkFailures().size()); assertEquals(0, bulkResponse.getSearchFailures().size()); } + { + // set require_alias=true, but there exists no alias + ReindexRequest reindexRequest = new ReindexRequest(); + reindexRequest.setSourceIndices(sourceIndex); + reindexRequest.setDestIndex(destinationIndex); + reindexRequest.setSourceQuery(new IdsQueryBuilder().addIds("1")); + reindexRequest.setRefresh(true); + reindexRequest.setRequireAlias(true); + + OpenSearchStatusException exception = expectThrows( + OpenSearchStatusException.class, + () -> { execute(reindexRequest, highLevelClient()::reindex, highLevelClient()::reindexAsync); } + ); + assertEquals(RestStatus.NOT_FOUND, exception.status()); + assertEquals( + "OpenSearch exception [type=index_not_found_exception, reason=no such index [dest] and [require_alias] request flag is [true] and [dest] is not an alias]", + exception.getMessage() + ); + } } public void testReindexTask() throws Exception {