use of org.opensearch.action.admin.indices.alias.IndicesAliasesRequest in project OpenSearch by opensearch-project.
the class IndicesRequestConverters method updateAliases.
static Request updateAliases(IndicesAliasesRequest indicesAliasesRequest) throws IOException {
Request request = new Request(HttpPost.METHOD_NAME, "/_aliases");
RequestConverters.Params parameters = new RequestConverters.Params();
parameters.withTimeout(indicesAliasesRequest.timeout());
parameters.withMasterTimeout(indicesAliasesRequest.masterNodeTimeout());
request.addParameters(parameters.asMap());
request.setEntity(RequestConverters.createEntity(indicesAliasesRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
return request;
}
use of org.opensearch.action.admin.indices.alias.IndicesAliasesRequest in project OpenSearch by opensearch-project.
the class IndicesRequestConvertersTests method testUpdateAliases.
public void testUpdateAliases() throws IOException {
IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest();
IndicesAliasesRequest.AliasActions aliasAction = randomAliasAction();
indicesAliasesRequest.addAliasAction(aliasAction);
Map<String, String> expectedParams = new HashMap<>();
RequestConvertersTests.setRandomTimeout(indicesAliasesRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
RequestConvertersTests.setRandomMasterTimeout(indicesAliasesRequest, expectedParams);
Request request = IndicesRequestConverters.updateAliases(indicesAliasesRequest);
Assert.assertEquals("/_aliases", request.getEndpoint());
Assert.assertEquals(expectedParams, request.getParameters());
RequestConvertersTests.assertToXContentBody(indicesAliasesRequest, request.getEntity());
}
use of org.opensearch.action.admin.indices.alias.IndicesAliasesRequest in project OpenSearch by opensearch-project.
the class IndicesClientIT method testAliasesNonExistentIndex.
public void testAliasesNonExistentIndex() throws IOException {
String index = "index";
String alias = "alias";
String nonExistentIndex = "non_existent_index";
IndicesAliasesRequest nonExistentIndexRequest = new IndicesAliasesRequest();
nonExistentIndexRequest.addAliasAction(new AliasActions(AliasActions.Type.ADD).index(nonExistentIndex).alias(alias));
OpenSearchException exception = expectThrows(OpenSearchException.class, () -> execute(nonExistentIndexRequest, highLevelClient().indices()::updateAliases, highLevelClient().indices()::updateAliasesAsync));
assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(exception.getMessage(), equalTo("OpenSearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
assertThat(exception.getMetadata("opensearch.index"), hasItem(nonExistentIndex));
createIndex(index, Settings.EMPTY);
IndicesAliasesRequest mixedRequest = new IndicesAliasesRequest();
mixedRequest.addAliasAction(new AliasActions(AliasActions.Type.ADD).indices(index).aliases(alias));
mixedRequest.addAliasAction(new AliasActions(AliasActions.Type.REMOVE).indices(nonExistentIndex).alias(alias));
exception = expectThrows(OpenSearchStatusException.class, () -> execute(mixedRequest, highLevelClient().indices()::updateAliases, highLevelClient().indices()::updateAliasesAsync));
assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(exception.getMessage(), equalTo("OpenSearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
assertThat(exception.getMetadata("opensearch.index"), hasItem(nonExistentIndex));
assertThat(exception.getMetadata("opensearch.index"), not(hasItem(index)));
assertThat(aliasExists(index, alias), equalTo(false));
assertThat(aliasExists(alias), equalTo(false));
IndicesAliasesRequest removeIndexRequest = new IndicesAliasesRequest();
removeIndexRequest.addAliasAction(new AliasActions(AliasActions.Type.ADD).index(nonExistentIndex).alias(alias));
removeIndexRequest.addAliasAction(new AliasActions(AliasActions.Type.REMOVE_INDEX).indices(nonExistentIndex));
exception = expectThrows(OpenSearchException.class, () -> execute(removeIndexRequest, highLevelClient().indices()::updateAliases, highLevelClient().indices()::updateAliasesAsync));
assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(exception.getMessage(), equalTo("OpenSearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
assertThat(exception.getMetadata("opensearch.index"), hasItem(nonExistentIndex));
assertThat(exception.getMetadata("opensearch.index"), not(hasItem(index)));
assertThat(aliasExists(index, alias), equalTo(false));
assertThat(aliasExists(alias), equalTo(false));
}
use of org.opensearch.action.admin.indices.alias.IndicesAliasesRequest in project OpenSearch by opensearch-project.
the class IndicesClientIT method testUpdateAliases.
@SuppressWarnings("unchecked")
public void testUpdateAliases() throws IOException {
String index = "index";
String alias = "alias";
createIndex(index, Settings.EMPTY);
assertThat(aliasExists(index, alias), equalTo(false));
assertThat(aliasExists(alias), equalTo(false));
IndicesAliasesRequest aliasesAddRequest = new IndicesAliasesRequest();
AliasActions addAction = new AliasActions(AliasActions.Type.ADD).index(index).aliases(alias);
if (randomBoolean()) {
addAction.writeIndex(randomBoolean());
}
addAction.routing("routing").searchRouting("search_routing").filter("{\"term\":{\"year\":2016}}");
aliasesAddRequest.addAliasAction(addAction);
AcknowledgedResponse aliasesAddResponse = execute(aliasesAddRequest, highLevelClient().indices()::updateAliases, highLevelClient().indices()::updateAliasesAsync);
assertTrue(aliasesAddResponse.isAcknowledged());
assertThat(aliasExists(alias), equalTo(true));
assertThat(aliasExists(index, alias), equalTo(true));
Map<String, Object> getAlias = getAlias(index, alias);
assertThat(getAlias.get("index_routing"), equalTo("routing"));
assertThat(getAlias.get("search_routing"), equalTo("search_routing"));
Map<String, Object> filter = (Map<String, Object>) getAlias.get("filter");
Map<String, Object> term = (Map<String, Object>) filter.get("term");
assertEquals(2016, term.get("year"));
Boolean isWriteIndex = (Boolean) getAlias.get("is_write_index");
assertThat(isWriteIndex, equalTo(addAction.writeIndex()));
String alias2 = "alias2";
IndicesAliasesRequest aliasesAddRemoveRequest = new IndicesAliasesRequest();
addAction = new AliasActions(AliasActions.Type.ADD).indices(index).alias(alias2);
aliasesAddRemoveRequest.addAliasAction(addAction);
AliasActions removeAction = new AliasActions(AliasActions.Type.REMOVE).index(index).alias(alias);
aliasesAddRemoveRequest.addAliasAction(removeAction);
AcknowledgedResponse aliasesAddRemoveResponse = execute(aliasesAddRemoveRequest, highLevelClient().indices()::updateAliases, highLevelClient().indices()::updateAliasesAsync);
assertTrue(aliasesAddRemoveResponse.isAcknowledged());
assertThat(aliasExists(alias), equalTo(false));
assertThat(aliasExists(alias2), equalTo(true));
assertThat(aliasExists(index, alias), equalTo(false));
assertThat(aliasExists(index, alias2), equalTo(true));
IndicesAliasesRequest aliasesRemoveIndexRequest = new IndicesAliasesRequest();
AliasActions removeIndexAction = new AliasActions(AliasActions.Type.REMOVE_INDEX).index(index);
aliasesRemoveIndexRequest.addAliasAction(removeIndexAction);
AcknowledgedResponse aliasesRemoveIndexResponse = execute(aliasesRemoveIndexRequest, highLevelClient().indices()::updateAliases, highLevelClient().indices()::updateAliasesAsync);
assertTrue(aliasesRemoveIndexResponse.isAcknowledged());
assertThat(aliasExists(alias), equalTo(false));
assertThat(aliasExists(alias2), equalTo(false));
assertThat(aliasExists(index, alias), equalTo(false));
assertThat(aliasExists(index, alias2), equalTo(false));
assertThat(indexExists(index), equalTo(false));
}
use of org.opensearch.action.admin.indices.alias.IndicesAliasesRequest in project OpenSearch by opensearch-project.
the class RestIndexDeleteAliasesAction method prepareRequest.
@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
final String[] aliases = Strings.splitStringByCommaToArray(request.param("name"));
IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest();
indicesAliasesRequest.timeout(request.paramAsTime("timeout", indicesAliasesRequest.timeout()));
indicesAliasesRequest.addAliasAction(AliasActions.remove().indices(indices).aliases(aliases));
indicesAliasesRequest.masterNodeTimeout(request.paramAsTime("master_timeout", indicesAliasesRequest.masterNodeTimeout()));
return channel -> client.admin().indices().aliases(indicesAliasesRequest, new RestToXContentListener<>(channel));
}
Aggregations