Search in sources :

Example 1 with RolloverRequest

use of org.opensearch.client.indices.rollover.RolloverRequest in project OpenSearch by opensearch-project.

the class IndicesClientIT method testRollover.

public void testRollover() throws IOException {
    highLevelClient().indices().create(new CreateIndexRequest("test").alias(new Alias("alias")), RequestOptions.DEFAULT);
    RolloverRequest rolloverRequest = new RolloverRequest("alias", "test_new");
    rolloverRequest.addMaxIndexDocsCondition(1);
    {
        RolloverResponse rolloverResponse = execute(rolloverRequest, highLevelClient().indices()::rollover, highLevelClient().indices()::rolloverAsync);
        assertFalse(rolloverResponse.isRolledOver());
        assertFalse(rolloverResponse.isDryRun());
        Map<String, Boolean> conditionStatus = rolloverResponse.getConditionStatus();
        assertEquals(1, conditionStatus.size());
        assertFalse(conditionStatus.get("[max_docs: 1]"));
        assertEquals("test", rolloverResponse.getOldIndex());
        assertEquals("test_new", rolloverResponse.getNewIndex());
    }
    highLevelClient().index(new IndexRequest("test").id("1").source("field", "value"), RequestOptions.DEFAULT);
    highLevelClient().index(new IndexRequest("test").id("2").source("field", "value").setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL), RequestOptions.DEFAULT);
    // without the refresh the rollover may not happen as the number of docs seen may be off
    {
        rolloverRequest.addMaxIndexAgeCondition(new TimeValue(1));
        rolloverRequest.dryRun(true);
        RolloverResponse rolloverResponse = execute(rolloverRequest, highLevelClient().indices()::rollover, highLevelClient().indices()::rolloverAsync);
        assertFalse(rolloverResponse.isRolledOver());
        assertTrue(rolloverResponse.isDryRun());
        Map<String, Boolean> conditionStatus = rolloverResponse.getConditionStatus();
        assertEquals(2, conditionStatus.size());
        assertTrue(conditionStatus.get("[max_docs: 1]"));
        assertTrue(conditionStatus.get("[max_age: 1ms]"));
        assertEquals("test", rolloverResponse.getOldIndex());
        assertEquals("test_new", rolloverResponse.getNewIndex());
    }
    {
        String mappings = "{\"properties\":{\"field2\":{\"type\":\"keyword\"}}}";
        rolloverRequest.getCreateIndexRequest().mapping(mappings, XContentType.JSON);
        rolloverRequest.dryRun(false);
        rolloverRequest.addMaxIndexSizeCondition(new ByteSizeValue(1, ByteSizeUnit.MB));
        RolloverResponse rolloverResponse = execute(rolloverRequest, highLevelClient().indices()::rollover, highLevelClient().indices()::rolloverAsync);
        assertTrue(rolloverResponse.isRolledOver());
        assertFalse(rolloverResponse.isDryRun());
        Map<String, Boolean> conditionStatus = rolloverResponse.getConditionStatus();
        assertEquals(3, conditionStatus.size());
        assertTrue(conditionStatus.get("[max_docs: 1]"));
        assertTrue(conditionStatus.get("[max_age: 1ms]"));
        assertFalse(conditionStatus.get("[max_size: 1mb]"));
        assertEquals("test", rolloverResponse.getOldIndex());
        assertEquals("test_new", rolloverResponse.getNewIndex());
    }
}
Also used : RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest) Alias(org.opensearch.action.admin.indices.alias.Alias) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) RolloverResponse(org.opensearch.client.indices.rollover.RolloverResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) CloseIndexRequest(org.opensearch.client.indices.CloseIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) Map(java.util.Map) HashMap(java.util.HashMap) TimeValue(org.opensearch.common.unit.TimeValue)

Example 2 with RolloverRequest

use of org.opensearch.client.indices.rollover.RolloverRequest in project OpenSearch by opensearch-project.

the class IndicesRequestConvertersTests method testRollover.

