Skip to content

Commit

Permalink
Fix for issue elastic#1860 : Index Templates API - Set Source
Browse files Browse the repository at this point in the history
  • Loading branch information
nhuray authored and kimchy committed Apr 15, 2012
1 parent 62954e6 commit f1a8e7e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 27 deletions.
Expand Up @@ -32,6 +32,7 @@
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;
Expand Down Expand Up @@ -150,7 +151,7 @@ public PutIndexTemplateRequest settings(Settings.Builder settings) {
}

/**
* 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();
Expand Down Expand Up @@ -237,6 +238,66 @@ Map<String, String> mappings() {
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
Expand Down
Expand Up @@ -139,6 +139,30 @@ public PutIndexTemplateRequestBuilder addMapping(String type, Map<String, Object
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>.
Expand Down
Expand Up @@ -68,32 +68,7 @@ public void handleRequest(final RestRequest request, final RestChannel channel)
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));
Expand Down

0 comments on commit f1a8e7e

Please sign in to comment.