Skip to content

Commit 8bf3324

Browse files
catalin-ursachijavanna
authored andcommitted
Add Delete Index API support to high-level REST client (#27019)
Relates to #25847
1 parent 77f8773 commit 8bf3324

File tree

17 files changed

+658
-32
lines changed

17 files changed

+658
-32
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client;
21+
22+
import org.apache.http.Header;
23+
import org.elasticsearch.action.ActionListener;
24+
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
25+
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
26+
27+
import java.io.IOException;
28+
import java.util.Collections;
29+
30+
/**
31+
* A wrapper for the {@link RestHighLevelClient} that provides methods for accessing the Indices API.
32+
*
33+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html">Indices API on elastic.co</a>
34+
*/
35+
public final class IndicesClient {
36+
private final RestHighLevelClient restHighLevelClient;
37+
38+
public IndicesClient(RestHighLevelClient restHighLevelClient) {
39+
this.restHighLevelClient = restHighLevelClient;
40+
}
41+
42+
/**
43+
* Deletes an index using the Delete Index API
44+
* <p>
45+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html">
46+
* Delete Index API on elastic.co</a>
47+
*/
48+
public DeleteIndexResponse deleteIndex(DeleteIndexRequest deleteIndexRequest, Header... headers) throws IOException {
49+
return restHighLevelClient.performRequestAndParseEntity(deleteIndexRequest, Request::deleteIndex, DeleteIndexResponse::fromXContent,
50+
Collections.emptySet(), headers);
51+
}
52+
53+
/**
54+
* Asynchronously deletes an index using the Delete Index API
55+
* <p>
56+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html">
57+
* Delete Index API on elastic.co</a>
58+
*/
59+
public void deleteIndexAsync(DeleteIndexRequest deleteIndexRequest, ActionListener<DeleteIndexResponse> listener, Header... headers) {
60+
restHighLevelClient.performRequestAsyncAndParseEntity(deleteIndexRequest, Request::deleteIndex, DeleteIndexResponse::fromXContent,
61+
listener, Collections.emptySet(), headers);
62+
}
63+
}

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

100644100755
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.http.entity.ContentType;
3030
import org.apache.lucene.util.BytesRef;
3131
import org.elasticsearch.action.DocWriteRequest;
32+
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
3233
import org.elasticsearch.action.bulk.BulkRequest;
3334
import org.elasticsearch.action.delete.DeleteRequest;
3435
import org.elasticsearch.action.get.GetRequest;
@@ -123,6 +124,17 @@ static Request delete(DeleteRequest deleteRequest) {
123124
return new Request(HttpDelete.METHOD_NAME, endpoint, parameters.getParams(), null);
124125
}
125126

127+
static Request deleteIndex(DeleteIndexRequest deleteIndexRequest) {
128+
String endpoint = endpoint(deleteIndexRequest.indices(), Strings.EMPTY_ARRAY, "");
129+
130+
Params parameters = Params.builder();
131+
parameters.withTimeout(deleteIndexRequest.timeout());
132+
parameters.withMasterTimeout(deleteIndexRequest.masterNodeTimeout());
133+
parameters.withIndicesOptions(deleteIndexRequest.indicesOptions());
134+
135+
return new Request(HttpDelete.METHOD_NAME, endpoint, parameters.getParams(), null);
136+
}
137+
126138
static Request info() {
127139
return new Request(HttpGet.METHOD_NAME, "/", Collections.emptyMap(), null);
128140
}
@@ -449,6 +461,10 @@ Params withFetchSourceContext(FetchSourceContext fetchSourceContext) {
449461
return this;
450462
}
451463

464+
Params withMasterTimeout(TimeValue masterTimeout) {
465+
return putParam("master_timeout", masterTimeout);
466+
}
467+
452468
Params withParent(String parent) {
453469
return putParam("parent", parent);
454470
}

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

100644100755
Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ public class RestHighLevelClient implements Closeable {
176176
private final NamedXContentRegistry registry;
177177
private final CheckedConsumer<RestClient, IOException> doClose;
178178

179+
private final IndicesClient indicesClient = new IndicesClient(this);
180+
179181
/**
180182
* Creates a {@link RestHighLevelClient} given the low level {@link RestClientBuilder} that allows to build the
181183
* {@link RestClient} to be used to perform requests.
@@ -220,6 +222,15 @@ public final void close() throws IOException {
220222
doClose.accept(client);
221223
}
222224

225+
/**
226+
* Provides an {@link IndicesClient} which can be used to access the Indices API.
227+
*
228+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html">Indices API on elastic.co</a>
229+
*/
230+
public IndicesClient indices() {
231+
return indicesClient;
232+
}
233+
223234
/**
224235
* Executes a bulk request using the Bulk API
225236
*
@@ -327,7 +338,7 @@ public void updateAsync(UpdateRequest updateRequest, ActionListener<UpdateRespon
327338
}
328339

329340
/**
330-
* Deletes a document by id using the Delete api
341+
* Deletes a document by id using the Delete API
331342
*
332343
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html">Delete API on elastic.co</a>
333344
*/
@@ -337,7 +348,7 @@ public DeleteResponse delete(DeleteRequest deleteRequest, Header... headers) thr
337348
}
338349

339350
/**
340-
* Asynchronously deletes a document by id using the Delete api
351+
* Asynchronously deletes a document by id using the Delete API
341352
*
342353
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html">Delete API on elastic.co</a>
343354
*/
@@ -347,7 +358,7 @@ public void deleteAsync(DeleteRequest deleteRequest, ActionListener<DeleteRespon
347358
}
348359

349360
/**
350-
* Executes a search using the Search api
361+
* Executes a search using the Search API
351362
*
352363
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html">Search API on elastic.co</a>
353364
*/
@@ -356,7 +367,7 @@ public SearchResponse search(SearchRequest searchRequest, Header... headers) thr
356367
}
357368

