Skip to content

Commit d860971

Browse files
authored
REST high-level client: add support for split and shrink index API (#28425)
Relates to #27205
1 parent d37c59d commit d860971

File tree

26 files changed

+996
-251
lines changed

26 files changed

+996
-251
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121

2222
import org.apache.http.Header;
2323
import org.elasticsearch.action.ActionListener;
24+
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
25+
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse;
2426
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
2527
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
2628
import org.elasticsearch.action.admin.indices.close.CloseIndexResponse;
27-
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
28-
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse;
2929
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
3030
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
3131
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
@@ -34,6 +34,8 @@
3434
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
3535
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
3636
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
37+
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
38+
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
3739

3840
import java.io.IOException;
3941

@@ -208,4 +210,48 @@ public void existsAliasAsync(GetAliasesRequest getAliasesRequest, ActionListener
208210
restHighLevelClient.performRequestAsync(getAliasesRequest, Request::existsAlias, RestHighLevelClient::convertExistsResponse,
209211
listener, emptySet(), headers);
210212
}
213+
214+
/**
215+
* Shrinks an index using the Shrink Index API
216+
* <p>
217+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-shrink-index.html">
218+
* Shrink Index API on elastic.co</a>
219+
*/
220+
public ResizeResponse shrink(ResizeRequest resizeRequest, Header... headers) throws IOException {
221+
return restHighLevelClient.performRequestAndParseEntity(resizeRequest, Request::shrink, ResizeResponse::fromXContent,
222+
emptySet(), headers);
223+
}
224+
225+
/**
226+
* Asynchronously shrinks an index using the Shrink index API
227+
* <p>
228+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-shrink-index.html">
229+
* Shrink Index API on elastic.co</a>
230+
*/
231+
public void shrinkAsync(ResizeRequest resizeRequest, ActionListener<ResizeResponse> listener, Header... headers) {
232+
restHighLevelClient.performRequestAsyncAndParseEntity(resizeRequest, Request::shrink, ResizeResponse::fromXContent,
233+
listener, emptySet(), headers);
234+
}
235+
236+
/**
237+
* Splits an index using the Split Index API
238+
* <p>
239+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-split-index.html">
240+
* Shrink Index API on elastic.co</a>
241+
*/
242+
public ResizeResponse split(ResizeRequest resizeRequest, Header... headers) throws IOException {
243+
return restHighLevelClient.performRequestAndParseEntity(resizeRequest, Request::split, ResizeResponse::fromXContent,
244+
emptySet(), headers);
245+
}
246+
247+
/**
248+
* Asynchronously splits an index using the Split Index API
249+
* <p>
250+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-split-index.html">
251+
* Split Index API on elastic.co</a>
252+
*/
253+
public void splitAsync(ResizeRequest resizeRequest, ActionListener<ResizeResponse> listener, Header... headers) {
254+
restHighLevelClient.performRequestAsyncAndParseEntity(resizeRequest, Request::split, ResizeResponse::fromXContent,
255+
listener, emptySet(), headers);
256+
}
211257
}

client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
3737
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
3838
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
39+
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
40+
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
3941
import org.elasticsearch.action.bulk.BulkRequest;
4042
import org.elasticsearch.action.delete.DeleteRequest;
4143
import org.elasticsearch.action.get.GetRequest;
@@ -182,12 +184,12 @@ static Request createIndex(CreateIndexRequest createIndexRequest) throws IOExcep
182184
HttpEntity entity = createEntity(createIndexRequest, REQUEST_BODY_CONTENT_TYPE);
183185
return new Request(HttpPut.METHOD_NAME, endpoint, parameters.getParams(), entity);
184186
}
185-
187+
186188
static Request updateAliases(IndicesAliasesRequest indicesAliasesRequest) throws IOException {
187189
Params parameters = Params.builder();
188190
parameters.withTimeout(indicesAliasesRequest.timeout());
189191
parameters.withMasterTimeout(indicesAliasesRequest.masterNodeTimeout());
190-
192+
191193
HttpEntity entity = createEntity(indicesAliasesRequest, REQUEST_BODY_CONTENT_TYPE);
192194
return new Request(HttpPost.METHOD_NAME, "/_aliases", parameters.getParams(), entity);
193195
}
@@ -496,7 +498,31 @@ static Request rankEval(RankEvalRequest rankEvalRequest) throws IOException {
496498
HttpEntity entity = null;
497499
entity = createEntity(rankEvalRequest.getRankEvalSpec(), REQUEST_BODY_CONTENT_TYPE);
498500
return new Request(HttpGet.METHOD_NAME, endpoint, Collections.emptyMap(), entity);
501+
}
502+
503+
static Request split(ResizeRequest resizeRequest) throws IOException {
504+
if (resizeRequest.getResizeType() != ResizeType.SPLIT) {
505+
throw new IllegalArgumentException("Wrong resize type [" + resizeRequest.getResizeType() + "] for indices split request");
506+
}
507+
return resize(resizeRequest);
508+
}
499509

510+
static Request shrink(ResizeRequest resizeRequest) throws IOException {
511+
if (resizeRequest.getResizeType() != ResizeType.SHRINK) {
512+
throw new IllegalArgumentException("Wrong resize type [" + resizeRequest.getResizeType() + "] for indices shrink request");
513+
}
514+
return resize(resizeRequest);
515+
}
516+
517+
private static Request resize(ResizeRequest resizeRequest) throws IOException {
518+
Params params = Params.builder();
519+
params.withTimeout(resizeRequest.timeout());
520+
params.withMasterTimeout(resizeRequest.masterNodeTimeout());
521+
params.withWaitForActiveShards(resizeRequest.getTargetIndexRequest().waitForActiveShards());
522+
String endpoint = buildEndpoint(resizeRequest.getSourceIndex(), "_" + resizeRequest.getResizeType().name().toLowerCase(Locale.ROOT),
523+
resizeRequest.getTargetIndexRequest().index());
524+
HttpEntity entity = createEntity(resizeRequest, REQUEST_BODY_CONTENT_TYPE);
525+
return new Request(HttpPut.METHOD_NAME, endpoint, params.getParams(), entity);
500526
}
501527

502528
private static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType) throws IOException {

0 commit comments

Comments
 (0)