Search in sources :

Example 1 with IndicesAliasesRequest

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;
}
Also used : UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) SimulateIndexTemplateRequest(org.opensearch.client.indices.SimulateIndexTemplateRequest) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest) RefreshRequest(org.opensearch.action.admin.indices.refresh.RefreshRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) DeleteAliasRequest(org.opensearch.client.indices.DeleteAliasRequest) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) GetFieldMappingsRequest(org.opensearch.client.indices.GetFieldMappingsRequest) ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) GetSettingsRequest(org.opensearch.action.admin.indices.settings.get.GetSettingsRequest) GetDataStreamRequest(org.opensearch.client.indices.GetDataStreamRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) ComposableIndexTemplateExistRequest(org.opensearch.client.indices.ComposableIndexTemplateExistRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) ResizeRequest(org.opensearch.client.indices.ResizeRequest) GetComposableIndexTemplateRequest(org.opensearch.client.indices.GetComposableIndexTemplateRequest) DataStreamsStatsRequest(org.opensearch.client.indices.DataStreamsStatsRequest) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) CloseIndexRequest(org.opensearch.client.indices.CloseIndexRequest) GetIndexTemplatesRequest(org.opensearch.client.indices.GetIndexTemplatesRequest) GetMappingsRequest(org.opensearch.client.indices.GetMappingsRequest) GetAliasesRequest(org.opensearch.action.admin.indices.alias.get.GetAliasesRequest) PutComposableIndexTemplateRequest(org.opensearch.client.indices.PutComposableIndexTemplateRequest) CreateDataStreamRequest(org.opensearch.client.indices.CreateDataStreamRequest) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) DeleteComposableIndexTemplateRequest(org.opensearch.client.indices.DeleteComposableIndexTemplateRequest) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) DeleteDataStreamRequest(org.opensearch.client.indices.DeleteDataStreamRequest) ClearIndicesCacheRequest(org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) IndexTemplatesExistRequest(org.opensearch.client.indices.IndexTemplatesExistRequest) RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest)

Example 2 with IndicesAliasesRequest

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());
}
Also used : HashMap(java.util.HashMap) UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) RefreshRequest(org.opensearch.action.admin.indices.refresh.RefreshRequest) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) GetDataStreamRequest(org.opensearch.client.indices.GetDataStreamRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) DeleteDataStreamRequest(org.opensearch.client.indices.DeleteDataStreamRequest) ClearIndicesCacheRequest(org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) DeleteAliasRequest(org.opensearch.client.indices.DeleteAliasRequest) GetFieldMappingsRequest(org.opensearch.client.indices.GetFieldMappingsRequest) GetSettingsRequest(org.opensearch.action.admin.indices.settings.get.GetSettingsRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) ResizeRequest(org.opensearch.client.indices.ResizeRequest) CloseIndexRequest(org.opensearch.client.indices.CloseIndexRequest) GetIndexTemplatesRequest(org.opensearch.client.indices.GetIndexTemplatesRequest) GetMappingsRequest(org.opensearch.client.indices.GetMappingsRequest) GetAliasesRequest(org.opensearch.action.admin.indices.alias.get.GetAliasesRequest) CreateDataStreamRequest(org.opensearch.client.indices.CreateDataStreamRequest) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) IndexTemplatesExistRequest(org.opensearch.client.indices.IndexTemplatesExistRequest) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest)

Example 3 with IndicesAliasesRequest

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));
}
Also used : IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) OpenSearchException(org.opensearch.OpenSearchException) Matchers.containsString(org.hamcrest.Matchers.containsString) AliasActions(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 4 with IndicesAliasesRequest

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));
}
Also used : AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map) HashMap(java.util.HashMap) AliasActions(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions)

Example 5 with IndicesAliasesRequest

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));
}
Also used : AliasActions(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) DELETE(org.opensearch.rest.RestRequest.Method.DELETE) List(java.util.List) NodeClient(org.opensearch.client.node.NodeClient) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Collections.unmodifiableList(java.util.Collections.unmodifiableList) RestRequest(org.opensearch.rest.RestRequest) Arrays.asList(java.util.Arrays.asList) IOException(java.io.IOException) BaseRestHandler(org.opensearch.rest.BaseRestHandler) Strings(org.opensearch.common.Strings) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest)

Aggregations

IndicesAliasesRequest (org.opensearch.action.admin.indices.alias.IndicesAliasesRequest)9 AliasActions (org.opensearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions)6 IOException (java.io.IOException)3 List (java.util.List)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 GetAliasesRequest (org.opensearch.action.admin.indices.alias.get.GetAliasesRequest)3 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)3 DeleteAliasRequest (org.opensearch.client.indices.DeleteAliasRequest)3 Arrays.asList (java.util.Arrays.asList)2 Collections.unmodifiableList (java.util.Collections.unmodifiableList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 OpenSearchException (org.opensearch.OpenSearchException)2 ClearIndicesCacheRequest (org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest)2 DeleteIndexRequest (org.opensearch.action.admin.indices.delete.DeleteIndexRequest)2 FlushRequest (org.opensearch.action.admin.indices.flush.FlushRequest)2 ForceMergeRequest (org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest)2 OpenIndexRequest (org.opensearch.action.admin.indices.open.OpenIndexRequest)2 RefreshRequest (org.opensearch.action.admin.indices.refresh.RefreshRequest)2 GetSettingsRequest (org.opensearch.action.admin.indices.settings.get.GetSettingsRequest)2