public void testRollover() throws IOException {
    RolloverRequest rolloverRequest = new RolloverRequest(OpenSearchTestCase.randomAlphaOfLengthBetween(3, 10), OpenSearchTestCase.randomBoolean() ? null : OpenSearchTestCase.randomAlphaOfLengthBetween(3, 10));
    Map<String, String> expectedParams = new HashMap<>();
    RequestConvertersTests.setRandomTimeout(rolloverRequest, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
    RequestConvertersTests.setRandomMasterTimeout(rolloverRequest, expectedParams);
    if (OpenSearchTestCase.randomBoolean()) {
        rolloverRequest.dryRun(OpenSearchTestCase.randomBoolean());
        if (rolloverRequest.isDryRun()) {
            expectedParams.put("dry_run", "true");
        }
    }
    if (OpenSearchTestCase.randomBoolean()) {
        rolloverRequest.addMaxIndexAgeCondition(new TimeValue(OpenSearchTestCase.randomNonNegativeLong()));
    }
    if (OpenSearchTestCase.randomBoolean()) {
        rolloverRequest.getCreateIndexRequest().mapping(randomMapping());
    }
    if (OpenSearchTestCase.randomBoolean()) {
        randomAliases(rolloverRequest.getCreateIndexRequest());
    }
    if (OpenSearchTestCase.randomBoolean()) {
        rolloverRequest.getCreateIndexRequest().settings(org.opensearch.index.RandomCreateIndexGenerator.randomIndexSettings());
    }
    RequestConvertersTests.setRandomWaitForActiveShards(rolloverRequest.getCreateIndexRequest()::waitForActiveShards, expectedParams);
    Request request = IndicesRequestConverters.rollover(rolloverRequest);
    if (rolloverRequest.getNewIndexName() == null) {
        Assert.assertEquals("/" + rolloverRequest.getAlias() + "/_rollover", request.getEndpoint());
    } else {
        Assert.assertEquals("/" + rolloverRequest.getAlias() + "/_rollover/" + rolloverRequest.getNewIndexName(), request.getEndpoint());
    }
    Assert.assertEquals(HttpPost.METHOD_NAME, request.getMethod());
    RequestConvertersTests.assertToXContentBody(rolloverRequest, request.getEntity());
    Assert.assertEquals(expectedParams, request.getParameters());
}
Also used : RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest) 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) TimeValue(org.opensearch.common.unit.TimeValue)

Example 3 with RolloverRequest

use of org.opensearch.client.indices.rollover.RolloverRequest in project OpenSearch by opensearch-project.

the class IndicesClientDocumentationIT method testRolloverIndex.

