Get template should return warmers

Fixes #2868
This commit is contained in:
Igor Motov 2013-04-08 19:12:20 -04:00
parent a10c80e20f
commit acc0950957
3 changed files with 60 additions and 39 deletions

View File

@ -240,14 +240,30 @@ public class IndexTemplateMetaData {
}
builder.endObject();
builder.startArray("mappings");
for (Map.Entry<String, CompressedString> entry : indexTemplateMetaData.mappings().entrySet()) {
byte[] data = entry.getValue().uncompressed();
XContentParser parser = XContentFactory.xContent(data).createParser(data);
Map<String, Object> mapping = parser.mapOrderedAndClose();
builder.map(mapping);
if (params.paramAsBoolean("reduce_mappings", false)) {
builder.startObject("mappings");
for (Map.Entry<String, CompressedString> entry : indexTemplateMetaData.mappings().entrySet()) {
byte[] mappingSource = entry.getValue().uncompressed();
XContentParser parser = XContentFactory.xContent(mappingSource).createParser(mappingSource);
Map<String, Object> mapping = parser.map();
if (mapping.size() == 1 && mapping.containsKey(entry.getKey())) {
// the type name is the root value, reduce it
mapping = (Map<String, Object>) mapping.get(entry.getKey());
}
builder.field(entry.getKey());
builder.map(mapping);
}
builder.endObject();
} else {
builder.startArray("mappings");
for (Map.Entry<String, CompressedString> entry : indexTemplateMetaData.mappings().entrySet()) {
byte[] data = entry.getValue().uncompressed();
XContentParser parser = XContentFactory.xContent(data).createParser(data);
Map<String, Object> mapping = parser.mapOrderedAndClose();
builder.map(mapping);
}
builder.endArray();
}
builder.endArray();
for (Map.Entry<String, IndexMetaData.Custom> entry : indexTemplateMetaData.customs().entrySet()) {
builder.startObject(entry.getKey(), XContentBuilder.FieldCaseConversion.NONE);

View File

@ -26,8 +26,6 @@ import java.util.Map;
/**
* An interface allowing to transfer an object to "XContent" using an {@link XContentBuilder}.
*
*
*/
public interface ToXContent {
@ -100,5 +98,35 @@ public interface ToXContent {
}
}
public static class DelegatingMapParams extends MapParams {
private final Params delegate;
public DelegatingMapParams(Map<String, String> params, Params delegate) {
super(params);
this.delegate = delegate;
}
@Override
public String param(String key) {
return super.param(key, delegate.param(key));
}
@Override
public String param(String key, String defaultValue) {
return super.param(key, delegate.param(key, defaultValue));
}
@Override
public boolean paramAsBoolean(String key, boolean defaultValue) {
return super.paramAsBoolean(key, delegate.paramAsBoolean(key, defaultValue));
}
@Override
public Boolean paramAsBooleanOptional(String key, Boolean defaultValue) {
return super.paramAsBooleanOptional(key, delegate.paramAsBooleanOptional(key, defaultValue));
}
}
XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException;
}

View File

@ -19,6 +19,7 @@
package org.elasticsearch.rest.action.admin.indices.template.get;
import com.google.common.collect.Maps;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
@ -26,13 +27,11 @@ import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.compress.CompressedString;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsFilter;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
@ -71,39 +70,17 @@ public class RestGetIndexTemplateAction extends BaseRestHandler {
client.admin().cluster().state(clusterStateRequest, new ActionListener<ClusterStateResponse>() {
@Override
public void onResponse(ClusterStateResponse response) {
Map<String, String> paramsMap = Maps.newHashMap();
paramsMap.put("reduce_mappings", "true");
ToXContent.Params params = new ToXContent.DelegatingMapParams(paramsMap, request);
try {
MetaData metaData = response.getState().metaData();
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
for (IndexTemplateMetaData indexMetaData : metaData.templates().values()) {
builder.startObject(indexMetaData.name(), XContentBuilder.FieldCaseConversion.NONE);
builder.field("template", indexMetaData.template());
builder.field("order", indexMetaData.order());
builder.startObject("settings");
Settings settings = settingsFilter.filterSettings(indexMetaData.settings());
for (Map.Entry<String, String> entry : settings.getAsMap().entrySet()) {
builder.field(entry.getKey(), entry.getValue());
}
builder.endObject();
builder.startObject("mappings");
for (Map.Entry<String, CompressedString> entry : indexMetaData.mappings().entrySet()) {
byte[] mappingSource = entry.getValue().uncompressed();
XContentParser parser = XContentFactory.xContent(mappingSource).createParser(mappingSource);
Map<String, Object> mapping = parser.map();
if (mapping.size() == 1 && mapping.containsKey(entry.getKey())) {
// the type name is the root value, reduce it
mapping = (Map<String, Object>) mapping.get(entry.getKey());
}
builder.field(entry.getKey());
builder.map(mapping);
}
builder.endObject();
builder.endObject();
IndexTemplateMetaData.Builder.toXContent(indexMetaData, builder, params);
}
builder.endObject();