Enable RestHighLevel-Client to set parameter require_alias for bulk index and reindex requests (#1533)

Signed-off-by: Jan Baudisch <jan.baudisch.libri@gmail.com>

Co-authored-by: Jan Baudisch <jan.baudisch.libri@gmail.com>
This commit is contained in:
Jan Baudisch 2021-11-22 18:50:35 +01:00 committed by GitHub
parent 79eaa06612
commit 4eaa60ce60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 0 deletions

View File

@ -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));

View File

@ -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 {

View File

@ -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 {