Skip to content

Commit 7cd0bdc

Browse files
authored
Add support for require_alias parameter to high level rest client (#67865)
The main changes are: * Set the require_alias parameter in the reindex, index and update methods in RequestConverters class. * Test that the parameter can be parsed correctly. Closes #67819.
1 parent 3d9bbd7 commit 7cd0bdc

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ static Request index(IndexRequest indexRequest) {
321321
parameters.withPipeline(indexRequest.getPipeline());
322322
parameters.withRefreshPolicy(indexRequest.getRefreshPolicy());
323323
parameters.withWaitForActiveShards(indexRequest.waitForActiveShards());
324+
parameters.withRequireAlias(indexRequest.isRequireAlias());
324325

325326
BytesRef source = indexRequest.source().toBytesRef();
326327
ContentType contentType = createContentType(indexRequest.getContentType());
@@ -347,6 +348,7 @@ static Request update(UpdateRequest updateRequest) throws IOException {
347348
parameters.withRetryOnConflict(updateRequest.retryOnConflict());
348349
parameters.withVersion(updateRequest.version());
349350
parameters.withVersionType(updateRequest.versionType());
351+
parameters.withRequireAlias(updateRequest.isRequireAlias());
350352

351353
// The Java API allows update requests with different content types
352354
// set for the partial document and the upsert document. This client
@@ -568,7 +570,8 @@ private static Request prepareReindexRequest(ReindexRequest reindexRequest, bool
568570
.withTimeout(reindexRequest.getTimeout())
569571
.withWaitForActiveShards(reindexRequest.getWaitForActiveShards())
570572
.withRequestsPerSecond(reindexRequest.getRequestsPerSecond())
571-
.withSlices(reindexRequest.getSlices());
573+
.withSlices(reindexRequest.getSlices())
574+
.withRequireAlias(reindexRequest.getDestination().isRequireAlias());
572575

573576
if (reindexRequest.getScrollTime() != null) {
574577
params.putParam("scroll", reindexRequest.getScrollTime());
@@ -1003,6 +1006,13 @@ Params withWaitForActiveShards(ActiveShardCount activeShardCount, ActiveShardCou
10031006
return this;
10041007
}
10051008

1009+
Params withRequireAlias(boolean requireAlias) {
1010+
if (requireAlias) {
1011+
return putParam("require_alias", Boolean.toString(requireAlias));
1012+
}
1013+
return this;
1014+
}
1015+
10061016
Params withIndicesOptions(IndicesOptions indicesOptions) {
10071017
if (indicesOptions != null) {
10081018
withIgnoreUnavailable(indicesOptions.ignoreUnavailable());

client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,19 @@ public void testIndex() throws IOException {
539539
assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, reason=[with_create_op_type]: " +
540540
"version conflict, document already exists (current version [1])]", exception.getMessage());
541541
}
542+
{
543+
ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> {
544+
IndexRequest indexRequest = new IndexRequest("index").id("require_alias");
545+
indexRequest.source(XContentBuilder.builder(xContentType.xContent()).startObject().field("field", "test").endObject());
546+
indexRequest.setRequireAlias(true);
547+
548+
execute(indexRequest, highLevelClient()::index, highLevelClient()::indexAsync);
549+
});
550+
551+
assertEquals(RestStatus.NOT_FOUND, exception.status());
552+
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index] and [require_alias]" +
553+
" request flag is [true] and [index] is not an alias]", exception.getMessage());
554+
}
542555
}
543556

544557
public void testUpdate() throws IOException {
@@ -717,6 +730,17 @@ public void testUpdate() throws IOException {
717730
assertEquals("Update request cannot have different content types for doc [JSON] and upsert [YAML] documents",
718731
exception.getMessage());
719732
}
733+
{
734+
ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> {
735+
UpdateRequest updateRequest = new UpdateRequest("index", "id");
736+
updateRequest.setRequireAlias(true);
737+
updateRequest.doc(new IndexRequest().source(Collections.singletonMap("field", "doc"), XContentType.JSON));
738+
execute(updateRequest, highLevelClient()::update, highLevelClient()::updateAsync);
739+
});
740+
assertEquals(RestStatus.NOT_FOUND, exception.status());
741+
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index] and [require_alias]" +
742+
" request flag is [true] and [index] is not an alias]", exception.getMessage());
743+
}
720744
}
721745

722746
public void testBulk() throws IOException {

client/rest-high-level/src/test/java/org/elasticsearch/client/ReindexIT.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
package org.elasticsearch.client;
1010

11+
import org.elasticsearch.ElasticsearchStatusException;
1112
import org.elasticsearch.action.ActionListener;
1213
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse;
1314
import org.elasticsearch.action.bulk.BulkItemResponse;
@@ -86,6 +87,24 @@ public void testReindex() throws IOException {
8687
assertEquals(0, bulkResponse.getBulkFailures().size());
8788
assertEquals(0, bulkResponse.getSearchFailures().size());
8889
}
90+
{
91+
// set require_alias to true but the destination index is not an alias
92+
ReindexRequest reindexRequest = new ReindexRequest();
93+
reindexRequest.setSourceIndices(sourceIndex);
94+
reindexRequest.setDestIndex(destinationIndex);
95+
reindexRequest.setSourceQuery(new IdsQueryBuilder().addIds("1"));
96+
reindexRequest.setRefresh(true);
97+
reindexRequest.setRequireAlias(true);
98+
99+
ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> {
100+
execute(reindexRequest, highLevelClient()::reindex, highLevelClient()::reindexAsync);
101+
});
102+
103+
assertEquals(RestStatus.NOT_FOUND, exception.status());
104+
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [" +
105+
destinationIndex + "] and [require_alias] request flag is [true] and [" +
106+
destinationIndex + "] is not an alias]", exception.getMessage());
107+
}
89108
}
90109

91110
public void testReindexTask() throws Exception {

client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,10 @@ public void testReindex() throws IOException {
437437
} else {
438438
expectedParams.put("slices", "1");
439439
}
440+
if (randomBoolean()) {
441+
reindexRequest.setRequireAlias(true);
442+
expectedParams.put("require_alias", "true");
443+
}
440444
setRandomTimeout(reindexRequest::setTimeout, ReplicationRequest.DEFAULT_TIMEOUT, expectedParams);
441445
setRandomWaitForActiveShards(reindexRequest::setWaitForActiveShards, ActiveShardCount.DEFAULT, expectedParams);
442446
expectedParams.put("scroll", reindexRequest.getScrollTime().getStringRep());
@@ -657,6 +661,11 @@ public void testIndex() throws IOException {
657661
}
658662
}
659663

664+
if (randomBoolean()) {
665+
indexRequest.setRequireAlias(true);
666+
expectedParams.put("require_alias", "true");
667+
}
668+
660669
XContentType xContentType = randomFrom(XContentType.values());
661670
int nbFields = randomIntBetween(0, 10);
662671
try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
@@ -746,6 +755,11 @@ public void testUpdate() throws IOException {
746755
randomizeFetchSourceContextParams(updateRequest::fetchSource, expectedParams);
747756
}
748757

758+
if (randomBoolean()) {
759+
updateRequest.setRequireAlias(true);
760+
expectedParams.put("require_alias", "true");
761+
}
762+
749763
Request request = RequestConverters.update(updateRequest);
750764
assertEquals("/" + index + "/_update/" + id, request.getEndpoint());
751765
assertEquals(expectedParams, request.getParameters());

0 commit comments

Comments
 (0)