Search in sources :

Example 1 with RolloverResponse

use of org.opensearch.client.indices.rollover.RolloverResponse 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 RolloverResponse

use of org.opensearch.client.indices.rollover.RolloverResponse 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)

Aggregations

Alias (org.opensearch.action.admin.indices.alias.Alias)2 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)2 RolloverRequest (org.opensearch.client.indices.rollover.RolloverRequest)2 RolloverResponse (org.opensearch.client.indices.rollover.RolloverResponse)2 ByteSizeValue (org.opensearch.common.unit.ByteSizeValue)2 TimeValue (org.opensearch.common.unit.TimeValue)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 OpenSearchException (org.opensearch.OpenSearchException)1 ActionListener (org.opensearch.action.ActionListener)1 LatchedActionListener (org.opensearch.action.LatchedActionListener)1 DeleteIndexRequest (org.opensearch.action.admin.indices.delete.DeleteIndexRequest)1 OpenIndexRequest (org.opensearch.action.admin.indices.open.OpenIndexRequest)1 IndexRequest (org.opensearch.action.index.IndexRequest)1 DefaultShardOperationFailedException (org.opensearch.action.support.DefaultShardOperationFailedException)1 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)1 CloseIndexRequest (org.opensearch.client.indices.CloseIndexRequest)1