Getting _settings or _mapping for non-existing index returns 200 OK, closes #1828.

This commit is contained in:
Shay Banon 2012-04-03 14:35:10 +03:00
parent cdfa87827a
commit 009731c9e7
2 changed files with 23 additions and 7 deletions

View File

@ -41,6 +41,7 @@ import java.io.IOException;
import java.util.Set;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
import static org.elasticsearch.rest.RestStatus.OK;
import static org.elasticsearch.rest.action.support.RestActions.splitIndices;
import static org.elasticsearch.rest.action.support.RestActions.splitTypes;
@ -72,15 +73,18 @@ public class RestGetMappingAction extends BaseRestHandler {
@Override
public void onResponse(ClusterStateResponse response) {
try {
boolean foundAny = false;
MetaData metaData = response.state().metaData();
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
if (indices.length == 1 && metaData.indices().isEmpty()) {
channel.sendResponse(new XContentThrowableRestResponse(request, new IndexMissingException(new Index(indices[0]))));
return;
}
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()) {
@ -88,6 +92,7 @@ public class RestGetMappingAction extends BaseRestHandler {
// filter this type out...
continue;
}
foundAny = true;
foundType = true;
builder.field(mappingMd.type());
builder.map(mappingMd.sourceAsMap());
@ -105,6 +110,7 @@ public class RestGetMappingAction extends BaseRestHandler {
// filter this type out...
continue;
}
foundAny = true;
builder.field(mappingMd.type());
builder.map(mappingMd.sourceAsMap());
}
@ -115,7 +121,7 @@ public class RestGetMappingAction extends BaseRestHandler {
builder.endObject();
channel.sendResponse(new XContentRestResponse(request, OK, builder));
channel.sendResponse(new XContentRestResponse(request, foundAny ? OK : NOT_FOUND, builder));
} catch (Exception e) {
onFailure(e);
}

View File

@ -30,6 +30,8 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsFilter;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.Index;
import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
@ -37,6 +39,7 @@ import java.io.IOException;
import java.util.Map;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
import static org.elasticsearch.rest.RestStatus.OK;
import static org.elasticsearch.rest.action.support.RestActions.splitIndices;
@ -67,12 +70,19 @@ public class RestGetSettingsAction extends BaseRestHandler {
public void onResponse(ClusterStateResponse response) {
try {
MetaData metaData = response.state().metaData();
if (metaData.indices().isEmpty()) {
channel.sendResponse(new XContentThrowableRestResponse(request, new IndexMissingException(new Index(indices[0]))));
return;
}
boolean foundAny = false;
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
for (IndexMetaData indexMetaData : metaData) {
builder.startObject(indexMetaData.index(), XContentBuilder.FieldCaseConversion.NONE);
foundAny = true;
builder.startObject("settings");
Settings settings = settingsFilter.filterSettings(indexMetaData.settings());
for (Map.Entry<String, String> entry : settings.getAsMap().entrySet()) {
@ -85,7 +95,7 @@ public class RestGetSettingsAction extends BaseRestHandler {
builder.endObject();
channel.sendResponse(new XContentRestResponse(request, OK, builder));
channel.sendResponse(new XContentRestResponse(request, foundAny ? OK : NOT_FOUND, builder));
} catch (Exception e) {
onFailure(e);
}