Skip to content

Commit 78e7325

Browse files
committed
put mapping to return the parsed source and an indication if it was ack from all the nodes within the timeout
1 parent 0e55c87 commit 78e7325

File tree

8 files changed

+111
-15
lines changed

8 files changed

+111
-15
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponse.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,34 @@
3131
*/
3232
public class PutMappingResponse implements ActionResponse, Streamable {
3333

34+
private boolean acknowledged;
35+
36+
private String parsedSource;
37+
38+
PutMappingResponse() {
39+
40+
}
41+
42+
public PutMappingResponse(boolean acknowledged, String parsedSource) {
43+
this.acknowledged = acknowledged;
44+
this.parsedSource = parsedSource;
45+
}
46+
47+
public boolean acknowledged() {
48+
return acknowledged;
49+
}
50+
51+
public String parsedSource() {
52+
return parsedSource;
53+
}
54+
3455
@Override public void readFrom(DataInput in) throws IOException, ClassNotFoundException {
56+
acknowledged = in.readBoolean();
57+
parsedSource = in.readUTF();
3558
}
3659

3760
@Override public void writeTo(DataOutput out) throws IOException {
61+
out.writeBoolean(acknowledged);
62+
out.writeUTF(parsedSource);
3863
}
3964
}

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/TransportPutMappingAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public class TransportPutMappingAction extends TransportMasterNodeOperationActio
7474

7575
@Override protected PutMappingResponse masterOperation(PutMappingRequest request) throws ElasticSearchException {
7676
final String[] indices = processIndices(clusterService.state(), request.indices());
77-
metaDataService.addMapping(indices, request.type(), request.mappingSource(), request.timeout());
78-
return new PutMappingResponse();
77+
MetaDataService.PutMappingResult result = metaDataService.putMapping(indices, request.type(), request.mappingSource(), request.timeout());
78+
return new PutMappingResponse(result.acknowledged(), result.parsedSource());
7979
}
8080

8181
@Override protected void doExecute(final PutMappingRequest request, final ActionListener<PutMappingResponse> listener) {

modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataService.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public synchronized boolean deleteIndex(final String index, TimeValue timeout) t
200200
}
201201
}
202202

203-
public boolean addMapping(final String[] indices, String mappingType, final String mappingSource, TimeValue timeout) throws ElasticSearchException {
203+
public PutMappingResult putMapping(final String[] indices, String mappingType, final String mappingSource, TimeValue timeout) throws ElasticSearchException {
204204
ClusterState clusterState = clusterService.state();
205205
for (String index : indices) {
206206
IndexRoutingTable indexTable = clusterState.routingTable().indicesRouting().get(index);
@@ -220,6 +220,8 @@ public boolean addMapping(final String[] indices, String mappingType, final Stri
220220
}
221221
}
222222

223+
String parsedSource = documentMapper.buildSource();
224+
223225
if (mappingType == null) {
224226
mappingType = documentMapper.type();
225227
} else if (!mappingType.equals(documentMapper.type())) {
@@ -229,7 +231,7 @@ public boolean addMapping(final String[] indices, String mappingType, final Stri
229231
throw new InvalidTypeNameException("Document mapping type name can't start with '_'");
230232
}
231233

232-
logger.info("Indices [" + Arrays.toString(indices) + "]: Creating mapping [" + mappingType + "] with source [" + mappingSource + "]");
234+
logger.info("Indices [" + Arrays.toString(indices) + "]: Put mapping [" + mappingType + "] with source [" + mappingSource + "]");
233235

234236
final CountDownLatch latch = new CountDownLatch(clusterService.state().nodes().size() * indices.length);
235237
final Set<String> indicesSet = Sets.newHashSet(indices);
@@ -258,13 +260,35 @@ public boolean addMapping(final String[] indices, String mappingType, final Stri
258260
}
259261
});
260262

263+
boolean acknowledged;
261264
try {
262-
return latch.await(timeout.millis(), TimeUnit.MILLISECONDS);
265+
acknowledged = latch.await(timeout.millis(), TimeUnit.MILLISECONDS);
263266
} catch (InterruptedException e) {
264-
return false;
267+
acknowledged = false;
265268
} finally {
266269
nodeMappingCreatedAction.remove(listener);
267270
}
271+
272+
return new PutMappingResult(acknowledged, parsedSource);
268273
}
269274

