From ee5bfba52f0d79879c3df2c5cdaaafbdfa9c5a49 Mon Sep 17 00:00:00 2001 From: kimchy Date: Thu, 2 Jun 2011 18:30:06 +0300 Subject: [PATCH] Get Mapping: Better error response when asking for specific index type, closes #991. --- .../mapping/get/RestGetMappingAction.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/mapping/get/RestGetMappingAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/mapping/get/RestGetMappingAction.java index 4794569dd9b..d6541da0d6e 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/mapping/get/RestGetMappingAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/mapping/get/RestGetMappingAction.java @@ -33,7 +33,15 @@ import org.elasticsearch.common.settings.Settings; 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.index.Index; +import org.elasticsearch.indices.IndexMissingException; +import org.elasticsearch.indices.TypeMissingException; +import org.elasticsearch.rest.BaseRestHandler; +import org.elasticsearch.rest.RestChannel; +import org.elasticsearch.rest.RestController; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.XContentRestResponse; +import org.elasticsearch.rest.XContentThrowableRestResponse; import org.elasticsearch.rest.action.support.RestXContentBuilder; import java.io.IOException; @@ -73,12 +81,18 @@ public class RestGetMappingAction extends BaseRestHandler { builder.startObject(); if (indices.length == 1 && types.size() == 1) { + if (metaData.indices().isEmpty()) { + channel.sendResponse(new XContentThrowableRestResponse(request, new IndexMissingException(new Index(indices[0])))); + return; + } + boolean foundType = false; IndexMetaData indexMetaData = metaData.iterator().next(); for (MappingMetaData mappingMd : indexMetaData.mappings().values()) { if (!types.isEmpty() && !types.contains(mappingMd.type())) { // filter this type out... continue; } + foundType = true; byte[] mappingSource = mappingMd.source().uncompressed(); XContentParser parser = XContentFactory.xContent(mappingSource).createParser(mappingSource); Map mapping = parser.map(); @@ -89,6 +103,10 @@ public class RestGetMappingAction extends BaseRestHandler { builder.field(mappingMd.type()); builder.map(mapping); } + if (!foundType) { + channel.sendResponse(new XContentThrowableRestResponse(request, new TypeMissingException(new Index(indices[0]), types.iterator().next()))); + return; + } } else { for (IndexMetaData indexMetaData : metaData) { builder.startObject(indexMetaData.index());