put mapping to return the parsed source and an indication if it was ack from all the nodes within the timeout
This commit is contained in:
parent
0e55c876a4
commit
78e73259a0
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<PutMappingResponse> listener) {
|
||||
|
|
|
@ -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<String> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -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<PutMappingResponse>() {
|
||||
@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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue