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());
}
}
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));
}
Aggregations