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 0ad96ed10a0..d100cf1f522 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.create.TransportCreateIndexAction; import org.elasticsearch.action.admin.indices.delete.TransportDeleteIndexAction; 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; import org.elasticsearch.action.admin.indices.mapping.put.TransportPutMappingAction; import org.elasticsearch.action.admin.indices.optimize.TransportOptimizeAction; import org.elasticsearch.action.admin.indices.refresh.TransportRefreshAction; @@ -78,8 +79,9 @@ public class TransportActionModule extends AbstractModule { bind(TransportIndicesStatusAction.class).asEagerSingleton(); bind(TransportCreateIndexAction.class).asEagerSingleton(); - bind(TransportPutMappingAction.class).asEagerSingleton(); bind(TransportDeleteIndexAction.class).asEagerSingleton(); + bind(TransportPutMappingAction.class).asEagerSingleton(); + bind(TransportDeleteMappingAction.class).asEagerSingleton(); bind(TransportIndicesAliasesAction.class).asEagerSingleton(); bind(TransportUpdateSettingsAction.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 76310fd3dd1..56117ae93bb 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/TransportActions.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/TransportActions.java @@ -62,6 +62,7 @@ public class TransportActions { public static class Mapping { public static final String PUT = "indices/mapping/put"; + public static final String DELETE = "indices/mapping/delete"; } public static class Cache { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/delete/DeleteMappingRequest.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/delete/DeleteMappingRequest.java new file mode 100644 index 00000000000..603ac7c9f14 --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/delete/DeleteMappingRequest.java @@ -0,0 +1,117 @@ +/* + * 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.mapping.delete; + +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.*; + +/** + * @author kimchy (shay.banon) + */ +public class DeleteMappingRequest extends MasterNodeOperationRequest { + + private String[] indices; + + private String mappingType; + + DeleteMappingRequest() { + } + + /** + * Constructs a new put mapping request against one or more indices. If nothing is set then + * it will be executed against all indices. + */ + public DeleteMappingRequest(String... indices) { + this.indices = indices; + } + + @Override public ActionRequestValidationException validate() { + ActionRequestValidationException validationException = null; + if (mappingType == null) { + validationException = addValidationError("mapping type is missing", validationException); + } + return validationException; + } + + /** + * Sets the indices this put mapping operation will execute on. + */ + public DeleteMappingRequest indices(String[] indices) { + this.indices = indices; + return this; + } + + /** + * The indices the mappings will be put. + */ + public String[] indices() { + return indices; + } + + /** + * The mapping type. + */ + public String type() { + return mappingType; + } + + /** + * The type of the mappings to remove. + */ + public DeleteMappingRequest type(String mappingType) { + this.mappingType = mappingType; + return this; + } + + @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(); + } + if (in.readBoolean()) { + mappingType = in.readUTF(); + } + } + + @Override public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + if (indices == null) { + out.writeVInt(0); + } else { + out.writeVInt(indices.length); + for (String index : indices) { + out.writeUTF(index); + } + } + if (mappingType == null) { + out.writeBoolean(false); + } else { + out.writeBoolean(true); + out.writeUTF(mappingType); + } + } +} \ No newline at end of file diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/delete/DeleteMappingResponse.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/delete/DeleteMappingResponse.java new file mode 100644 index 00000000000..85c63ab47a0 --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/delete/DeleteMappingResponse.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.action.admin.indices.mapping.delete; + +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; + +/** + * The response of remove mapping operation. + * + * @author kimchy (shay.banon) + */ +public class DeleteMappingResponse implements ActionResponse, Streamable { + + DeleteMappingResponse() { + + } + + @Override public void readFrom(StreamInput in) throws IOException { + } + + @Override public void writeTo(StreamOutput out) throws IOException { + } +} \ No newline at end of file diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/delete/TransportDeleteMappingAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/delete/TransportDeleteMappingAction.java new file mode 100644 index 00000000000..3e8f041d4f8 --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/delete/TransportDeleteMappingAction.java @@ -0,0 +1,116 @@ +/* + * 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.mapping.delete; + +import org.elasticsearch.ElasticSearchException; +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.TransportActions; +import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse; +import org.elasticsearch.action.deletebyquery.TransportDeleteByQueryAction; +import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction; +import org.elasticsearch.client.Requests; +import org.elasticsearch.cluster.ClusterService; +import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.block.ClusterBlockLevel; +import org.elasticsearch.cluster.metadata.MetaDataMappingService; +import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.mapper.TypeFieldMapper; +import org.elasticsearch.index.query.xcontent.FilterBuilders; +import org.elasticsearch.index.query.xcontent.QueryBuilders; +import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.transport.TransportService; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicReference; + +/** + * Delete mapping action. + * + * @author kimchy (shay.banon) + */ +public class TransportDeleteMappingAction extends TransportMasterNodeOperationAction { + + private final MetaDataMappingService metaDataMappingService; + + private final TransportDeleteByQueryAction deleteByQueryAction; + + @Inject public TransportDeleteMappingAction(Settings settings, TransportService transportService, ClusterService clusterService, + ThreadPool threadPool, MetaDataMappingService metaDataMappingService, TransportDeleteByQueryAction deleteByQueryAction) { + super(settings, transportService, clusterService, threadPool); + this.metaDataMappingService = metaDataMappingService; + this.deleteByQueryAction = deleteByQueryAction; + } + + + @Override protected String transportAction() { + return TransportActions.Admin.Indices.Mapping.DELETE; + } + + @Override protected DeleteMappingRequest newRequest() { + return new DeleteMappingRequest(); + } + + @Override protected DeleteMappingResponse newResponse() { + return new DeleteMappingResponse(); + } + + @Override protected void checkBlock(DeleteMappingRequest request, ClusterState state) { + // update to concrete indices + request.indices(state.metaData().concreteIndices(request.indices())); + + for (String index : request.indices()) { + state.blocks().indexBlockedRaiseException(ClusterBlockLevel.METADATA, index); + } + } + + @Override protected DeleteMappingResponse masterOperation(final DeleteMappingRequest request, final ClusterState state) throws ElasticSearchException { + + final AtomicReference failureRef = new AtomicReference(); + final CountDownLatch latch = new CountDownLatch(1); + deleteByQueryAction.execute(Requests.deleteByQueryRequest(request.indices()).query(QueryBuilders.filtered(QueryBuilders.matchAllQuery(), FilterBuilders.termFilter(TypeFieldMapper.NAME, request.type()))), new ActionListener() { + @Override public void onResponse(DeleteByQueryResponse deleteByQueryResponse) { + metaDataMappingService.removeMapping(new MetaDataMappingService.RemoveRequest(request.indices(), request.type())); + latch.countDown(); + } + + @Override public void onFailure(Throwable e) { + failureRef.set(e); + latch.countDown(); + } + }); + + try { + latch.await(); + } catch (InterruptedException e) { + failureRef.set(e); + } + + if (failureRef.get() != null) { + if (failureRef.get() instanceof ElasticSearchException) { + throw (ElasticSearchException) failureRef.get(); + } else { + throw new ElasticSearchException(failureRef.get().getMessage(), failureRef.get()); + } + } + + return new DeleteMappingResponse(); + } +} \ No newline at end of file diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/TransportPutMappingAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/TransportPutMappingAction.java index fdf89282b07..064c266dc78 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/TransportPutMappingAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/TransportPutMappingAction.java @@ -81,7 +81,7 @@ public class TransportPutMappingAction extends TransportMasterNodeOperationActio final AtomicReference responseRef = new AtomicReference(); final AtomicReference failureRef = new AtomicReference(); final CountDownLatch latch = new CountDownLatch(1); - metaDataMappingService.putMapping(new MetaDataMappingService.Request(request.indices(), request.type(), request.source()).ignoreConflicts(request.ignoreConflicts()).timeout(request.timeout()), new MetaDataMappingService.Listener() { + metaDataMappingService.putMapping(new MetaDataMappingService.PutRequest(request.indices(), request.type(), request.source()).ignoreConflicts(request.ignoreConflicts()).timeout(request.timeout()), new MetaDataMappingService.Listener() { @Override public void onResponse(MetaDataMappingService.Response response) { responseRef.set(new PutMappingResponse(response.acknowledged())); latch.countDown(); 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 cb5f7d9bff9..0c526ce4d93 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.flush.FlushRequest; import org.elasticsearch.action.admin.indices.flush.FlushResponse; import org.elasticsearch.action.admin.indices.gateway.snapshot.GatewaySnapshotRequest; import org.elasticsearch.action.admin.indices.gateway.snapshot.GatewaySnapshotResponse; +import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest; +import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingResponse; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse; import org.elasticsearch.action.admin.indices.optimize.OptimizeRequest; @@ -49,6 +51,7 @@ import org.elasticsearch.client.action.admin.indices.create.CreateIndexRequestBu import org.elasticsearch.client.action.admin.indices.delete.DeleteIndexRequestBuilder; 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; import org.elasticsearch.client.action.admin.indices.mapping.put.PutMappingRequestBuilder; import org.elasticsearch.client.action.admin.indices.optimize.OptimizeRequestBuilder; import org.elasticsearch.client.action.admin.indices.refresh.RefreshRequestBuilder; @@ -228,6 +231,29 @@ public interface IndicesAdminClient { */ PutMappingRequestBuilder preparePutMapping(String... indices); + /** + * Deletes mapping (and all its data) from one or more indices. + * + * @param request The delete mapping request + * @return A result future + * @see org.elasticsearch.client.Requests#deleteMappingRequest(String...) + */ + ActionFuture deleteMapping(DeleteMappingRequest request); + + /** + * Deletes mapping definition for a type into one or more indices. + * + * @param request The delete mapping request + * @param listener A listener to be notified with a result + * @see org.elasticsearch.client.Requests#deleteMappingRequest(String...) + */ + void deleteMapping(DeleteMappingRequest request, ActionListener listener); + + /** + * Deletes mapping definition for a type into one or more indices. + */ + DeleteMappingRequestBuilder prepareDeleteMapping(String... indices); + /** * Explicitly perform gateway snapshot for 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 b663a10a035..47830f5d571 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/client/Requests.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/Requests.java @@ -34,6 +34,7 @@ import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; 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; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.action.admin.indices.optimize.OptimizeRequest; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; @@ -212,6 +213,17 @@ public class Requests { return new PutMappingRequest(indices); } + /** + * Deletes mapping (and all its data) from one or more indices. + * + * @param indices The indices the mapping will be deleted from. Use null or _all to execute against all indices + * @return The create mapping request + * @see org.elasticsearch.client.IndicesAdminClient#deleteMapping(org.elasticsearch.action.admin.indices.mapping.put.DeleteMappingRequest) + */ + public static DeleteMappingRequest deleteMappingRequest(String... indices) { + return new DeleteMappingRequest(indices); + } + /** * Creates an index aliases request allowing to add and remove aliases. * diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/mapping/delete/DeleteMappingRequestBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/mapping/delete/DeleteMappingRequestBuilder.java new file mode 100644 index 00000000000..7f1ba36ed8b --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/mapping/delete/DeleteMappingRequestBuilder.java @@ -0,0 +1,62 @@ +/* + * 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.mapping.delete; + +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest; +import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingResponse; +import org.elasticsearch.client.IndicesAdminClient; +import org.elasticsearch.client.action.admin.indices.support.BaseIndicesRequestBuilder; +import org.elasticsearch.common.unit.TimeValue; + +/** + * @author kimchy (shay.banon) + */ +public class DeleteMappingRequestBuilder extends BaseIndicesRequestBuilder { + + public DeleteMappingRequestBuilder(IndicesAdminClient indicesClient) { + super(indicesClient, new DeleteMappingRequest()); + } + + public DeleteMappingRequestBuilder setIndices(String... indices) { + request.indices(indices); + return this; + } + + /** + * The type of the mapping to remove. + */ + public DeleteMappingRequestBuilder setType(String type) { + request.type(type); + return this; + } + + /** + * Sets the master node timeout in case the master has not yet been discovered. + */ + public DeleteMappingRequestBuilder setMasterNodeTimeout(TimeValue timeout) { + request.masterNodeTimeout(timeout); + return this; + } + + @Override protected void doExecute(ActionListener listener) { + client.deleteMapping(request, listener); + } +} \ No newline at end of file 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 f8a8766eae4..0ba16040586 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.flush.TransportFlushAction; import org.elasticsearch.action.admin.indices.gateway.snapshot.GatewaySnapshotRequest; import org.elasticsearch.action.admin.indices.gateway.snapshot.GatewaySnapshotResponse; import org.elasticsearch.action.admin.indices.gateway.snapshot.TransportGatewaySnapshotAction; +import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest; +import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingResponse; +import org.elasticsearch.action.admin.indices.mapping.delete.TransportDeleteMappingAction; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse; import org.elasticsearch.action.admin.indices.mapping.put.TransportPutMappingAction; @@ -81,6 +84,8 @@ public class NodeIndicesAdminClient extends AbstractIndicesAdminClient implement private final TransportPutMappingAction putMappingAction; + private final TransportDeleteMappingAction deleteMappingAction; + private final TransportGatewaySnapshotAction gatewaySnapshotAction; private final TransportIndicesAliasesAction indicesAliasesAction; @@ -92,7 +97,7 @@ public class NodeIndicesAdminClient extends AbstractIndicesAdminClient implement @Inject public NodeIndicesAdminClient(Settings settings, ThreadPool threadPool, TransportIndicesStatusAction indicesStatusAction, TransportCreateIndexAction createIndexAction, TransportDeleteIndexAction deleteIndexAction, TransportRefreshAction refreshAction, TransportFlushAction flushAction, TransportOptimizeAction optimizeAction, - TransportPutMappingAction putMappingAction, TransportGatewaySnapshotAction gatewaySnapshotAction, + TransportPutMappingAction putMappingAction, TransportDeleteMappingAction deleteMappingAction, TransportGatewaySnapshotAction gatewaySnapshotAction, TransportIndicesAliasesAction indicesAliasesAction, TransportClearIndicesCacheAction clearIndicesCacheAction, TransportUpdateSettingsAction updateSettingsAction) { this.threadPool = threadPool; @@ -102,6 +107,7 @@ public class NodeIndicesAdminClient extends AbstractIndicesAdminClient implement this.refreshAction = refreshAction; this.flushAction = flushAction; this.optimizeAction = optimizeAction; + this.deleteMappingAction = deleteMappingAction; this.putMappingAction = putMappingAction; this.gatewaySnapshotAction = gatewaySnapshotAction; this.indicesAliasesAction = indicesAliasesAction; @@ -169,6 +175,14 @@ public class NodeIndicesAdminClient extends AbstractIndicesAdminClient implement putMappingAction.execute(request, listener); } + @Override public ActionFuture deleteMapping(DeleteMappingRequest request) { + return deleteMappingAction.execute(request); + } + + @Override public void deleteMapping(DeleteMappingRequest request, ActionListener listener) { + deleteMappingAction.execute(request, listener); + } + @Override public ActionFuture gatewaySnapshot(GatewaySnapshotRequest request) { return gatewaySnapshotAction.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 0572f2e86c8..e028ae0c335 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.create.CreateIndexRequestBu import org.elasticsearch.client.action.admin.indices.delete.DeleteIndexRequestBuilder; 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; import org.elasticsearch.client.action.admin.indices.mapping.put.PutMappingRequestBuilder; import org.elasticsearch.client.action.admin.indices.optimize.OptimizeRequestBuilder; import org.elasticsearch.client.action.admin.indices.refresh.RefreshRequestBuilder; @@ -65,6 +66,10 @@ public abstract class AbstractIndicesAdminClient implements InternalIndicesAdmin return new PutMappingRequestBuilder(this).setIndices(indices); } + @Override public DeleteMappingRequestBuilder prepareDeleteMapping(String... indices) { + return new DeleteMappingRequestBuilder(this).setIndices(indices); + } + @Override public OptimizeRequestBuilder prepareOptimize(String... indices) { return new OptimizeRequestBuilder(this).setIndices(indices); } 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 29d6adf0755..bdc0f9704f3 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,7 +34,8 @@ import org.elasticsearch.client.transport.action.admin.indices.create.ClientTran import org.elasticsearch.client.transport.action.admin.indices.delete.ClientTransportDeleteIndexAction; 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.create.ClientTransportPutMappingAction; +import org.elasticsearch.client.transport.action.admin.indices.mapping.delete.ClientTransportDeleteMappingAction; +import org.elasticsearch.client.transport.action.admin.indices.mapping.put.ClientTransportPutMappingAction; import org.elasticsearch.client.transport.action.admin.indices.optimize.ClientTransportOptimizeAction; import org.elasticsearch.client.transport.action.admin.indices.refresh.ClientTransportRefreshAction; import org.elasticsearch.client.transport.action.admin.indices.settings.ClientTransportUpdateSettingsAction; @@ -71,6 +72,7 @@ public class ClientTransportActionModule extends AbstractModule { bind(ClientTransportCreateIndexAction.class).asEagerSingleton(); bind(ClientTransportDeleteIndexAction.class).asEagerSingleton(); bind(ClientTransportPutMappingAction.class).asEagerSingleton(); + bind(ClientTransportDeleteMappingAction.class).asEagerSingleton(); bind(ClientTransportGatewaySnapshotAction.class).asEagerSingleton(); bind(ClientTransportIndicesAliasesAction.class).asEagerSingleton(); bind(ClientTransportClearIndicesCacheAction.class).asEagerSingleton(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/action/admin/indices/mapping/delete/ClientTransportDeleteMappingAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/action/admin/indices/mapping/delete/ClientTransportDeleteMappingAction.java new file mode 100644 index 00000000000..d47ba9906dd --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/action/admin/indices/mapping/delete/ClientTransportDeleteMappingAction.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.mapping.delete; + +import org.elasticsearch.action.TransportActions; +import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest; +import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingResponse; +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 ClientTransportDeleteMappingAction extends BaseClientTransportAction { + + @Inject public ClientTransportDeleteMappingAction(Settings settings, TransportService transportService) { + super(settings, transportService, DeleteMappingResponse.class); + } + + @Override protected String action() { + return TransportActions.Admin.Indices.Mapping.DELETE; + } +} \ No newline at end of file diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/action/admin/indices/mapping/create/ClientTransportPutMappingAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/action/admin/indices/mapping/put/ClientTransportPutMappingAction.java similarity index 99% rename from modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/action/admin/indices/mapping/create/ClientTransportPutMappingAction.java rename to modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/action/admin/indices/mapping/put/ClientTransportPutMappingAction.java index a6f020e022a..af768fa5e92 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/action/admin/indices/mapping/create/ClientTransportPutMappingAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/transport/action/admin/indices/mapping/put/ClientTransportPutMappingAction.java @@ -17,7 +17,7 @@ * under the License. */ -package org.elasticsearch.client.transport.action.admin.indices.mapping.create; +package org.elasticsearch.client.transport.action.admin.indices.mapping.put; import org.elasticsearch.action.TransportActions; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; 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 8f37bf92478..31cdc314009 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.flush.FlushRequest; import org.elasticsearch.action.admin.indices.flush.FlushResponse; import org.elasticsearch.action.admin.indices.gateway.snapshot.GatewaySnapshotRequest; import org.elasticsearch.action.admin.indices.gateway.snapshot.GatewaySnapshotResponse; +import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest; +import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingResponse; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse; import org.elasticsearch.action.admin.indices.optimize.OptimizeRequest; @@ -53,7 +55,8 @@ import org.elasticsearch.client.transport.action.admin.indices.create.ClientTran import org.elasticsearch.client.transport.action.admin.indices.delete.ClientTransportDeleteIndexAction; 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.create.ClientTransportPutMappingAction; +import org.elasticsearch.client.transport.action.admin.indices.mapping.delete.ClientTransportDeleteMappingAction; +import org.elasticsearch.client.transport.action.admin.indices.mapping.put.ClientTransportPutMappingAction; import org.elasticsearch.client.transport.action.admin.indices.optimize.ClientTransportOptimizeAction; import org.elasticsearch.client.transport.action.admin.indices.refresh.ClientTransportRefreshAction; import org.elasticsearch.client.transport.action.admin.indices.settings.ClientTransportUpdateSettingsAction; @@ -86,6 +89,8 @@ public class InternalTransportIndicesAdminClient extends AbstractIndicesAdminCli private final ClientTransportPutMappingAction putMappingAction; + private final ClientTransportDeleteMappingAction deleteMappingAction; + private final ClientTransportGatewaySnapshotAction gatewaySnapshotAction; private final ClientTransportIndicesAliasesAction indicesAliasesAction; @@ -98,7 +103,7 @@ public class InternalTransportIndicesAdminClient extends AbstractIndicesAdminCli ClientTransportIndicesStatusAction indicesStatusAction, ClientTransportCreateIndexAction createIndexAction, ClientTransportDeleteIndexAction deleteIndexAction, ClientTransportRefreshAction refreshAction, ClientTransportFlushAction flushAction, ClientTransportOptimizeAction optimizeAction, - ClientTransportPutMappingAction putMappingAction, ClientTransportGatewaySnapshotAction gatewaySnapshotAction, + ClientTransportPutMappingAction putMappingAction, ClientTransportDeleteMappingAction deleteMappingAction, ClientTransportGatewaySnapshotAction gatewaySnapshotAction, ClientTransportIndicesAliasesAction indicesAliasesAction, ClientTransportClearIndicesCacheAction clearIndicesCacheAction, ClientTransportUpdateSettingsAction updateSettingsAction) { this.nodesService = nodesService; @@ -110,6 +115,7 @@ public class InternalTransportIndicesAdminClient extends AbstractIndicesAdminCli this.flushAction = flushAction; this.optimizeAction = optimizeAction; this.putMappingAction = putMappingAction; + this.deleteMappingAction = deleteMappingAction; this.gatewaySnapshotAction = gatewaySnapshotAction; this.indicesAliasesAction = indicesAliasesAction; this.clearIndicesCacheAction = clearIndicesCacheAction; @@ -239,6 +245,23 @@ public class InternalTransportIndicesAdminClient extends AbstractIndicesAdminCli }); } + @Override public ActionFuture deleteMapping(final DeleteMappingRequest request) { + return nodesService.execute(new TransportClientNodesService.NodeCallback>() { + @Override public ActionFuture doWithNode(DiscoveryNode node) throws ElasticSearchException { + return deleteMappingAction.execute(node, request); + } + }); + } + + @Override public void deleteMapping(final DeleteMappingRequest request, final ActionListener listener) { + nodesService.execute(new TransportClientNodesService.NodeCallback() { + @Override public Void doWithNode(DiscoveryNode node) throws ElasticSearchException { + deleteMappingAction.execute(node, request, listener); + return null; + } + }); + } + @Override public ActionFuture gatewaySnapshot(final GatewaySnapshotRequest 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/cluster/metadata/MetaDataMappingService.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java index d05a6b0f16a..18e39a83493 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java @@ -113,7 +113,26 @@ public class MetaDataMappingService extends AbstractComponent { }); } - public void putMapping(final Request request, final Listener userListener) { + public void removeMapping(final RemoveRequest request) { + clusterService.submitStateUpdateTask("remove-mapping [" + request.mappingType + "]", new ClusterStateUpdateTask() { + @Override public ClusterState execute(ClusterState currentState) { + if (request.indices.length == 0) { + throw new IndexMissingException(new Index("_all")); + } + + MetaData.Builder builder = newMetaDataBuilder().metaData(currentState.metaData()); + for (String indexName : request.indices) { + if (currentState.metaData().hasIndex(indexName)) { + builder.put(newIndexMetaDataBuilder(currentState.metaData().index(indexName)).removeMapping(request.mappingType)); + } + } + + return ClusterState.builder().state(currentState).metaData(builder).build(); + } + }); + } + + public void putMapping(final PutRequest request, final Listener userListener) { clusterService.submitStateUpdateTask("put-mapping [" + request.mappingType + "]", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { final PutMappingListener listener = new PutMappingListener(request, userListener); @@ -256,13 +275,13 @@ public class MetaDataMappingService extends AbstractComponent { private AtomicBoolean notified = new AtomicBoolean(); - private final Request request; + private final PutRequest request; private final Listener listener; volatile Timeout timeout; - private PutMappingListener(Request request, Listener listener) { + private PutMappingListener(PutRequest request, Listener listener) { this.request = request; this.listener = listener; } @@ -293,7 +312,19 @@ public class MetaDataMappingService extends AbstractComponent { void onFailure(Throwable t); } - public static class Request { + public static class RemoveRequest { + + final String[] indices; + + final String mappingType; + + public RemoveRequest(String[] indices, String mappingType) { + this.indices = indices; + this.mappingType = mappingType; + } + } + + public static class PutRequest { final String[] indices; @@ -305,18 +336,18 @@ public class MetaDataMappingService extends AbstractComponent { TimeValue timeout = TimeValue.timeValueSeconds(10); - public Request(String[] indices, String mappingType, String mappingSource) { + public PutRequest(String[] indices, String mappingType, String mappingSource) { this.indices = indices; this.mappingType = mappingType; this.mappingSource = mappingSource; } - public Request ignoreConflicts(boolean ignoreConflicts) { + public PutRequest ignoreConflicts(boolean ignoreConflicts) { this.ignoreConflicts = ignoreConflicts; return this; } - public Request timeout(TimeValue timeout) { + public PutRequest timeout(TimeValue timeout) { this.timeout = timeout; return this; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentFieldMappers.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentFieldMappers.java index f1f2845758a..4cbbccb0433 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentFieldMappers.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentFieldMappers.java @@ -99,6 +99,14 @@ public class DocumentFieldMappers implements Iterable { return fieldMappers.iterator(); } + public ImmutableList mappers() { + return this.fieldMappers; + } + + public boolean hasMapper(FieldMapper fieldMapper) { + return fieldMappers.contains(fieldMapper); + } + public FieldMappers name(String name) { return nameFieldMappers.get(name); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMappers.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMappers.java index d604f58ee78..aa4d8a4a970 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMappers.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMappers.java @@ -24,6 +24,8 @@ import org.elasticsearch.common.collect.Iterators; import org.elasticsearch.common.collect.UnmodifiableIterator; import org.elasticsearch.common.util.concurrent.Immutable; +import java.util.List; + /** * A holder for several {@link FieldMapper}. * @@ -85,4 +87,30 @@ public class FieldMappers implements Iterable { public FieldMappers concat(FieldMappers mappers) { return new FieldMappers(new ImmutableList.Builder().addAll(fieldMappers).addAll(mappers).build()); } + + public FieldMappers remove(List mappers) { + ImmutableList.Builder builder = new ImmutableList.Builder(); + for (FieldMapper fieldMapper : fieldMappers) { + boolean found = false; + for (FieldMapper mapper : mappers) { + if (fieldMapper.equals(mapper)) { // identify equality + found = true; + } + } + if (!found) { + builder.add(fieldMapper); + } + } + return new FieldMappers(builder.build()); + } + + public FieldMappers remove(FieldMapper mapper) { + ImmutableList.Builder builder = new ImmutableList.Builder(); + for (FieldMapper fieldMapper : fieldMappers) { + if (!fieldMapper.equals(mapper)) { // identify equality + builder.add(fieldMapper); + } + } + return new FieldMappers(builder.build()); + } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMappersFieldSelector.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMappersFieldSelector.java index daafda0bb62..88168a2145e 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMappersFieldSelector.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMappersFieldSelector.java @@ -31,6 +31,10 @@ public class FieldMappersFieldSelector implements FieldSelector { private final HashSet names = new HashSet(); + public void add(String fieldName) { + names.add(fieldName); + } + public void add(FieldMappers fieldMappers) { for (FieldMapper fieldMapper : fieldMappers) { names.add(fieldMapper.names().indexName()); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/MapperService.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/MapperService.java index feecabd668f..9a0cb0878a4 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/MapperService.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/MapperService.java @@ -67,10 +67,6 @@ public class MapperService extends AbstractIndexComponent implements Iterable nameFieldMappers = ImmutableMap.of(); private volatile ImmutableMap indexNameFieldMappers = ImmutableMap.of(); private volatile ImmutableMap fullNameFieldMappers = ImmutableMap.of(); - private volatile FieldMappers idFieldMappers = new FieldMappers(); - private volatile FieldMappers typeFieldMappers = new FieldMappers(); - private volatile FieldMappers uidFieldMappers = new FieldMappers(); - private volatile FieldMappers sourceFieldMappers = new FieldMappers(); // for now, just use the xcontent one. Can work on it more to support custom ones private final DocumentMapperParser documentParser; @@ -163,6 +159,49 @@ public class MapperService extends AbstractIndexComponent implements Iterable mappings = indexMetaData.mappings(); - // we don't support removing mappings for now ... + // go over and add the relevant mappings (or update them) for (Map.Entry entry : mappings.entrySet()) { String mappingType = entry.getKey(); CompressedString mappingSource = entry.getValue(); @@ -222,6 +222,13 @@ public class IndicesClusterStateService extends AbstractLifecycleComponent() { + @Override public void onResponse(DeleteMappingResponse response) { + try { + XContentBuilder builder = RestXContentBuilder.restContentBuilder(request); + builder.startObject() + .field("ok", true); + builder.endObject(); + channel.sendResponse(new XContentRestResponse(request, OK, builder)); + } catch (IOException e) { + onFailure(e); + } + } + + @Override public void onFailure(Throwable e) { + try { + XContentBuilder builder = RestXContentBuilder.restContentBuilder(request); + Throwable t = unwrapCause(e); + if (t instanceof IndexMissingException || t instanceof InvalidTypeNameException || t instanceof MergeMappingException) { + channel.sendResponse(new XContentRestResponse(request, BAD_REQUEST, builder.startObject().field("error", t.getMessage()).endObject())); + } else { + 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/elasticsearch/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java index d0759144d73..b13c88ee129 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java @@ -255,7 +255,7 @@ public class FetchPhase implements SearchPhase { } fieldSelector.add(x); } - fieldSelector.add(context.mapperService().uidFieldMappers()); + fieldSelector.add(UidFieldMapper.NAME); return fieldSelector; } } diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/indices/mapping/SimpleDeleteMappingTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/indices/mapping/SimpleDeleteMappingTests.java new file mode 100644 index 00000000000..4475f7b2cfb --- /dev/null +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/indices/mapping/SimpleDeleteMappingTests.java @@ -0,0 +1,100 @@ +/* + * 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.test.integration.indices.mapping; + +import org.elasticsearch.action.count.CountResponse; +import org.elasticsearch.client.Client; +import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.test.integration.AbstractNodesTests; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import static org.elasticsearch.client.Requests.*; +import static org.elasticsearch.common.xcontent.XContentFactory.*; +import static org.elasticsearch.index.query.xcontent.QueryBuilders.*; +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; + +/** + * @author kimchy (shay.banon) + */ +public class SimpleDeleteMappingTests extends AbstractNodesTests { + + protected Client client1; + protected Client client2; + + @BeforeMethod public void startNodes() { + startNode("node1"); + startNode("node2"); + client1 = getClient1(); + client2 = getClient2(); + + createIndex(); + } + + protected void createIndex() { + logger.info("Creating index test"); + client1.admin().indices().create(createIndexRequest("test")).actionGet(); + } + + @AfterMethod public void closeNodes() { + client1.close(); + client2.close(); + closeAllNodes(); + } + + protected Client getClient1() { + return client("node1"); + } + + protected Client getClient2() { + return client("node2"); + } + + @Test public void simpleDeleteMapping() throws Exception { + for (int i = 0; i < 10; i++) { + client1.prepareIndex("test", "type1", Integer.toString(i)).setSource(jsonBuilder().startObject() + .field("value", "test" + i) + .endObject()).execute().actionGet(); + } + + client1.admin().indices().prepareRefresh().execute().actionGet(); + + for (int i = 0; i < 10; i++) { + CountResponse countResponse = client1.prepareCount().setQuery(matchAllQuery()).execute().actionGet(); + assertThat(countResponse.count(), equalTo(10l)); + } + + ClusterState clusterState = client1.admin().cluster().prepareState().execute().actionGet().state(); + assertThat(clusterState.metaData().index("test").mappings().containsKey("type1"), equalTo(true)); + + client1.admin().indices().prepareDeleteMapping().setType("type1").execute().actionGet(); + Thread.sleep(500); // for now, we don't have ack logic, so just wait + + for (int i = 0; i < 10; i++) { + CountResponse countResponse = client1.prepareCount().setQuery(matchAllQuery()).execute().actionGet(); + assertThat(countResponse.count(), equalTo(0l)); + } + + clusterState = client1.admin().cluster().prepareState().execute().actionGet().state(); + assertThat(clusterState.metaData().index("test").mappings().containsKey("type1"), equalTo(false)); + } +}