Add delete alias to the HLRC (#48819)
The delete alias call is a rest only API call, but should still be added to the rest client. This commit adds it as well as relevant tests. Ref #47678
This commit is contained in:
parent
561351d2fc
commit
bc23bc5146
|
@ -50,6 +50,7 @@ import org.elasticsearch.client.indices.CloseIndexRequest;
|
|||
import org.elasticsearch.client.indices.CloseIndexResponse;
|
||||
import org.elasticsearch.client.indices.CreateIndexRequest;
|
||||
import org.elasticsearch.client.indices.CreateIndexResponse;
|
||||
import org.elasticsearch.client.indices.DeleteAliasRequest;
|
||||
import org.elasticsearch.client.indices.FreezeIndexRequest;
|
||||
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
|
||||
import org.elasticsearch.client.indices.GetFieldMappingsResponse;
|
||||
|
@ -1449,4 +1450,28 @@ public final class IndicesClient {
|
|||
return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::reloadAnalyzers, options,
|
||||
ReloadAnalyzersResponse::fromXContent, listener, emptySet());
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronously calls the delete alias api
|
||||
* @param request the request
|
||||
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
|
||||
*/
|
||||
public org.elasticsearch.client.core.AcknowledgedResponse deleteAlias(DeleteAliasRequest request,
|
||||
RequestOptions options) throws IOException {
|
||||
return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::deleteAlias, options,
|
||||
org.elasticsearch.client.core.AcknowledgedResponse::fromXContent, emptySet());
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously calls the delete alias 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
|
||||
* @return cancellable that may be used to cancel the request
|
||||
*/
|
||||
public Cancellable deleteAliasAsync(DeleteAliasRequest request, RequestOptions options,
|
||||
ActionListener<org.elasticsearch.client.core.AcknowledgedResponse> listener) {
|
||||
return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::deleteAlias, options,
|
||||
org.elasticsearch.client.core.AcknowledgedResponse::fromXContent, listener, emptySet());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@ import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryReques
|
|||
import org.elasticsearch.client.indices.AnalyzeRequest;
|
||||
import org.elasticsearch.client.indices.CloseIndexRequest;
|
||||
import org.elasticsearch.client.indices.CreateIndexRequest;
|
||||
import org.elasticsearch.client.indices.DeleteAliasRequest;
|
||||
import org.elasticsearch.client.indices.FreezeIndexRequest;
|
||||
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
|
||||
import org.elasticsearch.client.indices.GetIndexRequest;
|
||||
|
@ -663,4 +664,17 @@ final class IndicesRequestConverters {
|
|||
request.addParameters(parameters.asMap());
|
||||
return request;
|
||||
}
|
||||
|
||||
static Request deleteAlias(DeleteAliasRequest deleteAliasRequest) {
|
||||
String endpoint = new RequestConverters.EndpointBuilder()
|
||||
.addPathPart(deleteAliasRequest.getIndex())
|
||||
.addPathPartAsIs("_alias")
|
||||
.addPathPart(deleteAliasRequest.getAlias()).build();
|
||||
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
|
||||
RequestConverters.Params parameters = new RequestConverters.Params();
|
||||
parameters.withTimeout(deleteAliasRequest.timeout());
|
||||
parameters.withMasterTimeout(deleteAliasRequest.masterNodeTimeout());
|
||||
request.addParameters(parameters.asMap());
|
||||
return request;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* 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.client.TimedRequest;
|
||||
|
||||
public class DeleteAliasRequest extends TimedRequest {
|
||||
|
||||
private final String index;
|
||||
private final String alias;
|
||||
|
||||
public DeleteAliasRequest(String index, String alias) {
|
||||
this.index = index;
|
||||
this.alias = alias;
|
||||
}
|
||||
|
||||
public String getIndex() {
|
||||
return index;
|
||||
}
|
||||
|
||||
public String getAlias() {
|
||||
return alias;
|
||||
}
|
||||
}
|
|
@ -61,6 +61,7 @@ import org.elasticsearch.client.indices.CloseIndexRequest;
|
|||
import org.elasticsearch.client.indices.CloseIndexResponse;
|
||||
import org.elasticsearch.client.indices.CreateIndexRequest;
|
||||
import org.elasticsearch.client.indices.CreateIndexResponse;
|
||||
import org.elasticsearch.client.indices.DeleteAliasRequest;
|
||||
import org.elasticsearch.client.indices.FreezeIndexRequest;
|
||||
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
|
||||
import org.elasticsearch.client.indices.GetFieldMappingsResponse;
|
||||
|
@ -1947,4 +1948,32 @@ public class IndicesClientIT extends ESRestHighLevelClientTestCase {
|
|||
assertNotNull(reloadResponse.shards());
|
||||
assertTrue(reloadResponse.getReloadedDetails().containsKey("test"));
|
||||
}
|
||||
|
||||
public void testDeleteAlias() throws IOException {
|
||||
String index = "test";
|
||||
createIndex(index, Settings.EMPTY);
|
||||
|
||||
String alias = "alias";
|
||||
String alias2 = "alias2";
|
||||
IndicesAliasesRequest aliasesAddRemoveRequest = new IndicesAliasesRequest();
|
||||
aliasesAddRemoveRequest.addAliasAction(new AliasActions(AliasActions.Type.ADD).indices(index).alias(alias));
|
||||
aliasesAddRemoveRequest.addAliasAction(new AliasActions(AliasActions.Type.ADD).indices(index).alias(alias + "2"));
|
||||
AcknowledgedResponse aliasResponse = execute(aliasesAddRemoveRequest, highLevelClient().indices()::updateAliases,
|
||||
highLevelClient().indices()::updateAliasesAsync);
|
||||
assertTrue(aliasResponse.isAcknowledged());
|
||||
assertThat(aliasExists(alias), equalTo(true));
|
||||
assertThat(aliasExists(alias2), equalTo(true));
|
||||
assertThat(aliasExists(index, alias), equalTo(true));
|
||||
assertThat(aliasExists(index, alias2), equalTo(true));
|
||||
|
||||
DeleteAliasRequest request = new DeleteAliasRequest(index, alias);
|
||||
org.elasticsearch.client.core.AcknowledgedResponse aliasDeleteResponse = execute(request,
|
||||
highLevelClient().indices()::deleteAlias,
|
||||
highLevelClient().indices()::deleteAliasAsync);
|
||||
|
||||
assertThat(aliasExists(alias), equalTo(false));
|
||||
assertThat(aliasExists(alias2), equalTo(true));
|
||||
assertThat(aliasExists(index, alias), equalTo(false));
|
||||
assertThat(aliasExists(index, alias2), equalTo(true));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,6 +46,7 @@ import org.elasticsearch.action.support.master.AcknowledgedRequest;
|
|||
import org.elasticsearch.client.indices.AnalyzeRequest;
|
||||
import org.elasticsearch.client.indices.CloseIndexRequest;
|
||||
import org.elasticsearch.client.indices.CreateIndexRequest;
|
||||
import org.elasticsearch.client.indices.DeleteAliasRequest;
|
||||
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
|
||||
import org.elasticsearch.client.indices.GetIndexRequest;
|
||||
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
|
||||
|
@ -1243,4 +1244,18 @@ public class IndicesRequestConvertersTests extends ESTestCase {
|
|||
Assert.assertThat(request.getParameters(), equalTo(expectedParams));
|
||||
Assert.assertThat(request.getEntity(), nullValue());
|
||||
}
|
||||
|
||||
public void testDeleteAlias() {
|
||||
DeleteAliasRequest deleteAliasRequest = new DeleteAliasRequest(randomAlphaOfLength(4), randomAlphaOfLength(4));
|
||||
|
||||
Map<String, String> expectedParams = new HashMap<>();
|
||||
RequestConvertersTests.setRandomMasterTimeout(deleteAliasRequest, expectedParams);
|
||||
RequestConvertersTests.setRandomTimeout(deleteAliasRequest, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
|
||||
|
||||
Request request = IndicesRequestConverters.deleteAlias(deleteAliasRequest);
|
||||
Assert.assertThat(request.getMethod(), equalTo(HttpDelete.METHOD_NAME));
|
||||
Assert.assertThat(request.getEndpoint(), equalTo("/" + deleteAliasRequest.getIndex() + "/_alias/" + deleteAliasRequest.getAlias()));
|
||||
Assert.assertThat(request.getParameters(), equalTo(expectedParams));
|
||||
Assert.assertThat(request.getEntity(), nullValue());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -763,7 +763,6 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|||
"create",
|
||||
"get_script_context",
|
||||
"get_source",
|
||||
"indices.delete_alias",
|
||||
"indices.exists_type",
|
||||
"indices.get_upgrade",
|
||||
"indices.put_alias",
|
||||
|
|
|
@ -65,6 +65,7 @@ import org.elasticsearch.client.indices.CloseIndexRequest;
|
|||
import org.elasticsearch.client.indices.CloseIndexResponse;
|
||||
import org.elasticsearch.client.indices.CreateIndexRequest;
|
||||
import org.elasticsearch.client.indices.CreateIndexResponse;
|
||||
import org.elasticsearch.client.indices.DeleteAliasRequest;
|
||||
import org.elasticsearch.client.indices.DetailAnalyzeResponse;
|
||||
import org.elasticsearch.client.indices.FreezeIndexRequest;
|
||||
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
|
||||
|
@ -2879,4 +2880,87 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase
|
|||
// end::reload-analyzers-notfound
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public void testDeleteAlias() throws Exception {
|
||||
RestHighLevelClient client = highLevelClient();
|
||||
|
||||
{
|
||||
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index1"), RequestOptions.DEFAULT);
|
||||
assertTrue(createIndexResponse.isAcknowledged());
|
||||
}
|
||||
{
|
||||
IndicesAliasesRequest request = new IndicesAliasesRequest();
|
||||
AliasActions aliasAction =
|
||||
new AliasActions(AliasActions.Type.ADD)
|
||||
.index("index1")
|
||||
.alias("alias1");
|
||||
request.addAliasAction(aliasAction);
|
||||
AcknowledgedResponse indicesAliasesResponse =
|
||||
client.indices().updateAliases(request, RequestOptions.DEFAULT);
|
||||
assertTrue(indicesAliasesResponse.isAcknowledged());
|
||||
}
|
||||
{
|
||||
IndicesAliasesRequest request = new IndicesAliasesRequest();
|
||||
AliasActions aliasAction =
|
||||
new AliasActions(AliasActions.Type.ADD)
|
||||
.index("index1")
|
||||
.alias("alias2");
|
||||
request.addAliasAction(aliasAction);
|
||||
AcknowledgedResponse indicesAliasesResponse =
|
||||
client.indices().updateAliases(request, RequestOptions.DEFAULT);
|
||||
assertTrue(indicesAliasesResponse.isAcknowledged());
|
||||
}
|
||||
{
|
||||
// tag::delete-alias-request
|
||||
DeleteAliasRequest request = new DeleteAliasRequest("index1", "alias1");
|
||||
// end::delete-alias-request
|
||||
|
||||
// tag::delete-alias-request-timeout
|
||||
request.setTimeout(TimeValue.timeValueMinutes(2)); // <1>
|
||||
// end::delete-alias-request-timeout
|
||||
// tag::delete-alias-request-masterTimeout
|
||||
request.setMasterTimeout(TimeValue.timeValueMinutes(1)); // <1>
|
||||
// end::delete-alias-request-masterTimeout
|
||||
|
||||
// tag::delete-alias-execute
|
||||
org.elasticsearch.client.core.AcknowledgedResponse deleteAliasResponse =
|
||||
client.indices().deleteAlias(request, RequestOptions.DEFAULT);
|
||||
// end::delete-alias-execute
|
||||
|
||||
// tag::delete-alias-response
|
||||
boolean acknowledged = deleteAliasResponse.isAcknowledged(); // <1>
|
||||
// end::delete-alias-response
|
||||
assertTrue(acknowledged);
|
||||
}
|
||||
|
||||
{
|
||||
DeleteAliasRequest request = new DeleteAliasRequest("index1", "alias2"); // <1>
|
||||
|
||||
// tag::delete-alias-execute-listener
|
||||
ActionListener<org.elasticsearch.client.core.AcknowledgedResponse> listener =
|
||||
new ActionListener<org.elasticsearch.client.core.AcknowledgedResponse>() {
|
||||
@Override
|
||||
public void onResponse(org.elasticsearch.client.core.AcknowledgedResponse deleteAliasResponse) {
|
||||
// <1>
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Exception e) {
|
||||
// <2>
|
||||
}
|
||||
};
|
||||
// end::delete-alias-execute-listener
|
||||
|
||||
// Replace the empty listener by a blocking listener in test
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
listener = new LatchedActionListener<>(listener, latch);
|
||||
|
||||
// tag::delete-alias-execute-async
|
||||
client.indices().deleteAliasAsync(request, RequestOptions.DEFAULT, listener); // <1>
|
||||
// end::delete-alias-execute-async
|
||||
|
||||
assertTrue(latch.await(30L, TimeUnit.SECONDS));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
--
|
||||
:api: delete-alias
|
||||
:request: DeleteAliasRequest
|
||||
:response: AcknowledgedResponse
|
||||
--
|
||||
|
||||
[id="{upid}-{api}"]
|
||||
=== Delete Alias API
|
||||
|
||||
[id="{upid}-{api}-request"]
|
||||
==== Delete Alias Request
|
||||
|
||||
An +{request}+ requires an `index` and an `alias` argument:
|
||||
|
||||
["source","java",subs="attributes,callouts,macros"]
|
||||
--------------------------------------------------
|
||||
include-tagged::{doc-tests-file}[{api}-request]
|
||||
--------------------------------------------------
|
||||
|
||||
==== 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 opened
|
||||
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`
|
||||
|
||||
[id="{upid}-{api}-response"]
|
||||
==== Delete Alias Response
|
||||
|
||||
The returned +{response}+ indicates if the request to delete the alias
|
||||
was received.
|
||||
|
||||
["source","java",subs="attributes,callouts,macros"]
|
||||
--------------------------------------------------
|
||||
include-tagged::{doc-tests-file}[{api}-response]
|
||||
--------------------------------------------------
|
||||
<1> Whether or not the request to delete the alias was
|
||||
acknowledged.
|
||||
|
||||
include::../execution.asciidoc[]
|
|
@ -120,6 +120,7 @@ Alias Management::
|
|||
* <<{upid}-update-aliases>>
|
||||
* <<{upid}-exists-alias>>
|
||||
* <<{upid}-get-alias>>
|
||||
* <<{upid}-delete-alias>>
|
||||
|
||||
Template Management::
|
||||
* <<{upid}-get-templates>>
|
||||
|
@ -145,6 +146,7 @@ include::indices/put_mapping.asciidoc[]
|
|||
include::indices/get_mappings.asciidoc[]
|
||||
include::indices/get_field_mappings.asciidoc[]
|
||||
include::indices/update_aliases.asciidoc[]
|
||||
include::indices/delete_alias.asciidoc[]
|
||||
include::indices/exists_alias.asciidoc[]
|
||||
include::indices/get_alias.asciidoc[]
|
||||
include::indices/put_settings.asciidoc[]
|
||||
|
|
Loading…
Reference in New Issue