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