diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java index 3811ba78344..f2cefa98392 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java @@ -59,6 +59,9 @@ import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateReque import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.client.core.ShardsAcknowledgedResponse; +import org.elasticsearch.client.indices.FreezeIndexRequest; +import org.elasticsearch.client.indices.UnfreezeIndexRequest; import org.elasticsearch.rest.RestStatus; import java.io.IOException; @@ -836,4 +839,51 @@ public final class IndicesClient { restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::analyze, options, AnalyzeResponse::fromXContent, listener, emptySet()); } + + /** + * Synchronously calls the _freeze API + * + * @param request the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + */ + public ShardsAcknowledgedResponse freeze(FreezeIndexRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::freezeIndex, options, + ShardsAcknowledgedResponse::fromXContent, emptySet()); + } + + /** + * Asynchronously calls the _freeze API + * + * @param request the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param listener the listener to be notified upon request completion + */ + public void freezeAsync(FreezeIndexRequest request, RequestOptions options, ActionListener listener) { + restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::freezeIndex, options, + ShardsAcknowledgedResponse::fromXContent, listener, emptySet()); + } + + /** + * Synchronously calls the _unfreeze API + * + * @param request the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + */ + public ShardsAcknowledgedResponse unfreeze(UnfreezeIndexRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::unfreezeIndex, options, + ShardsAcknowledgedResponse::fromXContent, emptySet()); + } + + /** + * Asynchronously calls the _unfreeze API + * + * @param request the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param listener the listener to be notified upon request completion + */ + public void unfreezeAsync(UnfreezeIndexRequest request, RequestOptions options, ActionListener listener) { + restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::unfreezeIndex, options, + ShardsAcknowledgedResponse::fromXContent, listener, emptySet()); + } + } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java index ea81c88f8fe..e348305611a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java @@ -48,6 +48,8 @@ import org.elasticsearch.action.admin.indices.shrink.ResizeType; import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest; import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest; +import org.elasticsearch.client.indices.FreezeIndexRequest; +import org.elasticsearch.client.indices.UnfreezeIndexRequest; import org.elasticsearch.common.Strings; import java.io.IOException; @@ -403,4 +405,26 @@ final class IndicesRequestConverters { req.setEntity(RequestConverters.createEntity(request, RequestConverters.REQUEST_BODY_CONTENT_TYPE)); return req; } + + static Request freezeIndex(FreezeIndexRequest freezeIndexRequest) { + String endpoint = RequestConverters.endpoint(freezeIndexRequest.getIndices(), "_freeze"); + Request request = new Request(HttpPost.METHOD_NAME, endpoint); + RequestConverters.Params parameters = new RequestConverters.Params(request); + parameters.withTimeout(freezeIndexRequest.timeout()); + parameters.withMasterTimeout(freezeIndexRequest.masterNodeTimeout()); + parameters.withIndicesOptions(freezeIndexRequest.indicesOptions()); + parameters.withWaitForActiveShards(freezeIndexRequest.getWaitForActiveShards()); + return request; + } + + static Request unfreezeIndex(UnfreezeIndexRequest unfreezeIndexRequest) { + String endpoint = RequestConverters.endpoint(unfreezeIndexRequest.getIndices(), "_unfreeze"); + Request request = new Request(HttpPost.METHOD_NAME, endpoint); + RequestConverters.Params parameters = new RequestConverters.Params(request); + parameters.withTimeout(unfreezeIndexRequest.timeout()); + parameters.withMasterTimeout(unfreezeIndexRequest.masterNodeTimeout()); + parameters.withIndicesOptions(unfreezeIndexRequest.indicesOptions()); + parameters.withWaitForActiveShards(unfreezeIndexRequest.getWaitForActiveShards()); + return request; + } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java index 844a1d4e829..2f2b1f26b53 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java @@ -859,22 +859,24 @@ final class RequestConverters { } Params withIndicesOptions(IndicesOptions indicesOptions) { - withIgnoreUnavailable(indicesOptions.ignoreUnavailable()); - putParam("allow_no_indices", Boolean.toString(indicesOptions.allowNoIndices())); - String expandWildcards; - if (indicesOptions.expandWildcardsOpen() == false && indicesOptions.expandWildcardsClosed() == false) { - expandWildcards = "none"; - } else { - StringJoiner joiner = new StringJoiner(","); - if (indicesOptions.expandWildcardsOpen()) { - joiner.add("open"); + if (indicesOptions != null) { + withIgnoreUnavailable(indicesOptions.ignoreUnavailable()); + putParam("allow_no_indices", Boolean.toString(indicesOptions.allowNoIndices())); + String expandWildcards; + if (indicesOptions.expandWildcardsOpen() == false && indicesOptions.expandWildcardsClosed() == false) { + expandWildcards = "none"; + } else { + StringJoiner joiner = new StringJoiner(","); + if (indicesOptions.expandWildcardsOpen()) { + joiner.add("open"); + } + if (indicesOptions.expandWildcardsClosed()) { + joiner.add("closed"); + } + expandWildcards = joiner.toString(); } - if (indicesOptions.expandWildcardsClosed()) { - joiner.add("closed"); - } - expandWildcards = joiner.toString(); + putParam("expand_wildcards", expandWildcards); } - putParam("expand_wildcards", expandWildcards); return this; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/ShardsAcknowledgedResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/ShardsAcknowledgedResponse.java new file mode 100644 index 00000000000..50d9f46b760 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/ShardsAcknowledgedResponse.java @@ -0,0 +1,57 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.client.core; + +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.io.IOException; + +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; + +public class ShardsAcknowledgedResponse extends AcknowledgedResponse { + + protected static final String SHARDS_PARSE_FIELD_NAME = "shards_acknowledged"; + private static ConstructingObjectParser buildParser() { + + ConstructingObjectParser p = new ConstructingObjectParser<>("freeze", true, + args -> new ShardsAcknowledgedResponse((boolean) args[0], (boolean) args[1])); + p.declareBoolean(constructorArg(), new ParseField(AcknowledgedResponse.PARSE_FIELD_NAME)); + p.declareBoolean(constructorArg(), new ParseField(SHARDS_PARSE_FIELD_NAME)); + return p; + } + + private static final ConstructingObjectParser PARSER = buildParser(); + + private final boolean shardsAcknowledged; + + public ShardsAcknowledgedResponse(boolean acknowledged, boolean shardsAcknowledged) { + super(acknowledged); + this.shardsAcknowledged = shardsAcknowledged; + } + + public boolean isShardsAcknowledged() { + return shardsAcknowledged; + } + + public static ShardsAcknowledgedResponse fromXContent(XContentParser parser) throws IOException { + return PARSER.parse(parser, null); + } +} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/FreezeIndexRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/FreezeIndexRequest.java new file mode 100644 index 00000000000..d78f2d533ab --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/FreezeIndexRequest.java @@ -0,0 +1,96 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.client.indices; + +import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; +import org.elasticsearch.action.support.ActiveShardCount; +import org.elasticsearch.action.support.IndicesOptions; +import org.elasticsearch.client.TimedRequest; + +import java.util.Objects; + +/** + * Request for the _freeze index API + */ +public final class FreezeIndexRequest extends TimedRequest { + + private final String[] indices; + private IndicesOptions indicesOptions; + private ActiveShardCount waitForActiveShards; + + /** + * Creates a new freeze index request + * @param indices the index to freeze + */ + public FreezeIndexRequest(String... indices) { + this.indices = Objects.requireNonNull(indices); + } + + /** + * Returns the indices to freeze + */ + public String[] getIndices() { + return indices; + } + + /** + * Specifies what type of requested indices to ignore and how to deal with wildcard expressions. + * For example indices that don't exist. + * + * @return the current behaviour when it comes to index names and wildcard indices expressions + */ + public IndicesOptions indicesOptions() { + return indicesOptions; + } + + /** + * Specifies what type of requested indices to ignore and how to deal with wildcard expressions. + * For example indices that don't exist. + * + * @param indicesOptions the desired behaviour regarding indices to ignore and wildcard indices expressions + */ + public void setIndicesOptions(IndicesOptions indicesOptions) { + this.indicesOptions = indicesOptions; + } + + /** + * Returns the wait for active shard cound or null if the default should be used + */ + public ActiveShardCount getWaitForActiveShards() { + return waitForActiveShards; + } + + /** + * Sets the number of shard copies that should be active for indices opening to return. + * Defaults to {@link ActiveShardCount#DEFAULT}, which will wait for one shard copy + * (the primary) to become active. Set this value to {@link ActiveShardCount#ALL} to + * wait for all shards (primary and all replicas) to be active before returning. + * Otherwise, use {@link ActiveShardCount#from(int)} to set this value to any + * non-negative integer, up to the number of copies per shard (number of replicas + 1), + * to wait for the desired amount of shard copies to become active before returning. + * Indices opening will only wait up until the timeout value for the number of shard copies + * to be active before returning. Check {@link OpenIndexResponse#isShardsAcknowledged()} to + * determine if the requisite shard copies were all started before returning or timing out. + * + * @param waitForActiveShards number of active shard copies to wait on + */ + public void setWaitForActiveShards(ActiveShardCount waitForActiveShards) { + this.waitForActiveShards = waitForActiveShards; + } +} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/UnfreezeIndexRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/UnfreezeIndexRequest.java new file mode 100644 index 00000000000..1e2d7cded71 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/UnfreezeIndexRequest.java @@ -0,0 +1,97 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.client.indices; + +import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; +import org.elasticsearch.action.support.ActiveShardCount; +import org.elasticsearch.action.support.IndicesOptions; +import org.elasticsearch.client.TimedRequest; + +import java.util.Objects; + +/** + * Request for the _unfreeze index API + */ +public final class UnfreezeIndexRequest extends TimedRequest { + + private final String[] indices; + private IndicesOptions indicesOptions; + private ActiveShardCount waitForActiveShards; + + /** + * Creates a new unfreeze index request + * @param indices the index to unfreeze + */ + public UnfreezeIndexRequest(String... indices) { + this.indices = Objects.requireNonNull(indices); + } + + /** + * Returns the indices to unfreeze + */ + public String[] getIndices() { + return indices; + } + + /** + * Specifies what type of requested indices to ignore and how to deal with wildcard expressions. + * For example indices that don't exist. + * + * @return the current behaviour when it comes to index names and wildcard indices expressions + */ + public IndicesOptions indicesOptions() { + return indicesOptions; + } + + /** + * Specifies what type of requested indices to ignore and how to deal with wildcard expressions. + * For example indices that don't exist. + * + * @param indicesOptions the desired behaviour regarding indices to ignore and wildcard indices expressions + */ + public void setIndicesOptions(IndicesOptions indicesOptions) { + this.indicesOptions = indicesOptions; + } + + /** + * Returns the wait for active shard cound or null if the default should be used + */ + public ActiveShardCount getWaitForActiveShards() { + return waitForActiveShards; + } + + /** + * Sets the number of shard copies that should be active for indices opening to return. + * Defaults to {@link ActiveShardCount#DEFAULT}, which will wait for one shard copy + * (the primary) to become active. Set this value to {@link ActiveShardCount#ALL} to + * wait for all shards (primary and all replicas) to be active before returning. + * Otherwise, use {@link ActiveShardCount#from(int)} to set this value to any + * non-negative integer, up to the number of copies per shard (number of replicas + 1), + * to wait for the desired amount of shard copies to become active before returning. + * Indices opening will only wait up until the timeout value for the number of shard copies + * to be active before returning. Check {@link OpenIndexResponse#isShardsAcknowledged()} to + * determine if the requisite shard copies were all started before returning or timing out. + * + * @param waitForActiveShards number of active shard copies to wait on + */ + public void setWaitForActiveShards(ActiveShardCount waitForActiveShards) { + this.waitForActiveShards = waitForActiveShards; + } + +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java index 053f46f8496..d04c9b0e824 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java @@ -70,6 +70,9 @@ import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.action.support.broadcast.BroadcastResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.client.core.ShardsAcknowledgedResponse; +import org.elasticsearch.client.indices.FreezeIndexRequest; +import org.elasticsearch.client.indices.UnfreezeIndexRequest; import org.elasticsearch.cluster.metadata.AliasMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexTemplateMetaData; @@ -1370,6 +1373,20 @@ public class IndicesClientIT extends ESRestHighLevelClientTestCase { AnalyzeResponse detailsResponse = execute(detailsRequest, client.indices()::analyze, client.indices()::analyzeAsync); assertNotNull(detailsResponse.detail()); + } + public void testFreezeAndUnfreeze() throws IOException { + createIndex("test", Settings.EMPTY); + RestHighLevelClient client = highLevelClient(); + + ShardsAcknowledgedResponse freeze = execute(new FreezeIndexRequest("test"), client.indices()::freeze, + client.indices()::freezeAsync); + assertTrue(freeze.isShardsAcknowledged()); + assertTrue(freeze.isAcknowledged()); + + ShardsAcknowledgedResponse unfreeze = execute(new UnfreezeIndexRequest("test"), client.indices()::unfreeze, + client.indices()::unfreezeAsync); + assertTrue(unfreeze.isShardsAcknowledged()); + assertTrue(unfreeze.isAcknowledged()); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java index 56788e2eb76..119a50695ec 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java @@ -751,7 +751,8 @@ public class RestHighLevelClientTests extends ESTestCase { apiName.startsWith("migration.") == false && apiName.startsWith("security.") == false && apiName.startsWith("index_lifecycle.") == false && - apiName.startsWith("ccr.") == false) { + apiName.startsWith("ccr.") == false && + apiName.endsWith("freeze") == false) { apiNotFound.add(apiName); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/ShardsAcknowledgedResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/ShardsAcknowledgedResponseTests.java new file mode 100644 index 00000000000..1923ebd6bda --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/ShardsAcknowledgedResponseTests.java @@ -0,0 +1,52 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.client.core; + +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.test.ESTestCase; + +import java.io.IOException; + +import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester; + +public class ShardsAcknowledgedResponseTests extends ESTestCase { + + public void testFromXContent() throws IOException { + xContentTester(this::createParser, + this::createTestInstance, + ShardsAcknowledgedResponseTests::toXContent, + ShardsAcknowledgedResponse::fromXContent) + .supportsUnknownFields(false) + .test(); + } + + private ShardsAcknowledgedResponse createTestInstance() { + return new ShardsAcknowledgedResponse(randomBoolean(), randomBoolean()); + } + + public static void toXContent(ShardsAcknowledgedResponse response, XContentBuilder builder) throws IOException { + builder.startObject(); + { + builder.field(response.getFieldName(), response.isAcknowledged()); + builder.field(ShardsAcknowledgedResponse.SHARDS_PARSE_FIELD_NAME, response.isShardsAcknowledged()); + } + builder.endObject(); + } + +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java index c53b32ccca1..1fa177f8e03 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java @@ -70,10 +70,13 @@ import org.elasticsearch.action.support.DefaultShardOperationFailedException; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.ESRestHighLevelClientTestCase; +import org.elasticsearch.client.indices.FreezeIndexRequest; import org.elasticsearch.client.GetAliasesResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.SyncedFlushResponse; +import org.elasticsearch.client.indices.UnfreezeIndexRequest; +import org.elasticsearch.client.core.ShardsAcknowledgedResponse; import org.elasticsearch.cluster.metadata.AliasMetaData; import org.elasticsearch.cluster.metadata.IndexTemplateMetaData; import org.elasticsearch.cluster.metadata.MappingMetaData; @@ -2527,4 +2530,160 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase } } + + public void testFreezeIndex() throws Exception { + RestHighLevelClient client = highLevelClient(); + + { + CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index"), RequestOptions.DEFAULT); + assertTrue(createIndexResponse.isAcknowledged()); + } + + { + // tag::freeze-index-request + FreezeIndexRequest request = new FreezeIndexRequest("index"); // <1> + // end::freeze-index-request + + // tag::freeze-index-request-timeout + request.setTimeout(TimeValue.timeValueMinutes(2)); // <1> + // end::freeze-index-request-timeout + // tag::freeze-index-request-masterTimeout + request.setMasterTimeout(TimeValue.timeValueMinutes(1)); // <1> + // end::freeze-index-request-masterTimeout + // tag::freeze-index-request-waitForActiveShards + request.setWaitForActiveShards(ActiveShardCount.DEFAULT); // <1> + // end::freeze-index-request-waitForActiveShards + + // tag::freeze-index-request-indicesOptions + request.setIndicesOptions(IndicesOptions.strictExpandOpen()); // <1> + // end::freeze-index-request-indicesOptions + + // tag::freeze-index-execute + ShardsAcknowledgedResponse openIndexResponse = client.indices().freeze(request, RequestOptions.DEFAULT); + // end::freeze-index-execute + + // tag::freeze-index-response + boolean acknowledged = openIndexResponse.isAcknowledged(); // <1> + boolean shardsAcked = openIndexResponse.isShardsAcknowledged(); // <2> + // end::freeze-index-response + assertTrue(acknowledged); + assertTrue(shardsAcked); + + // tag::freeze-index-execute-listener + ActionListener listener = + new ActionListener() { + @Override + public void onResponse(ShardsAcknowledgedResponse freezeIndexResponse) { + // <1> + } + + @Override + public void onFailure(Exception e) { + // <2> + } + }; + // end::freeze-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::freeze-index-execute-async + client.indices().freezeAsync(request, RequestOptions.DEFAULT, listener); // <1> + // end::freeze-index-execute-async + + assertTrue(latch.await(30L, TimeUnit.SECONDS)); + } + + { + // tag::freeze-index-notfound + try { + FreezeIndexRequest request = new FreezeIndexRequest("does_not_exist"); + client.indices().freeze(request, RequestOptions.DEFAULT); + } catch (ElasticsearchException exception) { + if (exception.status() == RestStatus.BAD_REQUEST) { + // <1> + } + } + // end::freeze-index-notfound + } + } + + public void testUnfreezeIndex() throws Exception { + RestHighLevelClient client = highLevelClient(); + + { + CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index"), RequestOptions.DEFAULT); + assertTrue(createIndexResponse.isAcknowledged()); + } + + { + // tag::unfreeze-index-request + UnfreezeIndexRequest request = new UnfreezeIndexRequest("index"); // <1> + // end::unfreeze-index-request + + // tag::unfreeze-index-request-timeout + request.setTimeout(TimeValue.timeValueMinutes(2)); // <1> + // end::unfreeze-index-request-timeout + // tag::unfreeze-index-request-masterTimeout + request.setMasterTimeout(TimeValue.timeValueMinutes(1)); // <1> + // end::unfreeze-index-request-masterTimeout + // tag::unfreeze-index-request-waitForActiveShards + request.setWaitForActiveShards(ActiveShardCount.DEFAULT); // <1> + // end::unfreeze-index-request-waitForActiveShards + + // tag::unfreeze-index-request-indicesOptions + request.setIndicesOptions(IndicesOptions.strictExpandOpen()); // <1> + // end::unfreeze-index-request-indicesOptions + + // tag::unfreeze-index-execute + ShardsAcknowledgedResponse openIndexResponse = client.indices().unfreeze(request, RequestOptions.DEFAULT); + // end::unfreeze-index-execute + + // tag::unfreeze-index-response + boolean acknowledged = openIndexResponse.isAcknowledged(); // <1> + boolean shardsAcked = openIndexResponse.isShardsAcknowledged(); // <2> + // end::unfreeze-index-response + assertTrue(acknowledged); + assertTrue(shardsAcked); + + // tag::unfreeze-index-execute-listener + ActionListener listener = + new ActionListener() { + @Override + public void onResponse(ShardsAcknowledgedResponse freezeIndexResponse) { + // <1> + } + + @Override + public void onFailure(Exception e) { + // <2> + } + }; + // end::unfreeze-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::unfreeze-index-execute-async + client.indices().unfreezeAsync(request, RequestOptions.DEFAULT, listener); // <1> + // end::unfreeze-index-execute-async + + assertTrue(latch.await(30L, TimeUnit.SECONDS)); + } + + { + // tag::unfreeze-index-notfound + try { + UnfreezeIndexRequest request = new UnfreezeIndexRequest("does_not_exist"); + client.indices().unfreeze(request, RequestOptions.DEFAULT); + } catch (ElasticsearchException exception) { + if (exception.status() == RestStatus.BAD_REQUEST) { + // <1> + } + } + // end::unfreeze-index-notfound + } + } } diff --git a/docs/java-rest/high-level/indices/freeze_index.asciidoc b/docs/java-rest/high-level/indices/freeze_index.asciidoc new file mode 100644 index 00000000000..2a26fe8bcd4 --- /dev/null +++ b/docs/java-rest/high-level/indices/freeze_index.asciidoc @@ -0,0 +1,65 @@ +-- +:api: freeze-index +:request: FreezeIndexRequest +:response: FreezeIndexResponse +-- + +[id="{upid}-{api}"] +=== Freeze Index API + +[id="{upid}-{api}-request"] +==== Freeze Index Request + +An +{request}+ requires an `index` argument: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-request] +-------------------------------------------------- +<1> The index to freeze + +==== Optional arguments +The following arguments can optionally be provided: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-request-timeout] +-------------------------------------------------- +<1> Timeout to wait for the all the nodes to acknowledge the index is frozen +as a `TimeValue` + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-request-masterTimeout] +-------------------------------------------------- +<1> Timeout to connect to the master node as a `TimeValue` + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-request-waitForActiveShards] +-------------------------------------------------- +<1> The number of active shard copies to wait for before the freeze index API +returns a response, as an `ActiveShardCount` + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-request-indicesOptions] +-------------------------------------------------- +<1> Setting `IndicesOptions` controls how unavailable indices are resolved and +how wildcard expressions are expanded + +include::../execution.asciidoc[] + +[id="{upid}-{api}-response"] +==== Freeze Index Response + +The returned +{response}+ allows to retrieve information about the +executed operation as follows: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-response] +-------------------------------------------------- +<1> Indicates whether all of the nodes have acknowledged the request +<2> Indicates whether the requisite number of shard copies were started for +each shard in the index before timing out diff --git a/docs/java-rest/high-level/indices/unfreeze_index.asciidoc b/docs/java-rest/high-level/indices/unfreeze_index.asciidoc new file mode 100644 index 00000000000..466a62a7f6b --- /dev/null +++ b/docs/java-rest/high-level/indices/unfreeze_index.asciidoc @@ -0,0 +1,64 @@ +-- +:api: freeze-index +:request: UnfreezeIndexRequest +:response: UnfreezeIndexResponse +-- +[id="{upid}-{api}"] +=== Unfreeze Index API + +[id="{upid}-{api}-request"] +==== Unfreeze Index Request + +An +{request}+ requires an `index` argument: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-request] +-------------------------------------------------- +<1> The index to unreeze + +==== Optional arguments +The following arguments can optionally be provided: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-request-timeout] +-------------------------------------------------- +<1> Timeout to wait for the all the nodes to acknowledge the index is frozen +as a `TimeValue` + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-request-masterTimeout] +-------------------------------------------------- +<1> Timeout to connect to the master node as a `TimeValue` + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-request-waitForActiveShards] +-------------------------------------------------- +<1> The number of active shard copies to wait for before the unfreeze index API +returns a response, as an `ActiveShardCount` + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-request-indicesOptions] +-------------------------------------------------- +<1> Setting `IndicesOptions` controls how unavailable indices are resolved and +how wildcard expressions are expanded + +include::../execution.asciidoc[] + +[id="{upid}-{api}-response"] +==== Unfreeze Index Response + +The returned +{response}+ allows to retrieve information about the +executed operation as follows: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-response] +-------------------------------------------------- +<1> Indicates whether all of the nodes have acknowledged the request +<2> Indicates whether the requisite number of shard copies were started for +each shard in the index before timing out diff --git a/docs/java-rest/high-level/supported-apis.asciidoc b/docs/java-rest/high-level/supported-apis.asciidoc index 109dc94ac03..096a9e59382 100644 --- a/docs/java-rest/high-level/supported-apis.asciidoc +++ b/docs/java-rest/high-level/supported-apis.asciidoc @@ -146,6 +146,8 @@ include::indices/put_template.asciidoc[] include::indices/validate_query.asciidoc[] include::indices/get_templates.asciidoc[] include::indices/get_index.asciidoc[] +include::indices/freeze_index.asciidoc[] +include::indices/unfreeze_index.asciidoc[] == Cluster APIs