Skip to content

Commit bd32191

Browse files
Add support for require_alias parameter to high level rest client (#71914)
Backporting #67865 to 7.x branch. 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. Co-authored-by: bellengao <[email protected]>
1 parent 160141a commit bd32191

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
@@ -335,6 +335,7 @@ static Request index(IndexRequest indexRequest) {
335335
parameters.withPipeline(indexRequest.getPipeline());
336336
parameters.withRefreshPolicy(indexRequest.getRefreshPolicy());
337337
parameters.withWaitForActiveShards(indexRequest.waitForActiveShards());
338+
parameters.withRequireAlias(indexRequest.isRequireAlias());
338339

339340
BytesRef source = indexRequest.source().toBytesRef();
340341
ContentType contentType = createContentType(indexRequest.getContentType());
@@ -363,6 +364,7 @@ static Request update(UpdateRequest updateRequest) throws IOException {
363364
parameters.withRetryOnConflict(updateRequest.retryOnConflict());
364365
parameters.withVersion(updateRequest.version());
365366
parameters.withVersionType(updateRequest.versionType());
367+
parameters.withRequireAlias(updateRequest.isRequireAlias());
366368

367369
// The Java API allows update requests with different content types
368370
// set for the partial document and the upsert document. This client
@@ -586,7 +588,8 @@ private static Request prepareReindexRequest(ReindexRequest reindexRequest, bool
586588
.withTimeout(reindexRequest.getTimeout())
587589
.withWaitForActiveShards(reindexRequest.getWaitForActiveShards())
588590
.withRequestsPerSecond(reindexRequest.getRequestsPerSecond())
589-
.withSlices(reindexRequest.getSlices());
591+
.withSlices(reindexRequest.getSlices())
592+
.withRequireAlias(reindexRequest.getDestination().isRequireAlias());
590593

591594
if (reindexRequest.getScrollTime() != null) {
592595
params.putParam("scroll", reindexRequest.getScrollTime());
@@ -1014,6 +1017,13 @@ Params withWaitForActiveShards(ActiveShardCount activeShardCount, ActiveShardCou
10141017
return this;
10151018
}
10161019

1020+
Params withRequireAlias(boolean requireAlias) {
1021+
if (requireAlias) {
1022+
return putParam("require_alias", Boolean.toString(requireAlias));
1023+
}
1024+
return this;
1025+
}
1026+
10171027
Params withIndicesOptions(IndicesOptions indicesOptions) {
10181028
if (indicesOptions != null) {
10191029
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
@@ -641,6 +641,19 @@ public void testIndex() throws IOException {
641641
assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, reason=[with_create_op_type]: " +
642642
"version conflict, document already exists (current version [1])]", exception.getMessage());
643643
}
644+
{
645+
ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> {
646+
IndexRequest indexRequest = new IndexRequest("index").id("require_alias");
647+
indexRequest.source(XContentBuilder.builder(xContentType.xContent()).startObject().field("field", "test").endObject());
648+
indexRequest.setRequireAlias(true);
649+
650+
execute(indexRequest, highLevelClient()::index, highLevelClient()::indexAsync);
651+
});
652+
653+
assertEquals(RestStatus.NOT_FOUND, exception.status());
654+
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index] and [require_alias]" +
655+
" request flag is [true] and [index] is not an alias]", exception.getMessage());
656+
}
644657
}
645658

646659
public void testIndexWithTypes() throws IOException {
@@ -838,6 +851,17 @@ public void testUpdate() throws IOException {
838851
assertEquals("Update request cannot have different content types for doc [JSON] and upsert [YAML] documents",
839852
exception.getMessage());
840853
}
854+
{
855+
ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> {
856+
UpdateRequest updateRequest = new UpdateRequest("index", "id");
857+
updateRequest.setRequireAlias(true);
858+
updateRequest.doc(new IndexRequest().source(Collections.singletonMap("field", "doc"), XContentType.JSON));
859+
execute(updateRequest, highLevelClient()::update, highLevelClient()::updateAsync);
860+
});
861+
assertEquals(RestStatus.NOT_FOUND, exception.status());
862+
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index] and [require_alias]" +
863+
" request flag is [true] and [index] is not an alias]", exception.getMessage());
864+
}
841865
}
842866

843867
public void testUpdateWithTypes() 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
@@ -495,6 +495,10 @@ public void testReindex() throws IOException {
495495
} else {
496496
expectedParams.put("slices", "1");
497497
}
498+
if (randomBoolean()) {
499+
reindexRequest.setRequireAlias(true);
500+
expectedParams.put("require_alias", "true");
501+
}
498502
setRandomTimeout(reindexRequest::setTimeout, ReplicationRequest.DEFAULT_TIMEOUT, expectedParams);
499503
setRandomWaitForActiveShards(reindexRequest::setWaitForActiveShards, ActiveShardCount.DEFAULT, expectedParams);
500504
expectedParams.put("scroll", reindexRequest.getScrollTime().getStringRep());
@@ -735,6 +739,11 @@ public void testIndex() throws IOException {
735739
}
736740
}
737741

742+
if (randomBoolean()) {
743+
indexRequest.setRequireAlias(true);
744+
expectedParams.put("require_alias", "true");
745+
}
746+
738747
XContentType xContentType = randomFrom(XContentType.values());
739748
int nbFields = randomIntBetween(0, 10);
740749
try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
@@ -867,6 +876,11 @@ public void testUpdate() throws IOException {
867876
randomizeFetchSourceContextParams(updateRequest::fetchSource, expectedParams);
868877
}
869878

879+
if (randomBoolean()) {
880+
updateRequest.setRequireAlias(true);
881+
expectedParams.put("require_alias", "true");
882+
}
883+
870884
Request request = RequestConverters.update(updateRequest);
871885
assertEquals("/" + index + "/_update/" + id, request.getEndpoint());
872886
assertEquals(expectedParams, request.getParameters());

0 commit comments

Comments
 (0)