From 2dbe8900224455fde285fdf1649b28ec1c4cba05 Mon Sep 17 00:00:00 2001 From: kimchy Date: Sun, 12 Jun 2011 12:01:29 +0300 Subject: [PATCH] Indices exists API, closes #1022. --- .../action/TransportActionModule.java | 12 ++- .../action/TransportActions.java | 1 + .../indices/exists/IndicesExistsRequest.java | 70 ++++++++++++++++ .../indices/exists/IndicesExistsResponse.java | 55 ++++++++++++ .../exists/TransportIndicesExistsAction.java | 81 ++++++++++++++++++ .../client/IndicesAdminClient.java | 26 ++++++ .../org/elasticsearch/client/Requests.java | 12 +++ .../exists/IndicesExistsRequestBuilder.java | 45 ++++++++++ .../client/node/NodeIndicesAdminClient.java | 16 +++- .../support/AbstractIndicesAdminClient.java | 5 ++ .../action/ClientTransportActionModule.java | 2 + .../ClientTransportIndicesExistsAction.java | 42 ++++++++++ .../InternalTransportIndicesAdminClient.java | 25 +++++- .../rest/action/RestActionModule.java | 2 + .../exists/RestIndicesExistsAction.java | 83 +++++++++++++++++++ .../document/DocumentActionsTests.java | 5 ++ 16 files changed, 479 insertions(+), 3 deletions(-) create mode 100644 modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/exists/IndicesExistsRequest.java create mode 100644 modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/exists/IndicesExistsResponse.java create mode 100644 modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/exists/TransportIndicesExistsAction.java create mode 100644 modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/exists/IndicesExistsRequestBuilder.java create mode 100644 modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/action/admin/indices/exists/ClientTransportIndicesExistsAction.java create mode 100644 modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/exists/RestIndicesExistsAction.java diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/TransportActionModule.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/TransportActionModule.java index 23d9dfca92d..db5406babf7 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/TransportActionModule.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/TransportActionModule.java @@ -36,6 +36,7 @@ import org.elasticsearch.action.admin.indices.cache.clear.TransportClearIndicesC import org.elasticsearch.action.admin.indices.close.TransportCloseIndexAction; import org.elasticsearch.action.admin.indices.create.TransportCreateIndexAction; import org.elasticsearch.action.admin.indices.delete.TransportDeleteIndexAction; +import org.elasticsearch.action.admin.indices.exists.TransportIndicesExistsAction; import org.elasticsearch.action.admin.indices.flush.TransportFlushAction; import org.elasticsearch.action.admin.indices.gateway.snapshot.TransportGatewaySnapshotAction; import org.elasticsearch.action.admin.indices.mapping.delete.TransportDeleteMappingAction; @@ -62,7 +63,15 @@ import org.elasticsearch.action.mlt.TransportMoreLikeThisAction; import org.elasticsearch.action.percolate.TransportPercolateAction; import org.elasticsearch.action.search.TransportSearchAction; import org.elasticsearch.action.search.TransportSearchScrollAction; -import org.elasticsearch.action.search.type.*; +import org.elasticsearch.action.search.type.TransportSearchCache; +import org.elasticsearch.action.search.type.TransportSearchDfsQueryAndFetchAction; +import org.elasticsearch.action.search.type.TransportSearchDfsQueryThenFetchAction; +import org.elasticsearch.action.search.type.TransportSearchQueryAndFetchAction; +import org.elasticsearch.action.search.type.TransportSearchQueryThenFetchAction; +import org.elasticsearch.action.search.type.TransportSearchScanAction; +import org.elasticsearch.action.search.type.TransportSearchScrollQueryAndFetchAction; +import org.elasticsearch.action.search.type.TransportSearchScrollQueryThenFetchAction; +import org.elasticsearch.action.search.type.TransportSearchScrollScanAction; import org.elasticsearch.common.inject.AbstractModule; /** @@ -90,6 +99,7 @@ public class TransportActionModule extends AbstractModule { bind(TransportDeleteIndexAction.class).asEagerSingleton(); bind(TransportOpenIndexAction.class).asEagerSingleton(); bind(TransportCloseIndexAction.class).asEagerSingleton(); + bind(TransportIndicesExistsAction.class).asEagerSingleton(); bind(TransportPutMappingAction.class).asEagerSingleton(); bind(TransportDeleteMappingAction.class).asEagerSingleton(); bind(TransportIndicesAliasesAction.class).asEagerSingleton(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/TransportActions.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/TransportActions.java index fb1484e8add..c1056f6b9b0 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/TransportActions.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/TransportActions.java @@ -57,6 +57,7 @@ public class TransportActions { public static final String REFRESH = "indices/refresh"; public static final String OPTIMIZE = "indices/optimize"; public static final String STATUS = "indices/status"; + public static final String EXISTS = "indices/exists"; public static final String ALIASES = "indices/aliases"; public static final String UPDATE_SETTINGS = "indices/updateSettings"; public static final String ANALYZE = "indices/analyze"; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/exists/IndicesExistsRequest.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/exists/IndicesExistsRequest.java new file mode 100644 index 00000000000..7bf87d96dce --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/exists/IndicesExistsRequest.java @@ -0,0 +1,70 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search 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.exists; + +import org.elasticsearch.action.ActionRequestValidationException; +import org.elasticsearch.action.support.master.MasterNodeOperationRequest; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; + +import java.io.IOException; + +import static org.elasticsearch.action.Actions.*; + +public class IndicesExistsRequest extends MasterNodeOperationRequest { + + private String[] indices; + + public IndicesExistsRequest(String... indices) { + this.indices = indices; + } + + public String[] indices() { + return indices; + } + + public void indices(String[] indices) { + this.indices = indices; + } + + @Override public ActionRequestValidationException validate() { + ActionRequestValidationException validationException = null; + if (indices == null || indices.length == 0) { + validationException = addValidationError("index/indices is missing", validationException); + } + return validationException; + } + + @Override public void readFrom(StreamInput in) throws IOException { + super.readFrom(in); + indices = new String[in.readVInt()]; + for (int i = 0; i < indices.length; i++) { + indices[i] = in.readUTF(); + } + } + + @Override public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + out.writeVInt(indices.length); + for (String index : indices) { + out.writeUTF(index); + } + } +} \ No newline at end of file diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/exists/IndicesExistsResponse.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/exists/IndicesExistsResponse.java new file mode 100644 index 00000000000..c979ea715a7 --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/exists/IndicesExistsResponse.java @@ -0,0 +1,55 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search 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.exists; + +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.io.stream.Streamable; + +import java.io.IOException; + +public class IndicesExistsResponse implements ActionResponse, Streamable { + + private boolean exists; + + IndicesExistsResponse() { + } + + public IndicesExistsResponse(boolean exists) { + this.exists = exists; + } + + public boolean exists() { + return this.exists; + } + + public boolean isExists() { + return exists(); + } + + @Override public void readFrom(StreamInput in) throws IOException { + exists = in.readBoolean(); + } + + @Override public void writeTo(StreamOutput out) throws IOException { + out.writeBoolean(exists); + } +} \ No newline at end of file diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/exists/TransportIndicesExistsAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/exists/TransportIndicesExistsAction.java new file mode 100644 index 00000000000..c57e08d15f4 --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/exists/TransportIndicesExistsAction.java @@ -0,0 +1,81 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search 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.exists; + +import org.elasticsearch.ElasticSearchException; +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.TransportActions; +import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction; +import org.elasticsearch.cluster.ClusterService; +import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.block.ClusterBlockException; +import org.elasticsearch.cluster.block.ClusterBlockLevel; +import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.transport.TransportService; + +/** + * Indices exists action. + * + * @author kimchy (shay.banon) + */ +public class TransportIndicesExistsAction extends TransportMasterNodeOperationAction { + + @Inject public TransportIndicesExistsAction(Settings settings, TransportService transportService, ClusterService clusterService, + ThreadPool threadPool) { + super(settings, transportService, clusterService, threadPool); + } + + @Override protected String executor() { + return ThreadPool.Names.CACHED; + } + + @Override protected String transportAction() { + return TransportActions.Admin.Indices.CLOSE; + } + + @Override protected IndicesExistsRequest newRequest() { + return new IndicesExistsRequest(); + } + + @Override protected IndicesExistsResponse newResponse() { + return new IndicesExistsResponse(); + } + + @Override protected void doExecute(IndicesExistsRequest request, ActionListener listener) { + request.indices(clusterService.state().metaData().concreteIndices(request.indices())); + super.doExecute(request, listener); + } + + @Override protected ClusterBlockException checkBlock(IndicesExistsRequest request, ClusterState state) { + return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA, request.indices()); + } + + @Override protected IndicesExistsResponse masterOperation(IndicesExistsRequest request, ClusterState state) throws ElasticSearchException { + boolean exists = true; + for (String index : request.indices()) { + if (!state.metaData().hasConcreteIndex(index)) { + exists = false; + } + } + return new IndicesExistsResponse(exists); + } +} diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/IndicesAdminClient.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/IndicesAdminClient.java index 8c44ae9f528..6aa7131a3ad 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/client/IndicesAdminClient.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/IndicesAdminClient.java @@ -33,6 +33,8 @@ import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; +import org.elasticsearch.action.admin.indices.exists.IndicesExistsRequest; +import org.elasticsearch.action.admin.indices.exists.IndicesExistsResponse; import org.elasticsearch.action.admin.indices.flush.FlushRequest; import org.elasticsearch.action.admin.indices.flush.FlushResponse; import org.elasticsearch.action.admin.indices.gateway.snapshot.GatewaySnapshotRequest; @@ -61,6 +63,7 @@ import org.elasticsearch.client.action.admin.indices.cache.clear.ClearIndicesCac import org.elasticsearch.client.action.admin.indices.close.CloseIndexRequestBuilder; import org.elasticsearch.client.action.admin.indices.create.CreateIndexRequestBuilder; import org.elasticsearch.client.action.admin.indices.delete.DeleteIndexRequestBuilder; +import org.elasticsearch.client.action.admin.indices.exists.IndicesExistsRequestBuilder; import org.elasticsearch.client.action.admin.indices.flush.FlushRequestBuilder; import org.elasticsearch.client.action.admin.indices.gateway.snapshot.GatewaySnapshotRequestBuilder; import org.elasticsearch.client.action.admin.indices.mapping.delete.DeleteMappingRequestBuilder; @@ -81,6 +84,29 @@ import org.elasticsearch.client.action.admin.indices.template.put.PutIndexTempla */ public interface IndicesAdminClient { + /** + * Indices Exists. + * + * @param request The indices exists request + * @return The result future + * @see Requests#indicesExistsRequest(String...) + */ + ActionFuture exists(IndicesExistsRequest request); + + /** + * The status of one or more indices. + * + * @param request The indices status request + * @param listener A listener to be notified with a result + * @see Requests#indicesExistsRequest(String...) + */ + void exists(IndicesExistsRequest request, ActionListener listener); + + /** + * Indices exists. + */ + IndicesExistsRequestBuilder prepareExists(String... indices); + /** * The status of one or more indices. * diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/Requests.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/Requests.java index 7185d232634..c0e046c08ac 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/client/Requests.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/Requests.java @@ -33,6 +33,7 @@ import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheReque import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; +import org.elasticsearch.action.admin.indices.exists.IndicesExistsRequest; import org.elasticsearch.action.admin.indices.flush.FlushRequest; import org.elasticsearch.action.admin.indices.gateway.snapshot.GatewaySnapshotRequest; import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest; @@ -187,6 +188,17 @@ public class Requests { return new IndicesStatusRequest(indices); } + /** + * Creates an indices exists request. + * + * @param indices The indices to check if they exists or not. + * @return The indices exists request + * @see org.elasticsearch.client.IndicesAdminClient#exists(org.elasticsearch.action.admin.indices.exists.IndicesExistsRequest) + */ + public static IndicesExistsRequest indicesExistsRequest(String... indices) { + return new IndicesExistsRequest(indices); + } + /** * Creates a create index request. * diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/exists/IndicesExistsRequestBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/exists/IndicesExistsRequestBuilder.java new file mode 100644 index 00000000000..d0733cbc3e7 --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/exists/IndicesExistsRequestBuilder.java @@ -0,0 +1,45 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search 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.action.admin.indices.exists; + +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.admin.indices.exists.IndicesExistsRequest; +import org.elasticsearch.action.admin.indices.exists.IndicesExistsResponse; +import org.elasticsearch.client.IndicesAdminClient; +import org.elasticsearch.client.action.admin.indices.support.BaseIndicesRequestBuilder; + +/** + * @author kimchy (shay.banon) + */ +public class IndicesExistsRequestBuilder extends BaseIndicesRequestBuilder { + + public IndicesExistsRequestBuilder(IndicesAdminClient indicesClient, String... indices) { + super(indicesClient, new IndicesExistsRequest(indices)); + } + + public IndicesExistsRequestBuilder setIndices(String... indices) { + request.indices(indices); + return this; + } + + @Override protected void doExecute(ActionListener listener) { + client.exists(request, listener); + } +} diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/node/NodeIndicesAdminClient.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/node/NodeIndicesAdminClient.java index be938a6d9fa..6d16af48dba 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/client/node/NodeIndicesAdminClient.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/node/NodeIndicesAdminClient.java @@ -39,6 +39,9 @@ import org.elasticsearch.action.admin.indices.create.TransportCreateIndexAction; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; import org.elasticsearch.action.admin.indices.delete.TransportDeleteIndexAction; +import org.elasticsearch.action.admin.indices.exists.IndicesExistsRequest; +import org.elasticsearch.action.admin.indices.exists.IndicesExistsResponse; +import org.elasticsearch.action.admin.indices.exists.TransportIndicesExistsAction; import org.elasticsearch.action.admin.indices.flush.FlushRequest; import org.elasticsearch.action.admin.indices.flush.FlushResponse; import org.elasticsearch.action.admin.indices.flush.TransportFlushAction; @@ -85,6 +88,8 @@ public class NodeIndicesAdminClient extends AbstractIndicesAdminClient implement private final ThreadPool threadPool; + private final TransportIndicesExistsAction indicesExistsAction; + private final TransportIndicesStatusAction indicesStatusAction; private final TransportCreateIndexAction createIndexAction; @@ -119,7 +124,7 @@ public class NodeIndicesAdminClient extends AbstractIndicesAdminClient implement private final TransportDeleteIndexTemplateAction deleteIndexTemplateAction; - @Inject public NodeIndicesAdminClient(Settings settings, ThreadPool threadPool, TransportIndicesStatusAction indicesStatusAction, + @Inject public NodeIndicesAdminClient(Settings settings, ThreadPool threadPool, TransportIndicesExistsAction indicesExistsAction, TransportIndicesStatusAction indicesStatusAction, TransportCreateIndexAction createIndexAction, TransportDeleteIndexAction deleteIndexAction, TransportCloseIndexAction closeIndexAction, TransportOpenIndexAction openIndexAction, TransportRefreshAction refreshAction, TransportFlushAction flushAction, TransportOptimizeAction optimizeAction, @@ -128,6 +133,7 @@ public class NodeIndicesAdminClient extends AbstractIndicesAdminClient implement TransportUpdateSettingsAction updateSettingsAction, TransportAnalyzeAction analyzeAction, TransportPutIndexTemplateAction putIndexTemplateAction, TransportDeleteIndexTemplateAction deleteIndexTemplateAction) { this.threadPool = threadPool; + this.indicesExistsAction = indicesExistsAction; this.indicesStatusAction = indicesStatusAction; this.createIndexAction = createIndexAction; this.deleteIndexAction = deleteIndexAction; @@ -151,6 +157,14 @@ public class NodeIndicesAdminClient extends AbstractIndicesAdminClient implement return this.threadPool; } + @Override public ActionFuture exists(IndicesExistsRequest request) { + return indicesExistsAction.execute(request); + } + + @Override public void exists(IndicesExistsRequest request, ActionListener listener) { + indicesExistsAction.execute(request, listener); + } + @Override public ActionFuture status(IndicesStatusRequest request) { return indicesStatusAction.execute(request); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/support/AbstractIndicesAdminClient.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/support/AbstractIndicesAdminClient.java index 616908325f9..ce7fbf32230 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/client/support/AbstractIndicesAdminClient.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/support/AbstractIndicesAdminClient.java @@ -25,6 +25,7 @@ import org.elasticsearch.client.action.admin.indices.cache.clear.ClearIndicesCac import org.elasticsearch.client.action.admin.indices.close.CloseIndexRequestBuilder; import org.elasticsearch.client.action.admin.indices.create.CreateIndexRequestBuilder; import org.elasticsearch.client.action.admin.indices.delete.DeleteIndexRequestBuilder; +import org.elasticsearch.client.action.admin.indices.exists.IndicesExistsRequestBuilder; import org.elasticsearch.client.action.admin.indices.flush.FlushRequestBuilder; import org.elasticsearch.client.action.admin.indices.gateway.snapshot.GatewaySnapshotRequestBuilder; import org.elasticsearch.client.action.admin.indices.mapping.delete.DeleteMappingRequestBuilder; @@ -43,6 +44,10 @@ import org.elasticsearch.client.internal.InternalIndicesAdminClient; */ public abstract class AbstractIndicesAdminClient implements InternalIndicesAdminClient { + @Override public IndicesExistsRequestBuilder prepareExists(String... indices) { + return new IndicesExistsRequestBuilder(this, indices); + } + @Override public IndicesAliasesRequestBuilder prepareAliases() { return new IndicesAliasesRequestBuilder(this); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/action/ClientTransportActionModule.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/action/ClientTransportActionModule.java index 8e2b5e97ec6..d993164884a 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/action/ClientTransportActionModule.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/action/ClientTransportActionModule.java @@ -34,6 +34,7 @@ import org.elasticsearch.client.transport.action.admin.indices.cache.clear.Clien import org.elasticsearch.client.transport.action.admin.indices.close.ClientTransportCloseIndexAction; import org.elasticsearch.client.transport.action.admin.indices.create.ClientTransportCreateIndexAction; import org.elasticsearch.client.transport.action.admin.indices.delete.ClientTransportDeleteIndexAction; +import org.elasticsearch.client.transport.action.admin.indices.exists.ClientTransportIndicesExistsAction; import org.elasticsearch.client.transport.action.admin.indices.flush.ClientTransportFlushAction; import org.elasticsearch.client.transport.action.admin.indices.gateway.snapshot.ClientTransportGatewaySnapshotAction; import org.elasticsearch.client.transport.action.admin.indices.mapping.delete.ClientTransportDeleteMappingAction; @@ -72,6 +73,7 @@ public class ClientTransportActionModule extends AbstractModule { bind(ClientTransportBulkAction.class).asEagerSingleton(); bind(ClientTransportPercolateAction.class).asEagerSingleton(); + bind(ClientTransportIndicesExistsAction.class).asEagerSingleton(); bind(ClientTransportIndicesStatusAction.class).asEagerSingleton(); bind(ClientTransportRefreshAction.class).asEagerSingleton(); bind(ClientTransportFlushAction.class).asEagerSingleton(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/action/admin/indices/exists/ClientTransportIndicesExistsAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/action/admin/indices/exists/ClientTransportIndicesExistsAction.java new file mode 100644 index 00000000000..f60e71d5494 --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/action/admin/indices/exists/ClientTransportIndicesExistsAction.java @@ -0,0 +1,42 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search 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.transport.action.admin.indices.exists; + +import org.elasticsearch.action.TransportActions; +import org.elasticsearch.action.admin.indices.exists.IndicesExistsRequest; +import org.elasticsearch.action.admin.indices.exists.IndicesExistsResponse; +import org.elasticsearch.client.transport.action.support.BaseClientTransportAction; +import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.transport.TransportService; + +/** + * @author kimchy (Shay Banon) + */ +public class ClientTransportIndicesExistsAction extends BaseClientTransportAction { + + @Inject public ClientTransportIndicesExistsAction(Settings settings, TransportService transportService) { + super(settings, transportService, IndicesExistsResponse.class); + } + + @Override protected String action() { + return TransportActions.Admin.Indices.EXISTS; + } +} \ No newline at end of file diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/support/InternalTransportIndicesAdminClient.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/support/InternalTransportIndicesAdminClient.java index d9fcd3b12c0..ae76ce74c99 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/support/InternalTransportIndicesAdminClient.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/support/InternalTransportIndicesAdminClient.java @@ -34,6 +34,8 @@ import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; +import org.elasticsearch.action.admin.indices.exists.IndicesExistsRequest; +import org.elasticsearch.action.admin.indices.exists.IndicesExistsResponse; import org.elasticsearch.action.admin.indices.flush.FlushRequest; import org.elasticsearch.action.admin.indices.flush.FlushResponse; import org.elasticsearch.action.admin.indices.gateway.snapshot.GatewaySnapshotRequest; @@ -65,6 +67,7 @@ import org.elasticsearch.client.transport.action.admin.indices.cache.clear.Clien import org.elasticsearch.client.transport.action.admin.indices.close.ClientTransportCloseIndexAction; import org.elasticsearch.client.transport.action.admin.indices.create.ClientTransportCreateIndexAction; import org.elasticsearch.client.transport.action.admin.indices.delete.ClientTransportDeleteIndexAction; +import org.elasticsearch.client.transport.action.admin.indices.exists.ClientTransportIndicesExistsAction; import org.elasticsearch.client.transport.action.admin.indices.flush.ClientTransportFlushAction; import org.elasticsearch.client.transport.action.admin.indices.gateway.snapshot.ClientTransportGatewaySnapshotAction; import org.elasticsearch.client.transport.action.admin.indices.mapping.delete.ClientTransportDeleteMappingAction; @@ -90,6 +93,8 @@ public class InternalTransportIndicesAdminClient extends AbstractIndicesAdminCli private final ThreadPool threadPool; + private final ClientTransportIndicesExistsAction indicesExistsAction; + private final ClientTransportIndicesStatusAction indicesStatusAction; private final ClientTransportCreateIndexAction createIndexAction; @@ -125,7 +130,7 @@ public class InternalTransportIndicesAdminClient extends AbstractIndicesAdminCli private final ClientTransportDeleteIndexTemplateAction deleteIndexTemplateAction; @Inject public InternalTransportIndicesAdminClient(Settings settings, TransportClientNodesService nodesService, ThreadPool threadPool, - ClientTransportIndicesStatusAction indicesStatusAction, + ClientTransportIndicesExistsAction indicesExistsAction, ClientTransportIndicesStatusAction indicesStatusAction, ClientTransportCreateIndexAction createIndexAction, ClientTransportDeleteIndexAction deleteIndexAction, ClientTransportCloseIndexAction closeIndexAction, ClientTransportOpenIndexAction openIndexAction, ClientTransportRefreshAction refreshAction, ClientTransportFlushAction flushAction, ClientTransportOptimizeAction optimizeAction, @@ -135,6 +140,7 @@ public class InternalTransportIndicesAdminClient extends AbstractIndicesAdminCli ClientTransportPutIndexTemplateAction putIndexTemplateAction, ClientTransportDeleteIndexTemplateAction deleteIndexTemplateAction) { this.nodesService = nodesService; this.threadPool = threadPool; + this.indicesExistsAction = indicesExistsAction; this.indicesStatusAction = indicesStatusAction; this.createIndexAction = createIndexAction; this.deleteIndexAction = deleteIndexAction; @@ -158,6 +164,23 @@ public class InternalTransportIndicesAdminClient extends AbstractIndicesAdminCli return this.threadPool; } + @Override public ActionFuture exists(final IndicesExistsRequest request) { + return nodesService.execute(new TransportClientNodesService.NodeCallback>() { + @Override public ActionFuture doWithNode(DiscoveryNode node) throws ElasticSearchException { + return indicesExistsAction.execute(node, request); + } + }); + } + + @Override public void exists(final IndicesExistsRequest request, final ActionListener listener) { + nodesService.execute(new TransportClientNodesService.NodeCallback() { + @Override public Void doWithNode(DiscoveryNode node) throws ElasticSearchException { + indicesExistsAction.execute(node, request, listener); + return null; + } + }); + } + @Override public ActionFuture status(final IndicesStatusRequest request) { return nodesService.execute(new TransportClientNodesService.NodeCallback>() { @Override public ActionFuture doWithNode(DiscoveryNode node) throws ElasticSearchException { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/RestActionModule.java b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/RestActionModule.java index 42e718f68e8..892fb192054 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/RestActionModule.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/RestActionModule.java @@ -38,6 +38,7 @@ import org.elasticsearch.rest.action.admin.indices.cache.clear.RestClearIndicesC import org.elasticsearch.rest.action.admin.indices.close.RestCloseIndexAction; import org.elasticsearch.rest.action.admin.indices.create.RestCreateIndexAction; import org.elasticsearch.rest.action.admin.indices.delete.RestDeleteIndexAction; +import org.elasticsearch.rest.action.admin.indices.exists.RestIndicesExistsAction; import org.elasticsearch.rest.action.admin.indices.flush.RestFlushAction; import org.elasticsearch.rest.action.admin.indices.gateway.snapshot.RestGatewaySnapshotAction; import org.elasticsearch.rest.action.admin.indices.mapping.delete.RestDeleteMappingAction; @@ -94,6 +95,7 @@ public class RestActionModule extends AbstractModule { bind(RestBroadcastPingAction.class).asEagerSingleton(); bind(RestReplicationPingAction.class).asEagerSingleton(); + bind(RestIndicesExistsAction.class).asEagerSingleton(); bind(RestIndicesStatusAction.class).asEagerSingleton(); bind(RestGetIndicesAliasesAction.class).asEagerSingleton(); bind(RestIndicesAliasesAction.class).asEagerSingleton(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/exists/RestIndicesExistsAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/exists/RestIndicesExistsAction.java new file mode 100644 index 00000000000..dac777be434 --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/exists/RestIndicesExistsAction.java @@ -0,0 +1,83 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search 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.exists; + +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.admin.indices.exists.IndicesExistsRequest; +import org.elasticsearch.action.admin.indices.exists.IndicesExistsResponse; +import org.elasticsearch.client.Client; +import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.settings.SettingsFilter; +import org.elasticsearch.rest.BaseRestHandler; +import org.elasticsearch.rest.RestChannel; +import org.elasticsearch.rest.RestController; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.StringRestResponse; +import org.elasticsearch.rest.XContentThrowableRestResponse; + +import java.io.IOException; + +import static org.elasticsearch.rest.RestRequest.Method.*; +import static org.elasticsearch.rest.RestStatus.*; +import static org.elasticsearch.rest.action.support.RestActions.*; + +/** + * @author kimchy (Shay Banon) + */ +public class RestIndicesExistsAction extends BaseRestHandler { + + private final SettingsFilter settingsFilter; + + @Inject public RestIndicesExistsAction(Settings settings, Client client, RestController controller, + SettingsFilter settingsFilter) { + super(settings, client); + controller.registerHandler(HEAD, "/{index}", this); + + this.settingsFilter = settingsFilter; + } + + @Override public void handleRequest(final RestRequest request, final RestChannel channel) { + IndicesExistsRequest indicesExistsRequest = new IndicesExistsRequest(splitIndices(request.param("index"))); + // we just send back a response, no need to fork a listener + indicesExistsRequest.listenerThreaded(false); + client.admin().indices().exists(indicesExistsRequest, new ActionListener() { + @Override public void onResponse(IndicesExistsResponse response) { + try { + if (response.exists()) { + channel.sendResponse(new StringRestResponse(OK)); + } else { + channel.sendResponse(new StringRestResponse(NOT_FOUND)); + } + } catch (Exception e) { + onFailure(e); + } + } + + @Override public void onFailure(Throwable e) { + try { + channel.sendResponse(new XContentThrowableRestResponse(request, e)); + } catch (IOException e1) { + logger.error("Failed to send failure response", e1); + } + } + }); + } +} \ No newline at end of file diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/document/DocumentActionsTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/document/DocumentActionsTests.java index 24d879e850f..d81a920db35 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/document/DocumentActionsTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/document/DocumentActionsTests.java @@ -22,6 +22,7 @@ package org.elasticsearch.test.integration.document; import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus; import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse; +import org.elasticsearch.action.admin.indices.exists.IndicesExistsResponse; import org.elasticsearch.action.admin.indices.flush.FlushResponse; import org.elasticsearch.action.admin.indices.optimize.OptimizeResponse; import org.elasticsearch.action.admin.indices.refresh.RefreshResponse; @@ -120,6 +121,10 @@ public class DocumentActionsTests extends AbstractNodesTests { assertThat(refreshResponse.successfulShards(), equalTo(10)); assertThat(refreshResponse.failedShards(), equalTo(0)); + logger.info("--> index exists?"); + IndicesExistsResponse indicesExistsResponse = client1.admin().indices().prepareExists(getConcreteIndexName()).execute().actionGet(); + assertThat(indicesExistsResponse.exists(), equalTo(true)); + logger.info("Clearing cache"); ClearIndicesCacheResponse clearIndicesCacheResponse = client1.admin().indices().clearCache(clearIndicesCacheRequest("test")).actionGet(); assertThat(clearIndicesCacheResponse.successfulShards(), equalTo(10));