275+
public static class PutMappingResult {
276+
277+
private final boolean acknowledged;
278+
279+
private final String parsedSource;
280+
281+
public PutMappingResult(boolean acknowledged, String parsedSource) {
282+
this.acknowledged = acknowledged;
283+
this.parsedSource = parsedSource;
284+
}
285+
286+
public boolean acknowledged() {
287+
return acknowledged;
288+
}
289+
290+
public String parsedSource() {
291+
return parsedSource;
292+
}
293+
}
270294
}

modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public interface DocumentMapper {
3737
*/
3838
String mappingSource();
3939

40+
/**
41+
* Generates the source of the mapper based on the current mappings.
42+
*/
43+
String buildSource() throws FailedToGenerateSourceMapperException;
44+
4045
UidFieldMapper uidMapper();
4146

4247
IdFieldMapper idMapper();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.mapper;
21+
22+
/**
23+
* @author kimchy (shay.banon)
24+
*/
25+
public class FailedToGenerateSourceMapperException extends MapperException {
26+
27+
public FailedToGenerateSourceMapperException(String message) {
28+
super(message);
29+
}
30+
31+
public FailedToGenerateSourceMapperException(String message, Throwable cause) {
32+
super(message, cause);
33+
}
34+
}

modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonDocumentMapper.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,17 @@ void addFieldMapper(FieldMapper fieldMapper) {
339339
}
340340
}
341341

342-
public String toJson() throws IOException {
343-
JsonBuilder builder = jsonBuilder().prettyPrint();
344-
builder.startObject();
345-
toJson(builder, ToJson.EMPTY_PARAMS);
346-
builder.endObject();
347-
return builder.string();
342+
343+
@Override public String buildSource() throws FailedToGenerateSourceMapperException {
344+
try {
345+
JsonBuilder builder = jsonBuilder().prettyPrint();
346+
builder.startObject();
347+
toJson(builder, ToJson.EMPTY_PARAMS);
348+
builder.endObject();
349+
return builder.string();
350+
} catch (Exception e) {
351+
throw new FailedToGenerateSourceMapperException(e.getMessage(), e);
352+
}
348353
}
349354

350355
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {

modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/mapping/put/RestPutMappingAction.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,15 @@ public class RestPutMappingAction extends BaseRestHandler {
5757
putMappingRequest.mappingSource(request.contentAsString());
5858
putMappingRequest.timeout(request.paramAsTime("timeout", timeValueSeconds(10)));
5959
client.admin().indices().execPutMapping(putMappingRequest, new ActionListener<PutMappingResponse>() {
60-
@Override public void onResponse(PutMappingResponse result) {
60+
@Override public void onResponse(PutMappingResponse response) {
6161
try {
6262
JsonBuilder builder = RestJsonBuilder.cached(request);
6363
builder.startObject()
6464
.field("ok", true)
65-
.endObject();
65+
.field("acknowledged", response.acknowledged());
66+
builder.raw(", \"parsedSource\" : ");
67+
builder.raw(response.parsedSource());
68+
builder.endObject();
6669
channel.sendResponse(new JsonRestResponse(request, OK, builder));
6770
} catch (IOException e) {
6871
onFailure(e);

modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/json/simple/SimpleJsonMapperTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class SimpleJsonMapperTests {
6161
@Test public void testParseToJsonAndParse() throws Exception {
6262
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/json/simple/test-mapping.json");
6363
JsonDocumentMapper docMapper = (JsonDocumentMapper) new JsonDocumentMapperParser(new AnalysisService(new Index("test"))).parse(mapping);
64-
String builtMapping = docMapper.toJson();
64+
String builtMapping = docMapper.buildSource();
6565
System.out.println(builtMapping);
6666
// reparse it
6767
JsonDocumentMapper builtDocMapper = (JsonDocumentMapper) new JsonDocumentMapperParser(new AnalysisService(new Index("test"))).parse(builtMapping);

0 commit comments

Comments
 (0)