diff --git a/src/main/java/org/elasticsearch/action/ActionModule.java b/src/main/java/org/elasticsearch/action/ActionModule.java index 997e548639d..dc03a164528 100644 --- a/src/main/java/org/elasticsearch/action/ActionModule.java +++ b/src/main/java/org/elasticsearch/action/ActionModule.java @@ -42,6 +42,8 @@ import org.elasticsearch.action.admin.cluster.state.ClusterStateAction; import org.elasticsearch.action.admin.cluster.state.TransportClusterStateAction; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesAction; import org.elasticsearch.action.admin.indices.alias.TransportIndicesAliasesAction; +import org.elasticsearch.action.admin.indices.alias.exists.IndicesExistsAliasesAction; +import org.elasticsearch.action.admin.indices.alias.exists.TransportIndicesExistsAliasesAction; import org.elasticsearch.action.admin.indices.alias.get.IndicesGetAliasesAction; import org.elasticsearch.action.admin.indices.alias.get.TransportIndicesGetAliasesAction; import org.elasticsearch.action.admin.indices.analyze.AnalyzeAction; @@ -204,6 +206,7 @@ public class ActionModule extends AbstractModule { registerAction(PutWarmerAction.INSTANCE, TransportPutWarmerAction.class); registerAction(DeleteWarmerAction.INSTANCE, TransportDeleteWarmerAction.class); registerAction(IndicesGetAliasesAction.INSTANCE, TransportIndicesGetAliasesAction.class); + registerAction(IndicesExistsAliasesAction.INSTANCE, TransportIndicesExistsAliasesAction.class); registerAction(IndexAction.INSTANCE, TransportIndexAction.class); registerAction(GetAction.INSTANCE, TransportGetAction.class); diff --git a/src/main/java/org/elasticsearch/action/admin/indices/alias/exists/IndicesExistsAliasesAction.java b/src/main/java/org/elasticsearch/action/admin/indices/alias/exists/IndicesExistsAliasesAction.java new file mode 100644 index 00000000000..f0d5bc63c62 --- /dev/null +++ b/src/main/java/org/elasticsearch/action/admin/indices/alias/exists/IndicesExistsAliasesAction.java @@ -0,0 +1,46 @@ +/* + * Licensed to ElasticSearch and Shay Banon 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.action.admin.indices.alias.exists; + +import org.elasticsearch.action.admin.indices.IndicesAction; +import org.elasticsearch.action.admin.indices.alias.get.IndicesGetAliasesRequest; +import org.elasticsearch.client.IndicesAdminClient; + +/** + */ +public class IndicesExistsAliasesAction extends IndicesAction { + + public static final IndicesExistsAliasesAction INSTANCE = new IndicesExistsAliasesAction(); + public static final String NAME = "indices/exists/aliases"; + + private IndicesExistsAliasesAction() { + super(NAME); + } + + @Override + public IndicesExistsAliasesRequestBuilder newRequestBuilder(IndicesAdminClient client) { + return new IndicesExistsAliasesRequestBuilder(client); + } + + @Override + public IndicesExistsAliasesResponse newResponse() { + return new IndicesExistsAliasesResponse(); + } +} diff --git a/src/main/java/org/elasticsearch/action/admin/indices/alias/exists/IndicesExistsAliasesRequestBuilder.java b/src/main/java/org/elasticsearch/action/admin/indices/alias/exists/IndicesExistsAliasesRequestBuilder.java new file mode 100644 index 00000000000..17a65bfb0ed --- /dev/null +++ b/src/main/java/org/elasticsearch/action/admin/indices/alias/exists/IndicesExistsAliasesRequestBuilder.java @@ -0,0 +1,39 @@ +/* + * Licensed to ElasticSearch and Shay Banon 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.action.admin.indices.alias.exists; + +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.admin.indices.alias.get.BaseIndicesAliasesRequestBuilder; +import org.elasticsearch.client.IndicesAdminClient; + +/** + */ +public class IndicesExistsAliasesRequestBuilder extends BaseIndicesAliasesRequestBuilder { + + public IndicesExistsAliasesRequestBuilder(IndicesAdminClient client, String... aliases) { + super(client, aliases); + } + + @Override + protected void doExecute(ActionListener listener) { + ((IndicesAdminClient) client).existsAliases(request, listener); + } + +} diff --git a/src/main/java/org/elasticsearch/action/admin/indices/alias/exists/IndicesExistsAliasesResponse.java b/src/main/java/org/elasticsearch/action/admin/indices/alias/exists/IndicesExistsAliasesResponse.java new file mode 100644 index 00000000000..0c57f586812 --- /dev/null +++ b/src/main/java/org/elasticsearch/action/admin/indices/alias/exists/IndicesExistsAliasesResponse.java @@ -0,0 +1,60 @@ +/* + * Licensed to ElasticSearch and Shay Banon 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.action.admin.indices.alias.exists; + +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; + +import java.io.IOException; + +/** + */ +public class IndicesExistsAliasesResponse extends ActionResponse { + + private boolean exists; + + public IndicesExistsAliasesResponse(boolean exists) { + this.exists = exists; + } + + IndicesExistsAliasesResponse() { + } + + public boolean exists() { + return exists; + } + + public boolean isExists() { + return exists(); + } + + @Override + public void readFrom(StreamInput in) throws IOException { + super.readFrom(in); + exists = in.readBoolean(); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + out.writeBoolean(exists); + } +} diff --git a/src/main/java/org/elasticsearch/action/admin/indices/alias/exists/TransportIndicesExistsAliasesAction.java b/src/main/java/org/elasticsearch/action/admin/indices/alias/exists/TransportIndicesExistsAliasesAction.java new file mode 100644 index 00000000000..88ec7317cfe --- /dev/null +++ b/src/main/java/org/elasticsearch/action/admin/indices/alias/exists/TransportIndicesExistsAliasesAction.java @@ -0,0 +1,69 @@ +/* + * Licensed to ElasticSearch and Shay Banon 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.action.admin.indices.alias.exists; + +import org.elasticsearch.ElasticSearchException; +import org.elasticsearch.action.admin.indices.alias.get.IndicesGetAliasesRequest; +import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction; +import org.elasticsearch.cluster.ClusterService; +import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.transport.TransportService; + +/** + */ +public class TransportIndicesExistsAliasesAction extends TransportMasterNodeOperationAction { + + @Inject + public TransportIndicesExistsAliasesAction(Settings settings, TransportService transportService, ClusterService clusterService, ThreadPool threadPool) { + super(settings, transportService, clusterService, threadPool); + } + + @Override + protected String transportAction() { + return IndicesExistsAliasesAction.NAME; + } + + @Override + protected String executor() { + return ThreadPool.Names.MANAGEMENT; + } + + @Override + protected IndicesGetAliasesRequest newRequest() { + return new IndicesGetAliasesRequest(); + } + + @Override + protected IndicesExistsAliasesResponse newResponse() { + return new IndicesExistsAliasesResponse(); + } + + @Override + protected IndicesExistsAliasesResponse masterOperation(IndicesGetAliasesRequest request, ClusterState state) throws ElasticSearchException { + String[] concreteIndices = state.metaData().concreteIndices(request.indices(), request.ignoreIndices(), true); + request.indices(concreteIndices); + + boolean result = state.metaData().hasAliases(request.aliases(), request.indices()); + return new IndicesExistsAliasesResponse(result); + } + +} diff --git a/src/main/java/org/elasticsearch/action/admin/indices/alias/get/BaseIndicesAliasesRequestBuilder.java b/src/main/java/org/elasticsearch/action/admin/indices/alias/get/BaseIndicesAliasesRequestBuilder.java new file mode 100644 index 00000000000..ee9d31fc3c7 --- /dev/null +++ b/src/main/java/org/elasticsearch/action/admin/indices/alias/get/BaseIndicesAliasesRequestBuilder.java @@ -0,0 +1,60 @@ +/* + * Licensed to ElasticSearch and Shay Banon 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.action.admin.indices.alias.get; + +import com.google.common.collect.ObjectArrays; +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder; +import org.elasticsearch.client.IndicesAdminClient; +import org.elasticsearch.client.internal.InternalIndicesAdminClient; + +/** + */ +public abstract class BaseIndicesAliasesRequestBuilder> extends MasterNodeOperationRequestBuilder { + + public BaseIndicesAliasesRequestBuilder(IndicesAdminClient client, String... aliases) { + super((InternalIndicesAdminClient) client, new IndicesGetAliasesRequest(aliases)); + } + + @SuppressWarnings("unchecked") + public Builder setAliases(String... aliases) { + request.aliases(aliases); + return (Builder) this; + } + + @SuppressWarnings("unchecked") + public Builder addAliases(String... aliases) { + request.aliases(ObjectArrays.concat(request.aliases(), aliases, String.class)); + return (Builder) this; + } + + @SuppressWarnings("unchecked") + public Builder setIndices(String... indices) { + request.indices(indices); + return (Builder) this; + } + + @SuppressWarnings("unchecked") + public Builder addIndices(String... indices) { + request.indices(ObjectArrays.concat(request.indices(), indices, String.class)); + return (Builder) this; + } + +} diff --git a/src/main/java/org/elasticsearch/action/admin/indices/alias/get/IndicesGetAliasesRequest.java b/src/main/java/org/elasticsearch/action/admin/indices/alias/get/IndicesGetAliasesRequest.java index 9d57a7c0f3b..dceb989c5cd 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/alias/get/IndicesGetAliasesRequest.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/alias/get/IndicesGetAliasesRequest.java @@ -46,7 +46,7 @@ public class IndicesGetAliasesRequest extends MasterNodeOperationRequest { +public class IndicesGetAliasesRequestBuilder extends BaseIndicesAliasesRequestBuilder { public IndicesGetAliasesRequestBuilder(IndicesAdminClient client, String... aliases) { - super((InternalIndicesAdminClient)client, new IndicesGetAliasesRequest(aliases)); - } - - public IndicesGetAliasesRequestBuilder setAliases(String... aliases) { - request.aliases(aliases); - return this; - } - - public IndicesGetAliasesRequestBuilder addAliases(String... aliases) { - request.aliases(ObjectArrays.concat(request.aliases(), aliases, String.class)); - return this; - } - - public IndicesGetAliasesRequestBuilder setIndices(String... indices) { - request.indices(indices); - return this; - } - - public IndicesGetAliasesRequestBuilder addIndices(String... indices) { - request.indices(ObjectArrays.concat(request.indices(), indices, String.class)); - return this; + super(client, aliases); } @Override diff --git a/src/main/java/org/elasticsearch/client/IndicesAdminClient.java b/src/main/java/org/elasticsearch/client/IndicesAdminClient.java index 2c828ef2ab3..4e86e9319f3 100644 --- a/src/main/java/org/elasticsearch/client/IndicesAdminClient.java +++ b/src/main/java/org/elasticsearch/client/IndicesAdminClient.java @@ -24,6 +24,8 @@ import org.elasticsearch.action.admin.indices.IndicesAction; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequestBuilder; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse; +import org.elasticsearch.action.admin.indices.alias.exists.IndicesExistsAliasesRequestBuilder; +import org.elasticsearch.action.admin.indices.alias.exists.IndicesExistsAliasesResponse; import org.elasticsearch.action.admin.indices.alias.get.IndicesGetAliasesRequest; import org.elasticsearch.action.admin.indices.alias.get.IndicesGetAliasesRequestBuilder; import org.elasticsearch.action.admin.indices.alias.get.IndicesGetAliasesResponse; @@ -480,14 +482,14 @@ public interface IndicesAdminClient { IndicesAliasesRequestBuilder prepareAliases(); /** - * Get specific index aliases. + * Get specific index aliases that exists in particular indices and / or by name. * * @param request The result future */ ActionFuture getAliases(IndicesGetAliasesRequest request); /** - * Get specific index aliases. + * Get specific index aliases that exists in particular indices and / or by name. * * @param request The index aliases request * @param listener A listener to be notified with a result @@ -495,10 +497,30 @@ public interface IndicesAdminClient { void getAliases(IndicesGetAliasesRequest request, ActionListener listener); /** - * Adds specific index aliases. + * Get specific index aliases that exists in particular indices and / or by name. */ IndicesGetAliasesRequestBuilder prepareGetAliases(String... aliases); + /** + * Allows to check to existence of aliases from indices. + */ + IndicesExistsAliasesRequestBuilder prepareExistsAliases(String... aliases); + + /** + * Check to existence of index aliases. + * + * @param request The result future + */ + ActionFuture existsAliases(IndicesGetAliasesRequest request); + + /** + * Check the existence of specified index aliases. + * + * @param request The index aliases request + * @param listener A listener to be notified with a result + */ + void existsAliases(IndicesGetAliasesRequest request, ActionListener listener); + /** * Clear indices cache. * diff --git a/src/main/java/org/elasticsearch/client/support/AbstractIndicesAdminClient.java b/src/main/java/org/elasticsearch/client/support/AbstractIndicesAdminClient.java index 7a6b05617d5..d9ca26640bb 100644 --- a/src/main/java/org/elasticsearch/client/support/AbstractIndicesAdminClient.java +++ b/src/main/java/org/elasticsearch/client/support/AbstractIndicesAdminClient.java @@ -25,6 +25,9 @@ import org.elasticsearch.action.admin.indices.alias.IndicesAliasesAction; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequestBuilder; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse; +import org.elasticsearch.action.admin.indices.alias.exists.IndicesExistsAliasesAction; +import org.elasticsearch.action.admin.indices.alias.exists.IndicesExistsAliasesRequestBuilder; +import org.elasticsearch.action.admin.indices.alias.exists.IndicesExistsAliasesResponse; import org.elasticsearch.action.admin.indices.alias.get.IndicesGetAliasesAction; import org.elasticsearch.action.admin.indices.alias.get.IndicesGetAliasesRequest; import org.elasticsearch.action.admin.indices.alias.get.IndicesGetAliasesRequestBuilder; @@ -199,6 +202,21 @@ public abstract class AbstractIndicesAdminClient implements InternalIndicesAdmin return execute(ClearIndicesCacheAction.INSTANCE, request); } + @Override + public void existsAliases(IndicesGetAliasesRequest request, ActionListener listener) { + execute(IndicesExistsAliasesAction.INSTANCE, request, listener); + } + + @Override + public ActionFuture existsAliases(IndicesGetAliasesRequest request) { + return execute(IndicesExistsAliasesAction.INSTANCE, request); + } + + @Override + public IndicesExistsAliasesRequestBuilder prepareExistsAliases(String... aliases) { + return new IndicesExistsAliasesRequestBuilder(this, aliases); + } + @Override public void clearCache(final ClearIndicesCacheRequest request, final ActionListener listener) { execute(ClearIndicesCacheAction.INSTANCE, request, listener); diff --git a/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java b/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java index 1ae798bdc9f..10549934107 100644 --- a/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java +++ b/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java @@ -293,8 +293,8 @@ public class MetaData implements Iterable { IndexMetaData indexMetaData = indices.get(index); Collection filteredValues = Maps.filterKeys(indexMetaData.getAliases(), new Predicate() { public boolean apply(String alias) { - // Simon says: we could build and FST out of the alias key and then run a regexp query against it ;) - return Regex.simpleMatch(aliases, alias); + // Simon says: we could build and FST out of the alias key and then run a regexp query against it ;) + return Regex.simpleMatch(aliases, alias); } }).values(); if (!filteredValues.isEmpty()) { @@ -304,6 +304,37 @@ public class MetaData implements Iterable { return mapBuilder.build(); } + /** + * Checks if at least one of the specified aliases exists in the specified concrete indices. Wildcards are supported in the + * alias names for partial matches. + * + * @param aliases The names of the index aliases to find + * @param concreteIndices The concrete indexes the index aliases must point to order to be returned. + * + * @return whether at least one of the specified aliases exists in one of the specified concrete indices. + */ + public boolean hasAliases(final String[] aliases, String[] concreteIndices) { + assert aliases != null; + assert concreteIndices != null; + if (concreteIndices.length == 0) { + return false; + } + + Sets.SetView intersection = Sets.intersection(Sets.newHashSet(concreteIndices), indices.keySet()); + for (String index : intersection) { + IndexMetaData indexMetaData = indices.get(index); + Collection filteredValues = Maps.filterKeys(indexMetaData.getAliases(), new Predicate() { + public boolean apply(String alias) { + return Regex.simpleMatch(aliases, alias); + } + }).values(); + if (!filteredValues.isEmpty()) { + return true; + } + } + return false; + } + /** * Returns all the concrete indices. */ diff --git a/src/main/java/org/elasticsearch/rest/action/RestActionModule.java b/src/main/java/org/elasticsearch/rest/action/RestActionModule.java index cab9a58581c..86ab53676dc 100644 --- a/src/main/java/org/elasticsearch/rest/action/RestActionModule.java +++ b/src/main/java/org/elasticsearch/rest/action/RestActionModule.java @@ -37,6 +37,7 @@ import org.elasticsearch.rest.action.admin.indices.alias.RestGetIndicesAliasesAc import org.elasticsearch.rest.action.admin.indices.alias.RestIndicesAliasesAction; import org.elasticsearch.rest.action.admin.indices.alias.delete.RestIndexDeleteAliasesAction; import org.elasticsearch.rest.action.admin.indices.alias.get.RestIndicesGetAliasesAction; +import org.elasticsearch.rest.action.admin.indices.alias.head.RestIndicesHeadAliasesAction; import org.elasticsearch.rest.action.admin.indices.alias.put.RestIndexPutAliasAction; import org.elasticsearch.rest.action.admin.indices.analyze.RestAnalyzeAction; import org.elasticsearch.rest.action.admin.indices.cache.clear.RestClearIndicesCacheAction; @@ -123,6 +124,7 @@ public class RestActionModule extends AbstractModule { bind(RestIndicesSegmentsAction.class).asEagerSingleton(); bind(RestGetIndicesAliasesAction.class).asEagerSingleton(); bind(RestIndicesGetAliasesAction.class).asEagerSingleton(); + bind(RestIndicesHeadAliasesAction.class).asEagerSingleton(); bind(RestIndexDeleteAliasesAction.class).asEagerSingleton(); bind(RestIndexPutAliasAction.class).asEagerSingleton(); bind(RestIndicesAliasesAction.class).asEagerSingleton(); diff --git a/src/main/java/org/elasticsearch/rest/action/admin/indices/alias/head/RestIndicesHeadAliasesAction.java b/src/main/java/org/elasticsearch/rest/action/admin/indices/alias/head/RestIndicesHeadAliasesAction.java new file mode 100644 index 00000000000..06b33e7b6e8 --- /dev/null +++ b/src/main/java/org/elasticsearch/rest/action/admin/indices/alias/head/RestIndicesHeadAliasesAction.java @@ -0,0 +1,85 @@ +/* + * Licensed to ElasticSearch and Shay Banon 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.rest.action.admin.indices.alias.head; + +import org.elasticsearch.ExceptionsHelper; +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.admin.indices.alias.exists.IndicesExistsAliasesResponse; +import org.elasticsearch.action.admin.indices.alias.get.IndicesGetAliasesRequest; +import org.elasticsearch.action.support.IgnoreIndices; +import org.elasticsearch.client.Client; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.rest.*; +import org.elasticsearch.rest.action.support.RestActions; + +import static org.elasticsearch.rest.RestRequest.Method.HEAD; +import static org.elasticsearch.rest.RestStatus.NOT_FOUND; +import static org.elasticsearch.rest.RestStatus.OK; + +/** + */ +public class RestIndicesHeadAliasesAction extends BaseRestHandler { + + @Inject + public RestIndicesHeadAliasesAction(Settings settings, Client client, RestController controller) { + super(settings, client); + controller.registerHandler(HEAD, "/_alias/{name}", this); + controller.registerHandler(HEAD, "/{index}/_alias/{name}", this); + } + + @Override + public void handleRequest(final RestRequest request, final RestChannel channel) { + String[] aliases = request.paramAsStringArray("name", Strings.EMPTY_ARRAY); + final String[] indices = RestActions.splitIndices(request.param("index")); + IndicesGetAliasesRequest getAliasesRequest = new IndicesGetAliasesRequest(aliases); + getAliasesRequest.indices(indices); + + if (request.hasParam("ignore_indices")) { + getAliasesRequest.ignoreIndices(IgnoreIndices.fromString(request.param("ignore_indices"))); + } + + client.admin().indices().existsAliases(getAliasesRequest, new ActionListener() { + + @Override + public void onResponse(IndicesExistsAliasesResponse response) { + try { + if (response.isExists()) { + channel.sendResponse(new StringRestResponse(OK)); + } else { + channel.sendResponse(new StringRestResponse(NOT_FOUND)); + } + } catch (Throwable e) { + onFailure(e); + } + } + + @Override + public void onFailure(Throwable e) { + try { + channel.sendResponse(new StringRestResponse(ExceptionsHelper.status(e))); + } catch (Exception e1) { + logger.error("Failed to send failure response", e1); + } + } + }); + } +} diff --git a/src/test/java/org/elasticsearch/test/integration/aliases/IndexAliasesTests.java b/src/test/java/org/elasticsearch/test/integration/aliases/IndexAliasesTests.java index 487741f9aeb..1327c2f8682 100644 --- a/src/test/java/org/elasticsearch/test/integration/aliases/IndexAliasesTests.java +++ b/src/test/java/org/elasticsearch/test/integration/aliases/IndexAliasesTests.java @@ -22,6 +22,7 @@ package org.elasticsearch.test.integration.aliases; import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse; +import org.elasticsearch.action.admin.indices.alias.exists.IndicesExistsAliasesResponse; import org.elasticsearch.action.admin.indices.alias.get.IndicesGetAliasesResponse; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchResponse; @@ -655,31 +656,35 @@ public class IndexAliasesTests extends AbstractNodesTests { assertThat(indicesAliasesResponse.isAcknowledged(), equalTo(true)); logger.info("--> getting alias1"); - IndicesGetAliasesResponse response = client2.admin().indices().prepareGetAliases("alias1").execute().actionGet(); - assertThat(response, notNullValue()); - assertThat(response.getAliases().size(), equalTo(1)); - assertThat(response.getAliases().get("foobar").size(), equalTo(1)); - assertThat(response.getAliases().get("foobar").get(0), notNullValue()); - assertThat(response.getAliases().get("foobar").get(0).alias(), equalTo("alias1")); - assertThat(response.getAliases().get("foobar").get(0).getFilter(), nullValue()); - assertThat(response.getAliases().get("foobar").get(0).getIndexRouting(), nullValue()); - assertThat(response.getAliases().get("foobar").get(0).getSearchRouting(), nullValue()); + IndicesGetAliasesResponse getResponse = client2.admin().indices().prepareGetAliases("alias1").execute().actionGet(); + assertThat(getResponse, notNullValue()); + assertThat(getResponse.getAliases().size(), equalTo(1)); + assertThat(getResponse.getAliases().get("foobar").size(), equalTo(1)); + assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("alias1")); + assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue()); + IndicesExistsAliasesResponse existsResponse = client2.admin().indices().prepareExistsAliases("alias1").execute().actionGet(); + assertThat(existsResponse.exists(), equalTo(true)); logger.info("--> getting all aliases that start with alias*"); - response = client2.admin().indices().prepareGetAliases("alias*").execute().actionGet(); - assertThat(response, notNullValue()); - assertThat(response.getAliases().size(), equalTo(1)); - assertThat(response.getAliases().get("foobar").size(), equalTo(2)); - assertThat(response.getAliases().get("foobar").get(0), notNullValue()); - assertThat(response.getAliases().get("foobar").get(0).alias(), equalTo("alias2")); - assertThat(response.getAliases().get("foobar").get(0).getFilter(), nullValue()); - assertThat(response.getAliases().get("foobar").get(0).getIndexRouting(), nullValue()); - assertThat(response.getAliases().get("foobar").get(0).getSearchRouting(), nullValue()); - assertThat(response.getAliases().get("foobar").get(1), notNullValue()); - assertThat(response.getAliases().get("foobar").get(1).alias(), equalTo("alias1")); - assertThat(response.getAliases().get("foobar").get(1).getFilter(), nullValue()); - assertThat(response.getAliases().get("foobar").get(1).getIndexRouting(), nullValue()); - assertThat(response.getAliases().get("foobar").get(1).getSearchRouting(), nullValue()); + getResponse = client2.admin().indices().prepareGetAliases("alias*").execute().actionGet(); + assertThat(getResponse, notNullValue()); + assertThat(getResponse.getAliases().size(), equalTo(1)); + assertThat(getResponse.getAliases().get("foobar").size(), equalTo(2)); + assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("alias2")); + assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue()); + assertThat(getResponse.getAliases().get("foobar").get(1), notNullValue()); + assertThat(getResponse.getAliases().get("foobar").get(1).alias(), equalTo("alias1")); + assertThat(getResponse.getAliases().get("foobar").get(1).getFilter(), nullValue()); + assertThat(getResponse.getAliases().get("foobar").get(1).getIndexRouting(), nullValue()); + assertThat(getResponse.getAliases().get("foobar").get(1).getSearchRouting(), nullValue()); + existsResponse = client2.admin().indices().prepareExistsAliases("alias*").execute().actionGet(); + assertThat(existsResponse.exists(), equalTo(true)); logger.info("--> creating aliases [bar, baz, foo]"); client1.admin().indices().prepareAliases() @@ -694,103 +699,124 @@ public class IndexAliasesTests extends AbstractNodesTests { assertThat(indicesAliasesResponse.isAcknowledged(), equalTo(true)); logger.info("--> getting bar and baz for index bazbar"); - response = client2.admin().indices().prepareGetAliases("bar", "bac").addIndices("bazbar").execute().actionGet(); - assertThat(response, notNullValue()); - assertThat(response.getAliases().size(), equalTo(1)); - assertThat(response.getAliases().get("bazbar").size(), equalTo(2)); - assertThat(response.getAliases().get("bazbar").get(0), notNullValue()); - assertThat(response.getAliases().get("bazbar").get(0).alias(), equalTo("bac")); - assertThat(response.getAliases().get("bazbar").get(0).getFilter().string(), containsString("term")); - assertThat(response.getAliases().get("bazbar").get(0).getFilter().string(), containsString("field")); - assertThat(response.getAliases().get("bazbar").get(0).getFilter().string(), containsString("value")); - assertThat(response.getAliases().get("bazbar").get(0).getIndexRouting(), nullValue()); - assertThat(response.getAliases().get("bazbar").get(0).getSearchRouting(), nullValue()); - assertThat(response.getAliases().get("bazbar").get(1), notNullValue()); - assertThat(response.getAliases().get("bazbar").get(1).alias(), equalTo("bar")); - assertThat(response.getAliases().get("bazbar").get(1).getFilter(), nullValue()); - assertThat(response.getAliases().get("bazbar").get(1).getIndexRouting(), nullValue()); - assertThat(response.getAliases().get("bazbar").get(1).getSearchRouting(), nullValue()); + getResponse = client2.admin().indices().prepareGetAliases("bar", "bac").addIndices("bazbar").execute().actionGet(); + assertThat(getResponse, notNullValue()); + assertThat(getResponse.getAliases().size(), equalTo(1)); + assertThat(getResponse.getAliases().get("bazbar").size(), equalTo(2)); + assertThat(getResponse.getAliases().get("bazbar").get(0), notNullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(0).alias(), equalTo("bac")); + assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("term")); + assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("field")); + assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("value")); + assertThat(getResponse.getAliases().get("bazbar").get(0).getIndexRouting(), nullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(0).getSearchRouting(), nullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(1), notNullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(1).alias(), equalTo("bar")); + assertThat(getResponse.getAliases().get("bazbar").get(1).getFilter(), nullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(1).getIndexRouting(), nullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(1).getSearchRouting(), nullValue()); + existsResponse = client2.admin().indices().prepareExistsAliases("bar", "bac") + .addIndices("bazbar").execute().actionGet(); + assertThat(existsResponse.exists(), equalTo(true)); logger.info("--> getting *b* for index baz*"); - response = client2.admin().indices().prepareGetAliases("*b*").addIndices("baz*").execute().actionGet(); - assertThat(response, notNullValue()); - assertThat(response.getAliases().size(), equalTo(1)); - assertThat(response.getAliases().get("bazbar").size(), equalTo(2)); - assertThat(response.getAliases().get("bazbar").get(0), notNullValue()); - assertThat(response.getAliases().get("bazbar").get(0).alias(), equalTo("bac")); - assertThat(response.getAliases().get("bazbar").get(0).getFilter().string(), containsString("term")); - assertThat(response.getAliases().get("bazbar").get(0).getFilter().string(), containsString("field")); - assertThat(response.getAliases().get("bazbar").get(0).getFilter().string(), containsString("value")); - assertThat(response.getAliases().get("bazbar").get(0).getIndexRouting(), nullValue()); - assertThat(response.getAliases().get("bazbar").get(0).getSearchRouting(), nullValue()); - assertThat(response.getAliases().get("bazbar").get(1), notNullValue()); - assertThat(response.getAliases().get("bazbar").get(1).alias(), equalTo("bar")); - assertThat(response.getAliases().get("bazbar").get(1).getFilter(), nullValue()); - assertThat(response.getAliases().get("bazbar").get(1).getIndexRouting(), nullValue()); - assertThat(response.getAliases().get("bazbar").get(1).getSearchRouting(), nullValue()); + getResponse = client2.admin().indices().prepareGetAliases("*b*").addIndices("baz*").execute().actionGet(); + assertThat(getResponse, notNullValue()); + assertThat(getResponse.getAliases().size(), equalTo(1)); + assertThat(getResponse.getAliases().get("bazbar").size(), equalTo(2)); + assertThat(getResponse.getAliases().get("bazbar").get(0), notNullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(0).alias(), equalTo("bac")); + assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("term")); + assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("field")); + assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("value")); + assertThat(getResponse.getAliases().get("bazbar").get(0).getIndexRouting(), nullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(0).getSearchRouting(), nullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(1), notNullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(1).alias(), equalTo("bar")); + assertThat(getResponse.getAliases().get("bazbar").get(1).getFilter(), nullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(1).getIndexRouting(), nullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(1).getSearchRouting(), nullValue()); + existsResponse = client2.admin().indices().prepareExistsAliases("*b*") + .addIndices("baz*").execute().actionGet(); + assertThat(existsResponse.exists(), equalTo(true)); logger.info("--> getting *b* for index *bar"); - response = client2.admin().indices().prepareGetAliases("b*").addIndices("*bar").execute().actionGet(); - assertThat(response, notNullValue()); - assertThat(response.getAliases().size(), equalTo(2)); - assertThat(response.getAliases().get("bazbar").size(), equalTo(2)); - assertThat(response.getAliases().get("bazbar").get(0), notNullValue()); - assertThat(response.getAliases().get("bazbar").get(0).alias(), equalTo("bac")); - assertThat(response.getAliases().get("bazbar").get(0).getFilter().string(), containsString("term")); - assertThat(response.getAliases().get("bazbar").get(0).getFilter().string(), containsString("field")); - assertThat(response.getAliases().get("bazbar").get(0).getFilter().string(), containsString("value")); - assertThat(response.getAliases().get("bazbar").get(0).getIndexRouting(), nullValue()); - assertThat(response.getAliases().get("bazbar").get(0).getSearchRouting(), nullValue()); - assertThat(response.getAliases().get("bazbar").get(1), notNullValue()); - assertThat(response.getAliases().get("bazbar").get(1).alias(), equalTo("bar")); - assertThat(response.getAliases().get("bazbar").get(1).getFilter(), nullValue()); - assertThat(response.getAliases().get("bazbar").get(1).getIndexRouting(), nullValue()); - assertThat(response.getAliases().get("bazbar").get(1).getSearchRouting(), nullValue()); - assertThat(response.getAliases().get("foobar").get(0), notNullValue()); - assertThat(response.getAliases().get("foobar").get(0).alias(), equalTo("bac")); - assertThat(response.getAliases().get("foobar").get(0).getFilter(), nullValue()); - assertThat(response.getAliases().get("foobar").get(0).getIndexRouting(), equalTo("bla")); - assertThat(response.getAliases().get("foobar").get(0).getSearchRouting(), equalTo("bla")); + getResponse = client2.admin().indices().prepareGetAliases("b*").addIndices("*bar").execute().actionGet(); + assertThat(getResponse, notNullValue()); + assertThat(getResponse.getAliases().size(), equalTo(2)); + assertThat(getResponse.getAliases().get("bazbar").size(), equalTo(2)); + assertThat(getResponse.getAliases().get("bazbar").get(0), notNullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(0).alias(), equalTo("bac")); + assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("term")); + assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("field")); + assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("value")); + assertThat(getResponse.getAliases().get("bazbar").get(0).getIndexRouting(), nullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(0).getSearchRouting(), nullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(1), notNullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(1).alias(), equalTo("bar")); + assertThat(getResponse.getAliases().get("bazbar").get(1).getFilter(), nullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(1).getIndexRouting(), nullValue()); + assertThat(getResponse.getAliases().get("bazbar").get(1).getSearchRouting(), nullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("bac")); + assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), equalTo("bla")); + assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), equalTo("bla")); + existsResponse = client2.admin().indices().prepareExistsAliases("b*") + .addIndices("*bar").execute().actionGet(); + assertThat(existsResponse.exists(), equalTo(true)); logger.info("--> getting f* for index *bar"); - response = client2.admin().indices().prepareGetAliases("f*").addIndices("*bar").execute().actionGet(); - assertThat(response, notNullValue()); - assertThat(response.getAliases().size(), equalTo(1)); - assertThat(response.getAliases().get("foobar").get(0), notNullValue()); - assertThat(response.getAliases().get("foobar").get(0).alias(), equalTo("foo")); - assertThat(response.getAliases().get("foobar").get(0).getFilter(), nullValue()); - assertThat(response.getAliases().get("foobar").get(0).getIndexRouting(), nullValue()); - assertThat(response.getAliases().get("foobar").get(0).getSearchRouting(), nullValue()); + getResponse = client2.admin().indices().prepareGetAliases("f*").addIndices("*bar").execute().actionGet(); + assertThat(getResponse, notNullValue()); + assertThat(getResponse.getAliases().size(), equalTo(1)); + assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("foo")); + assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue()); + existsResponse = client2.admin().indices().prepareExistsAliases("f*") + .addIndices("*bar").execute().actionGet(); + assertThat(existsResponse.exists(), equalTo(true)); // alias at work logger.info("--> getting f* for index *bac"); - response = client2.admin().indices().prepareGetAliases("foo").addIndices("*bac").execute().actionGet(); - assertThat(response, notNullValue()); - assertThat(response.getAliases().size(), equalTo(1)); - assertThat(response.getAliases().get("foobar").size(), equalTo(1)); - assertThat(response.getAliases().get("foobar").get(0), notNullValue()); - assertThat(response.getAliases().get("foobar").get(0).alias(), equalTo("foo")); - assertThat(response.getAliases().get("foobar").get(0).getFilter(), nullValue()); - assertThat(response.getAliases().get("foobar").get(0).getIndexRouting(), nullValue()); - assertThat(response.getAliases().get("foobar").get(0).getSearchRouting(), nullValue()); + getResponse = client2.admin().indices().prepareGetAliases("foo").addIndices("*bac").execute().actionGet(); + assertThat(getResponse, notNullValue()); + assertThat(getResponse.getAliases().size(), equalTo(1)); + assertThat(getResponse.getAliases().get("foobar").size(), equalTo(1)); + assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("foo")); + assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue()); + existsResponse = client2.admin().indices().prepareExistsAliases("foo") + .addIndices("*bac").execute().actionGet(); + assertThat(existsResponse.exists(), equalTo(true)); logger.info("--> getting foo for index foobar"); - response = client2.admin().indices().prepareGetAliases("foo").addIndices("foobar").execute().actionGet(); - assertThat(response, notNullValue()); - assertThat(response.getAliases().size(), equalTo(1)); - assertThat(response.getAliases().get("foobar").get(0), notNullValue()); - assertThat(response.getAliases().get("foobar").get(0).alias(), equalTo("foo")); - assertThat(response.getAliases().get("foobar").get(0).getFilter(), nullValue()); - assertThat(response.getAliases().get("foobar").get(0).getIndexRouting(), nullValue()); - assertThat(response.getAliases().get("foobar").get(0).getSearchRouting(), nullValue()); + getResponse = client2.admin().indices().prepareGetAliases("foo").addIndices("foobar").execute().actionGet(); + assertThat(getResponse, notNullValue()); + assertThat(getResponse.getAliases().size(), equalTo(1)); + assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("foo")); + assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue()); + assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue()); + existsResponse = client2.admin().indices().prepareExistsAliases("foo") + .addIndices("foobar").execute().actionGet(); + assertThat(existsResponse.exists(), equalTo(true)); // alias at work again logger.info("--> getting * for index *bac"); - response = client2.admin().indices().prepareGetAliases("*").addIndices("*bac").execute().actionGet(); - assertThat(response, notNullValue()); - assertThat(response.getAliases().size(), equalTo(2)); - assertThat(response.getAliases().get("foobar").size(), equalTo(4)); - assertThat(response.getAliases().get("bazbar").size(), equalTo(2)); + getResponse = client2.admin().indices().prepareGetAliases("*").addIndices("*bac").execute().actionGet(); + assertThat(getResponse, notNullValue()); + assertThat(getResponse.getAliases().size(), equalTo(2)); + assertThat(getResponse.getAliases().get("foobar").size(), equalTo(4)); + assertThat(getResponse.getAliases().get("bazbar").size(), equalTo(2)); + existsResponse = client2.admin().indices().prepareExistsAliases("*") + .addIndices("*bac").execute().actionGet(); + assertThat(existsResponse.exists(), equalTo(true)); indicesAliasesResponse = client2.admin().indices().prepareAliases() .removeAlias("foobar", "foo") @@ -803,6 +829,9 @@ public class IndexAliasesTests extends AbstractNodesTests { } catch (AliasMissingException e) { assertThat(e.getMessage(), equalTo("alias [foo] missing")); } + existsResponse = client2.admin().indices().prepareExistsAliases("foo") + .addIndices("foobar").execute().actionGet(); + assertThat(existsResponse.exists(), equalTo(false)); } private void assertHits(SearchHits hits, String... ids) {