public void testRolloverIndex() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        client.indices().create(new CreateIndexRequest("index-1").alias(new Alias("alias")), RequestOptions.DEFAULT);
    }
    // tag::rollover-index-request
    // <1>
    RolloverRequest request = new RolloverRequest("alias", "index-2");
    // <2>
    request.addMaxIndexAgeCondition(new TimeValue(7, TimeUnit.DAYS));
    // <3>
    request.addMaxIndexDocsCondition(1000);
    // <4>
    request.addMaxIndexSizeCondition(new ByteSizeValue(5, ByteSizeUnit.GB));
    // end::rollover-index-request
    // tag::rollover-index-request-timeout
    // <1>
    request.setTimeout(TimeValue.timeValueMinutes(2));
    // end::rollover-index-request-timeout
    // tag::rollover-index-request-masterTimeout
    // <1>
    request.setMasterTimeout(TimeValue.timeValueMinutes(1));
    // end::rollover-index-request-masterTimeout
    // tag::rollover-index-request-dryRun
    // <1>
    request.dryRun(true);
    // end::rollover-index-request-dryRun
    // tag::rollover-index-request-waitForActiveShards
    // <1>
    request.getCreateIndexRequest().waitForActiveShards(ActiveShardCount.from(2));
    // <2>
    request.getCreateIndexRequest().waitForActiveShards(ActiveShardCount.DEFAULT);
    // end::rollover-index-request-waitForActiveShards
    // tag::rollover-index-request-settings
    request.getCreateIndexRequest().settings(Settings.builder().put("index.number_of_shards", // <1>
    4));
    // end::rollover-index-request-settings
    // tag::rollover-index-request-mapping
    String mappings = "{\"properties\":{\"field-1\":{\"type\":\"keyword\"}}}";
    // <1>
    request.getCreateIndexRequest().mapping(mappings, XContentType.JSON);
    // end::rollover-index-request-mapping
    // tag::rollover-index-request-alias
    // <1>
    request.getCreateIndexRequest().alias(new Alias("another_alias"));
    // end::rollover-index-request-alias
    // tag::rollover-index-execute
    RolloverResponse rolloverResponse = client.indices().rollover(request, RequestOptions.DEFAULT);
    // end::rollover-index-execute
    // tag::rollover-index-response
    // <1>
    boolean acknowledged = rolloverResponse.isAcknowledged();
    // <2>
    boolean shardsAcked = rolloverResponse.isShardsAcknowledged();
    // <3>
    String oldIndex = rolloverResponse.getOldIndex();
    // <4>
    String newIndex = rolloverResponse.getNewIndex();
    // <5>
    boolean isRolledOver = rolloverResponse.isRolledOver();
    // <6>
    boolean isDryRun = rolloverResponse.isDryRun();
    // <7>
    Map<String, Boolean> conditionStatus = rolloverResponse.getConditionStatus();
    // end::rollover-index-response
    assertFalse(acknowledged);
    assertFalse(shardsAcked);
    assertEquals("index-1", oldIndex);
    assertEquals("index-2", newIndex);
    assertFalse(isRolledOver);
    assertTrue(isDryRun);
    assertEquals(3, conditionStatus.size());
    // tag::rollover-index-execute-listener
    ActionListener<RolloverResponse> listener = new ActionListener<RolloverResponse>() {

        @Override
        public void onResponse(RolloverResponse rolloverResponse) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::rollover-index-execute-listener
    // Replace the empty listener by a blocking listener in test
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::rollover-index-execute-async
    // <1>
    client.indices().rolloverAsync(request, RequestOptions.DEFAULT, listener);
    // end::rollover-index-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) Alias(org.opensearch.action.admin.indices.alias.Alias) RolloverResponse(org.opensearch.client.indices.rollover.RolloverResponse) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) TimeValue(org.opensearch.common.unit.TimeValue)

Example 4 with RolloverRequest

use of org.opensearch.client.indices.rollover.RolloverRequest in project OpenSearch by opensearch-project.

the class IndicesRequestConverters method rollover.

static Request rollover(RolloverRequest rolloverRequest) throws IOException {
    String endpoint = new RequestConverters.EndpointBuilder().addPathPart(rolloverRequest.getAlias()).addPathPartAsIs("_rollover").addPathPart(rolloverRequest.getNewIndexName()).build();
    Request request = new Request(HttpPost.METHOD_NAME, endpoint);
    RequestConverters.Params params = new RequestConverters.Params();
    params.withTimeout(rolloverRequest.timeout());
    params.withMasterTimeout(rolloverRequest.masterNodeTimeout());
    params.withWaitForActiveShards(rolloverRequest.getCreateIndexRequest().waitForActiveShards());
    if (rolloverRequest.isDryRun()) {
        params.putParam("dry_run", Boolean.TRUE.toString());
    }
    request.addParameters(params.asMap());
    request.setEntity(RequestConverters.createEntity(rolloverRequest, 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)

Aggregations

CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)4 RolloverRequest (org.opensearch.client.indices.rollover.RolloverRequest)4 DeleteIndexRequest (org.opensearch.action.admin.indices.delete.DeleteIndexRequest)3 OpenIndexRequest (org.opensearch.action.admin.indices.open.OpenIndexRequest)3 CloseIndexRequest (org.opensearch.client.indices.CloseIndexRequest)3 GetIndexRequest (org.opensearch.client.indices.GetIndexRequest)3 TimeValue (org.opensearch.common.unit.TimeValue)3 HashMap (java.util.HashMap)2 Alias (org.opensearch.action.admin.indices.alias.Alias)2 IndicesAliasesRequest (org.opensearch.action.admin.indices.alias.IndicesAliasesRequest)2 GetAliasesRequest (org.opensearch.action.admin.indices.alias.get.GetAliasesRequest)2 ClearIndicesCacheRequest (org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest)2 FlushRequest (org.opensearch.action.admin.indices.flush.FlushRequest)2 ForceMergeRequest (org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest)2 RefreshRequest (org.opensearch.action.admin.indices.refresh.RefreshRequest)2 GetSettingsRequest (org.opensearch.action.admin.indices.settings.get.GetSettingsRequest)2 UpdateSettingsRequest (org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest)2 DeleteIndexTemplateRequest (org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)2 ValidateQueryRequest (org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest)2 AnalyzeRequest (org.opensearch.client.indices.AnalyzeRequest)2