-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Added Delete Index support to high-level REST client #27019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
e0345a8
1540e93
c8e35a6
77a9939
a18662f
a03b759
30319ea
7261ac9
1fe19fe
ec05d37
63ba5b5
afdcb40
c8620a9
dda2d39
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ | |
| import org.apache.http.entity.ContentType; | ||
| import org.apache.lucene.util.BytesRef; | ||
| import org.elasticsearch.action.DocWriteRequest; | ||
| import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; | ||
| import org.elasticsearch.action.bulk.BulkRequest; | ||
| import org.elasticsearch.action.delete.DeleteRequest; | ||
| import org.elasticsearch.action.get.GetRequest; | ||
|
|
@@ -123,6 +124,17 @@ static Request delete(DeleteRequest deleteRequest) { | |
| return new Request(HttpDelete.METHOD_NAME, endpoint, parameters.getParams(), null); | ||
| } | ||
|
|
||
|
|
||
| static Request deleteIndex(DeleteIndexRequest deleteIndexRequest) { | ||
| String endpoint = endpoint(deleteIndexRequest.indices(), ""); | ||
|
|
||
| Params parameters = Params.builder(); | ||
| parameters.withTimeout(deleteIndexRequest.timeout()); | ||
| parameters.withIndicesOptions(deleteIndexRequest.indicesOptions()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that |
||
|
|
||
| return new Request(HttpDelete.METHOD_NAME, endpoint, parameters.getParams(), null); | ||
| } | ||
|
|
||
| static Request info() { | ||
| return new Request(HttpGet.METHOD_NAME, "/", Collections.emptyMap(), null); | ||
| } | ||
|
|
@@ -378,6 +390,11 @@ static String endpoint(String[] indices, String[] types, String endpoint) { | |
| return endpoint(String.join(",", indices), String.join(",", types), endpoint); | ||
| } | ||
|
|
||
|
|
||
|
||
| static String endpoint(String[] indices, String endpoint) { | ||
|
||
| return endpoint(String.join(",", indices), endpoint); | ||
| } | ||
|
|
||
| /** | ||
| * Utility method to build request's endpoint. | ||
| */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a unit test method to RequestTests for the new deleteIndex method? |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,8 @@ | |
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.action.ActionRequest; | ||
| import org.elasticsearch.action.ActionRequestValidationException; | ||
| import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; | ||
| import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; | ||
| import org.elasticsearch.action.bulk.BulkRequest; | ||
| import org.elasticsearch.action.bulk.BulkResponse; | ||
| import org.elasticsearch.action.delete.DeleteRequest; | ||
|
|
@@ -346,6 +348,26 @@ public void deleteAsync(DeleteRequest deleteRequest, ActionListener<DeleteRespon | |
| Collections.singleton(404), headers); | ||
| } | ||
|
|
||
| /** | ||
| * Deletes an index using the Delete Index api | ||
| * <p> | ||
| * See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html">Delete API on elastic.co</a> | ||
|
||
| */ | ||
| public DeleteIndexResponse deleteIndex(DeleteIndexRequest deleteIndexRequest, Header... headers) throws IOException { | ||
|
||
| return performRequestAndParseEntity(deleteIndexRequest, Request::deleteIndex, DeleteIndexResponse::fromXContent, | ||
| Collections.singleton(404), headers); | ||
| } | ||
|
|
||
| /** | ||
| * Asynchronously deletes an index using the Delete Index api | ||
| * <p> | ||
| * See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html">Delete API on elastic.co</a> | ||
|
||
| */ | ||
| public void deleteIndexAsync(DeleteIndexRequest deleteIndexRequest, ActionListener<DeleteIndexResponse> listener, Header... headers) { | ||
| performRequestAsyncAndParseEntity(deleteIndexRequest, Request::deleteIndex, DeleteIndexResponse::fromXContent, listener, | ||
| Collections.singleton(404), headers); | ||
|
||
| } | ||
|
|
||
| /** | ||
| * Executes a search using the Search api | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
|
|
||
| import org.elasticsearch.ElasticsearchException; | ||
| import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; | ||
| import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; | ||
| import org.elasticsearch.action.get.GetRequest; | ||
| import org.elasticsearch.action.index.IndexRequest; | ||
| import org.elasticsearch.rest.RestStatus; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
|
|
||
| public class IndexAdminIT extends ESRestHighLevelClientTestCase { | ||
|
||
|
|
||
| public void testDeleteIndex_ifIndexExists() throws IOException { | ||
|
||
| // Testing existing index is deleted | ||
| GetRequest getRequest = new GetRequest("test_index", "type", "id"); | ||
| highLevelClient().index(new IndexRequest("test_index", "type", "id").source(Collections.singletonMap("foo", "bar"))); | ||
|
|
||
| DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("test_index"); | ||
| DeleteIndexResponse deleteIndexResponse = | ||
| execute(deleteIndexRequest, highLevelClient()::deleteIndex, highLevelClient()::deleteIndexAsync); | ||
| assertTrue(deleteIndexResponse.isAcknowledged()); | ||
|
|
||
| ElasticsearchException exception = expectThrows(ElasticsearchException.class, | ||
|
||
| () -> execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync)); | ||
| assertEquals(RestStatus.NOT_FOUND, exception.status()); | ||
| } | ||
|
|
||
| public void testDeleteIndex_ifIndexNotExists() throws IOException { | ||
| // Testing error on non-existing index | ||
| DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("non_existent_index"); | ||
|
|
||
| ElasticsearchException exception = expectThrows(ElasticsearchException.class, | ||
| () -> execute(deleteIndexRequest, highLevelClient()::deleteIndex, highLevelClient()::deleteIndexAsync)); | ||
| assertEquals(RestStatus.NOT_FOUND, exception.status()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| import org.elasticsearch.action.support.master.AcknowledgedResponse; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
|
|
@@ -48,5 +49,4 @@ public void writeTo(StreamOutput out) throws IOException { | |
| super.writeTo(out); | ||
| writeAcknowledged(out); | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you remove this unnecessary change? |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,9 +22,12 @@ | |
| import org.elasticsearch.action.support.master.AcknowledgedResponse; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; | ||
|
|
||
| /** | ||
| * A response for a delete index action. | ||
| */ | ||
|
|
@@ -48,4 +51,34 @@ public void writeTo(StreamOutput out) throws IOException { | |
| super.writeTo(out); | ||
| writeAcknowledged(out); | ||
| } | ||
|
|
||
| public static DeleteIndexResponse fromXContent(XContentParser parser) throws IOException { | ||
|
||
| ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); | ||
|
|
||
| DeleteIndexResponse.Builder context = new DeleteIndexResponse.Builder(); | ||
| while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
| parseXContentFields(parser, context); | ||
| } | ||
| return context.build(); | ||
| } | ||
|
|
||
| /** | ||
| * Parse the current token and update the parsing context appropriately. | ||
| */ | ||
| private static void parseXContentFields(XContentParser parser, DeleteIndexResponse.Builder context) throws IOException { | ||
| AcknowledgedResponse.parseInnerToXContent(parser, context); | ||
| } | ||
|
|
||
| /** | ||
| * Builder class for {@link DeleteIndexResponse}. This builder is usually used during xcontent parsing to | ||
| * temporarily store the parsed values, then the {@link AcknowledgedResponse.Builder#build()} method is called to | ||
| * instantiate the {@link DeleteIndexResponse}. | ||
| */ | ||
| public static class Builder extends AcknowledgedResponse.Builder { | ||
|
|
||
| @Override | ||
| public DeleteIndexResponse build() { | ||
| return new DeleteIndexResponse(acknowledged); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,14 +21,21 @@ | |
| import org.elasticsearch.action.ActionResponse; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.common.xcontent.ToXContentObject; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; | ||
|
|
||
| /** | ||
| * Abstract class that allows to mark action responses that support acknowledgements. | ||
| * Facilitates consistency across different api. | ||
| */ | ||
| public abstract class AcknowledgedResponse extends ActionResponse { | ||
| public abstract class AcknowledgedResponse extends ActionResponse implements ToXContentObject { | ||
|
|
||
| private static final String ACKNOWLEDGED = "acknowledged"; | ||
|
|
||
| private boolean acknowledged; | ||
|
|
||
|
|
@@ -61,4 +68,41 @@ protected void readAcknowledged(StreamInput in) throws IOException { | |
| protected void writeAcknowledged(StreamOutput out) throws IOException { | ||
| out.writeBoolean(acknowledged); | ||
| } | ||
|
|
||
| @Override | ||
| public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
|
||
| builder.startObject(); | ||
| builder.field("acknowledged", acknowledged); | ||
| builder.endObject(); | ||
| return builder; | ||
| } | ||
|
|
||
| protected static void parseInnerToXContent(XContentParser parser, AcknowledgedResponse.Builder context) throws IOException { | ||
| XContentParser.Token token = parser.currentToken(); | ||
|
||
| ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser::getTokenLocation); | ||
|
|
||
| String currentFieldName = parser.currentName(); | ||
| token = parser.nextToken(); | ||
|
|
||
| if (token.isValue()) { | ||
| if (ACKNOWLEDGED.equals(currentFieldName)) { | ||
| context.setAcknowledged(parser.booleanValue()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public abstract static class Builder { | ||
|
|
||
| protected boolean acknowledged = false; | ||
|
|
||
| public boolean isAcknowledged() { | ||
| return acknowledged; | ||
| } | ||
|
|
||
| public void setAcknowledged(boolean acknowledged) { | ||
| this.acknowledged = acknowledged; | ||
| } | ||
|
|
||
| public abstract AcknowledgedResponse build(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can you remove this additional empty line? one is enough between two methods.