diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponse.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponse.java index cce7cf2027c..9cdee21a345 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponse.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponse.java @@ -31,9 +31,34 @@ import java.io.IOException; */ public class PutMappingResponse implements ActionResponse, Streamable { + private boolean acknowledged; + + private String parsedSource; + + PutMappingResponse() { + + } + + public PutMappingResponse(boolean acknowledged, String parsedSource) { + this.acknowledged = acknowledged; + this.parsedSource = parsedSource; + } + + public boolean acknowledged() { + return acknowledged; + } + + public String parsedSource() { + return parsedSource; + } + @Override public void readFrom(DataInput in) throws IOException, ClassNotFoundException { + acknowledged = in.readBoolean(); + parsedSource = in.readUTF(); } @Override public void writeTo(DataOutput out) throws IOException { + out.writeBoolean(acknowledged); + out.writeUTF(parsedSource); } } \ 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 ba134735770..b71511764d4 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 @@ -74,8 +74,8 @@ public class TransportPutMappingAction extends TransportMasterNodeOperationActio @Override protected PutMappingResponse masterOperation(PutMappingRequest request) throws ElasticSearchException { final String[] indices = processIndices(clusterService.state(), request.indices()); - metaDataService.addMapping(indices, request.type(), request.mappingSource(), request.timeout()); - return new PutMappingResponse(); + MetaDataService.PutMappingResult result = metaDataService.putMapping(indices, request.type(), request.mappingSource(), request.timeout()); + return new PutMappingResponse(result.acknowledged(), result.parsedSource()); } @Override protected void doExecute(final PutMappingRequest request, final ActionListener listener) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataService.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataService.java index b0ae246f5d4..18062ad7478 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataService.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataService.java @@ -200,7 +200,7 @@ public class MetaDataService extends AbstractComponent { } } - public boolean addMapping(final String[] indices, String mappingType, final String mappingSource, TimeValue timeout) throws ElasticSearchException { + public PutMappingResult putMapping(final String[] indices, String mappingType, final String mappingSource, TimeValue timeout) throws ElasticSearchException { ClusterState clusterState = clusterService.state(); for (String index : indices) { IndexRoutingTable indexTable = clusterState.routingTable().indicesRouting().get(index); @@ -220,6 +220,8 @@ public class MetaDataService extends AbstractComponent { } } + String parsedSource = documentMapper.buildSource(); + if (mappingType == null) { mappingType = documentMapper.type(); } else if (!mappingType.equals(documentMapper.type())) { @@ -229,7 +231,7 @@ public class MetaDataService extends AbstractComponent { throw new InvalidTypeNameException("Document mapping type name can't start with '_'"); } - logger.info("Indices [" + Arrays.toString(indices) + "]: Creating mapping [" + mappingType + "] with source [" + mappingSource + "]"); + logger.info("Indices [" + Arrays.toString(indices) + "]: Put mapping [" + mappingType + "] with source [" + mappingSource + "]"); final CountDownLatch latch = new CountDownLatch(clusterService.state().nodes().size() * indices.length); final Set indicesSet = Sets.newHashSet(indices); @@ -258,13 +260,35 @@ public class MetaDataService extends AbstractComponent { } }); + boolean acknowledged; try { - return latch.await(timeout.millis(), TimeUnit.MILLISECONDS); + acknowledged = latch.await(timeout.millis(), TimeUnit.MILLISECONDS); } catch (InterruptedException e) { - return false; + acknowledged = false; } finally { nodeMappingCreatedAction.remove(listener); } + + return new PutMappingResult(acknowledged, parsedSource); } + public static class PutMappingResult { + + private final boolean acknowledged; + + private final String parsedSource; + + public PutMappingResult(boolean acknowledged, String parsedSource) { + this.acknowledged = acknowledged; + this.parsedSource = parsedSource; + } + + public boolean acknowledged() { + return acknowledged; + } + + public String parsedSource() { + return parsedSource; + } + } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java index 74aabf1fe7a..8e279dcda92 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java @@ -37,6 +37,11 @@ public interface DocumentMapper { */ String mappingSource(); + /** + * Generates the source of the mapper based on the current mappings. + */ + String buildSource() throws FailedToGenerateSourceMapperException; + UidFieldMapper uidMapper(); IdFieldMapper idMapper(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FailedToGenerateSourceMapperException.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FailedToGenerateSourceMapperException.java new file mode 100644 index 00000000000..7689929b5fe --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FailedToGenerateSourceMapperException.java @@ -0,0 +1,34 @@ +/* + * 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.index.mapper; + +/** + * @author kimchy (shay.banon) + */ +public class FailedToGenerateSourceMapperException extends MapperException { + + public FailedToGenerateSourceMapperException(String message) { + super(message); + } + + public FailedToGenerateSourceMapperException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonDocumentMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonDocumentMapper.java index 666ba426871..285d63fb232 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonDocumentMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonDocumentMapper.java @@ -339,12 +339,17 @@ public class JsonDocumentMapper implements DocumentMapper, ToJson { } } - public String toJson() throws IOException { - JsonBuilder builder = jsonBuilder().prettyPrint(); - builder.startObject(); - toJson(builder, ToJson.EMPTY_PARAMS); - builder.endObject(); - return builder.string(); + + @Override public String buildSource() throws FailedToGenerateSourceMapperException { + try { + JsonBuilder builder = jsonBuilder().prettyPrint(); + builder.startObject(); + toJson(builder, ToJson.EMPTY_PARAMS); + builder.endObject(); + return builder.string(); + } catch (Exception e) { + throw new FailedToGenerateSourceMapperException(e.getMessage(), e); + } } @Override public void toJson(JsonBuilder builder, Params params) throws IOException { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/mapping/put/RestPutMappingAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/mapping/put/RestPutMappingAction.java index bd9c95ae7e3..06be304d505 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/mapping/put/RestPutMappingAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/mapping/put/RestPutMappingAction.java @@ -57,12 +57,15 @@ public class RestPutMappingAction extends BaseRestHandler { putMappingRequest.mappingSource(request.contentAsString()); putMappingRequest.timeout(request.paramAsTime("timeout", timeValueSeconds(10))); client.admin().indices().execPutMapping(putMappingRequest, new ActionListener() { - @Override public void onResponse(PutMappingResponse result) { + @Override public void onResponse(PutMappingResponse response) { try { JsonBuilder builder = RestJsonBuilder.cached(request); builder.startObject() .field("ok", true) - .endObject(); + .field("acknowledged", response.acknowledged()); + builder.raw(", \"parsedSource\" : "); + builder.raw(response.parsedSource()); + builder.endObject(); channel.sendResponse(new JsonRestResponse(request, OK, builder)); } catch (IOException e) { onFailure(e); diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/json/simple/SimpleJsonMapperTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/json/simple/SimpleJsonMapperTests.java index a28eb5f09e9..3062435a042 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/json/simple/SimpleJsonMapperTests.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/json/simple/SimpleJsonMapperTests.java @@ -61,7 +61,7 @@ public class SimpleJsonMapperTests { @Test public void testParseToJsonAndParse() throws Exception { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/json/simple/test-mapping.json"); JsonDocumentMapper docMapper = (JsonDocumentMapper) new JsonDocumentMapperParser(new AnalysisService(new Index("test"))).parse(mapping); - String builtMapping = docMapper.toJson(); + String builtMapping = docMapper.buildSource(); System.out.println(builtMapping); // reparse it JsonDocumentMapper builtDocMapper = (JsonDocumentMapper) new JsonDocumentMapperParser(new AnalysisService(new Index("test"))).parse(builtMapping);