Fix for issue #1860 : Index Templates API - Set Source

This commit is contained in:
Nicolas Huray 2012-04-15 12:53:07 +02:00 committed by Shay Banon
parent 62954e6d1f
commit f1a8e7e4c7
3 changed files with 87 additions and 27 deletions

View File

@ -32,6 +32,7 @@ import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import java.io.IOException;
import java.util.Map;
@ -150,7 +151,7 @@ public class PutIndexTemplateRequest extends MasterNodeOperationRequest {
}
/**
* The settings to crete the index template with (either json/yaml/properties format).
* The settings to create the index template with (either json/yaml/properties format).
*/
public PutIndexTemplateRequest settings(String source) {
this.settings = ImmutableSettings.settingsBuilder().loadFromSource(source).build();
@ -237,6 +238,66 @@ public class PutIndexTemplateRequest extends MasterNodeOperationRequest {
return this.mappings;
}
/**
* The template source definition.
*/
public PutIndexTemplateRequest source(XContentBuilder templateBuilder) {
try {
return source(templateBuilder.string());
} catch (IOException e) {
throw new ElasticSearchIllegalArgumentException("Failed to build json for template request", e);
}
}
/**
* The template source definition.
*/
public PutIndexTemplateRequest source(Map templateSource) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
builder.map(templateSource);
return source(builder.string());
} catch (IOException e) {
throw new ElasticSearchGenerationException("Failed to generate [" + templateSource + "]", e);
}
}
/**
* The template source definition.
*/
public PutIndexTemplateRequest source(String templateSource) {
// parse source
Map<String, Object> source = null;
try {
source = XContentFactory.xContent(templateSource)
.createParser(templateSource).mapOrderedAndClose();
if (source.containsKey("template")) {
template(source.get("template").toString());
}
if (source.containsKey("order")) {
order(XContentMapValues.nodeIntegerValue(source.get("order"), order()));
}
if (source.containsKey("settings")) {
if (!(source.get("settings") instanceof Map)) {
throw new ElasticSearchIllegalArgumentException("Malformed settings section, should include an inner object");
}
settings((Map<String, Object>) source.get("settings"));
}
if (source.containsKey("mappings")) {
Map<String, Object> mappings = (Map<String, Object>) source.get("mappings");
for (Map.Entry<String, Object> entry : mappings.entrySet()) {
if (!(entry.getValue() instanceof Map)) {
throw new ElasticSearchIllegalArgumentException("Malformed mappings section for type [" + entry.getKey() + "], should include an inner object describing the mapping");
}
mapping(entry.getKey(), (Map<String, Object>) entry.getValue());
}
}
} catch (IOException e) {
throw new ElasticSearchIllegalArgumentException("Malformed template source", e);
}
return this;
}
/**
* Timeout to wait till the put mapping gets acknowledged of all current cluster nodes. Defaults to

View File

@ -139,6 +139,30 @@ public class PutIndexTemplateRequestBuilder extends BaseIndicesRequestBuilder<Pu
return this;
}
/**
* The template source definition.
*/
public PutIndexTemplateRequestBuilder setSource(XContentBuilder templateBuilder) {
request.source(templateBuilder);
return this;
}
/**
* The template source definition.
*/
public PutIndexTemplateRequestBuilder setSource(Map templateSource) {
request.source(templateSource);
return this;
}
/**
* The template source definition.
*/
public PutIndexTemplateRequestBuilder setSource(String templateSource) {
request.source(templateSource);
return this;
}
/**
* Timeout to wait for the index creation to be acknowledged by current cluster nodes. Defaults
* to <tt>10s</tt>.

View File

@ -68,32 +68,7 @@ public class RestPutIndexTemplateAction extends BaseRestHandler {
putRequest.create(request.paramAsBoolean("create", false));
putRequest.cause(request.param("cause", ""));
putRequest.timeout(request.paramAsTime("timeout", timeValueSeconds(10)));
// parse the parameters
Map<String, Object> source = XContentFactory.xContent(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength())
.createParser(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength()).mapOrderedAndClose();
if (source.containsKey("template")) {
putRequest.template(source.get("template").toString());
}
if (source.containsKey("order")) {
putRequest.order(XContentMapValues.nodeIntegerValue(source.get("order"), putRequest.order()));
}
if (source.containsKey("settings")) {
if (!(source.get("settings") instanceof Map)) {
throw new ElasticSearchIllegalArgumentException("Malformed settings section, should include an inner object");
}
putRequest.settings((Map<String, Object>) source.get("settings"));
}
if (source.containsKey("mappings")) {
Map<String, Object> mappings = (Map<String, Object>) source.get("mappings");
for (Map.Entry<String, Object> entry : mappings.entrySet()) {
if (!(entry.getValue() instanceof Map)) {
throw new ElasticSearchIllegalArgumentException("Malformed mappings section for type [" + entry.getKey() + "], should include an inner object describing the mapping");
}
putRequest.mapping(entry.getKey(), (Map<String, Object>) entry.getValue());
}
}
putRequest.source(request.contentAsString());
} catch (Exception e) {
try {
channel.sendResponse(new XContentThrowableRestResponse(request, e));