358369
/**
359-
* Asynchronously executes a search using the Search api
370+
* Asynchronously executes a search using the Search API
360371
*
361372
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html">Search API on elastic.co</a>
362373
*/
@@ -365,7 +376,7 @@ public void searchAsync(SearchRequest searchRequest, ActionListener<SearchRespon
365376
}
366377

367378
/**
368-
* Executes a search using the Search Scroll api
379+
* Executes a search using the Search Scroll API
369380
*
370381
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html">Search Scroll
371382
* API on elastic.co</a>
@@ -375,7 +386,7 @@ public SearchResponse searchScroll(SearchScrollRequest searchScrollRequest, Head
375386
}
376387

377388
/**
378-
* Asynchronously executes a search using the Search Scroll api
389+
* Asynchronously executes a search using the Search Scroll API
379390
*
380391
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html">Search Scroll
381392
* API on elastic.co</a>
@@ -386,7 +397,7 @@ public void searchScrollAsync(SearchScrollRequest searchScrollRequest, ActionLis
386397
}
387398

388399
/**
389-
* Clears one or more scroll ids using the Clear Scroll api
400+
* Clears one or more scroll ids using the Clear Scroll API
390401
*
391402
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html#_clear_scroll_api">
392403
* Clear Scroll API on elastic.co</a>
@@ -397,7 +408,7 @@ public ClearScrollResponse clearScroll(ClearScrollRequest clearScrollRequest, He
397408
}
398409

399410
/**
400-
* Asynchronously clears one or more scroll ids using the Clear Scroll api
411+
* Asynchronously clears one or more scroll ids using the Clear Scroll API
401412
*
402413
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html#_clear_scroll_api">
403414
* Clear Scroll API on elastic.co</a>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client;
21+
22+
import org.elasticsearch.ElasticsearchException;
23+
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
24+
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
25+
import org.elasticsearch.rest.RestStatus;
26+
27+
import java.io.IOException;
28+
29+
public class IndicesClientIT extends ESRestHighLevelClientTestCase {
30+
31+
public void testDeleteIndex() throws IOException {
32+
{
33+
// Delete index if exists
34+
String indexName = "test_index";
35+
createIndex(indexName);
36+
37+
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
38+
DeleteIndexResponse deleteIndexResponse =
39+
execute(deleteIndexRequest, highLevelClient().indices()::deleteIndex, highLevelClient().indices()::deleteIndexAsync);
40+
assertTrue(deleteIndexResponse.isAcknowledged());
41+
42+
assertFalse(indexExists(indexName));
43+
}
44+
{
45+
// Return 404 if index doesn't exist
46+
String nonExistentIndex = "non_existent_index";
47+
assertFalse(indexExists(nonExistentIndex));
48+
49+
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(nonExistentIndex);
50+
51+
ElasticsearchException exception = expectThrows(ElasticsearchException.class,
52+
() -> execute(deleteIndexRequest, highLevelClient().indices()::deleteIndex, highLevelClient().indices()::deleteIndexAsync));
53+
assertEquals(RestStatus.NOT_FOUND, exception.status());
54+
}
55+
}
56+
57+
private static void createIndex(String index) throws IOException {
58+
Response response = client().performRequest("PUT", index);
59+
60+
assertEquals(200, response.getStatusLine().getStatusCode());
61+
}
62+
63+
private static boolean indexExists(String index) throws IOException {
64+
Response response = client().performRequest("HEAD", index);
65+
66+
return response.getStatusLine().getStatusCode() == 200;
67+
}
68+
}

0 commit comments

Comments
 (0)