Skip to content

Commit 9a9ce99

Browse files
committed
Create Mapping API: Automatically create indices. Closes #12.
1 parent b5f3fc9 commit 9a9ce99

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/create/TransportCreateMappingAction.java

+41-1
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,25 @@
2020
package org.elasticsearch.action.admin.indices.mapping.create;
2121

2222
import com.google.inject.Inject;
23+
import org.elasticsearch.ExceptionsHelper;
2324
import org.elasticsearch.action.ActionListener;
2425
import org.elasticsearch.action.Actions;
2526
import org.elasticsearch.action.TransportActions;
27+
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
28+
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
29+
import org.elasticsearch.action.admin.indices.create.TransportCreateIndexAction;
2630
import org.elasticsearch.action.support.BaseAction;
2731
import org.elasticsearch.cluster.ClusterService;
2832
import org.elasticsearch.cluster.metadata.MetaDataService;
33+
import org.elasticsearch.indices.IndexAlreadyExistsException;
2934
import org.elasticsearch.threadpool.ThreadPool;
3035
import org.elasticsearch.transport.*;
3136
import org.elasticsearch.util.io.VoidStreamable;
3237
import org.elasticsearch.util.settings.Settings;
3338

3439
import java.io.IOException;
40+
import java.util.concurrent.CountDownLatch;
41+
import java.util.concurrent.TimeUnit;
3542

3643
/**
3744
* @author kimchy (Shay Banon)
@@ -44,21 +51,54 @@ public class TransportCreateMappingAction extends BaseAction<CreateMappingReques
4451

4552
private final MetaDataService metaDataService;
4653

54+
private final TransportCreateIndexAction createIndexAction;
55+
4756
private final ThreadPool threadPool;
4857

58+
private final boolean autoCreateIndex;
59+
4960
@Inject public TransportCreateMappingAction(Settings settings, TransportService transportService, ClusterService clusterService,
50-
ThreadPool threadPool, MetaDataService metaDataService) {
61+
ThreadPool threadPool, MetaDataService metaDataService, TransportCreateIndexAction createIndexAction) {
5162
super(settings);
5263
this.transportService = transportService;
5364
this.clusterService = clusterService;
5465
this.threadPool = threadPool;
5566
this.metaDataService = metaDataService;
67+
this.createIndexAction = createIndexAction;
68+
this.autoCreateIndex = settings.getAsBoolean("action.autoCreateIndex", true);
5669

5770
transportService.registerHandler(TransportActions.Admin.Indices.Mapping.CREATE, new TransportHandler());
5871
}
5972

6073
@Override protected void doExecute(final CreateMappingRequest request, final ActionListener<CreateMappingResponse> listener) {
6174
final String[] indices = Actions.processIndices(clusterService.state(), request.indices());
75+
if (autoCreateIndex) {
76+
final CountDownLatch latch = new CountDownLatch(indices.length);
77+
for (String index : indices) {
78+
if (!clusterService.state().metaData().hasIndex(index)) {
79+
createIndexAction.execute(new CreateIndexRequest(index), new ActionListener<CreateIndexResponse>() {
80+
@Override public void onResponse(CreateIndexResponse result) {
81+
latch.countDown();
82+
}
83+
84+
@Override public void onFailure(Throwable e) {
85+
if (ExceptionsHelper.unwrapCause(e) instanceof IndexAlreadyExistsException) {
86+
latch.countDown();
87+
} else {
88+
listener.onFailure(e);
89+
}
90+
}
91+
});
92+
} else {
93+
latch.countDown();
94+
}
95+
}
96+
try {
97+
latch.await(10, TimeUnit.SECONDS);
98+
} catch (InterruptedException e) {
99+
// ignore
100+
}
101+
}
62102
if (clusterService.state().nodes().localNodeMaster()) {
63103
threadPool.execute(new Runnable() {
64104
@Override public void run() {

modules/elasticsearch/src/main/java/org/elasticsearch/action/index/TransportIndexAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class TransportIndexAction extends TransportShardReplicationOperationActi
5353
TransportCreateIndexAction createIndexAction) {
5454
super(settings, transportService, clusterService, indicesService, threadPool, shardStateAction);
5555
this.createIndexAction = createIndexAction;
56-
this.autoCreateIndex = componentSettings.getAsBoolean("autoCreateIndex", true);
56+
this.autoCreateIndex = settings.getAsBoolean("action.autoCreateIndex", true);
5757
this.allowIdGeneration = componentSettings.getAsBoolean("allowIdGeneration", true);
5858
}
5959

0 commit comments

Comments
 (0)