Skip to content

Commit

Permalink
Getting _settings or _mapping for non-existing index returns 200 OK, c…
Browse files Browse the repository at this point in the history
…loses #1828.
  • Loading branch information
kimchy committed Apr 3, 2012
1 parent 587e4f8 commit a53006f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
Expand Up @@ -41,6 +41,7 @@
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;
Expand Down Expand Up @@ -72,22 +73,26 @@ public void handleRequest(final RestRequest request, final RestChannel channel)
@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()) {
if (!types.isEmpty() && !types.contains(mappingMd.type())) {
// filter this type out...
continue;
}
foundAny = true;
foundType = true;
builder.field(mappingMd.type());
builder.map(mappingMd.sourceAsMap());
Expand All @@ -105,6 +110,7 @@ public void onResponse(ClusterStateResponse response) {
// filter this type out...
continue;
}
foundAny = true;
builder.field(mappingMd.type());
builder.map(mappingMd.sourceAsMap());
}
Expand All @@ -115,7 +121,7 @@ public void onResponse(ClusterStateResponse response) {

builder.endObject();

channel.sendResponse(new XContentRestResponse(request, OK, builder));
channel.sendResponse(new XContentRestResponse(request, foundAny ? OK : NOT_FOUND, builder));
} catch (Exception e) {
onFailure(e);
}
Expand Down
Expand Up @@ -30,13 +30,16 @@
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;

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;

Expand Down Expand Up @@ -67,12 +70,19 @@ public void handleRequest(final RestRequest request, final RestChannel channel)
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()) {
Expand All @@ -85,7 +95,7 @@ public void onResponse(ClusterStateResponse response) {

builder.endObject();

channel.sendResponse(new XContentRestResponse(request, OK, builder));
channel.sendResponse(new XContentRestResponse(request, foundAny ? OK : NOT_FOUND, builder));
} catch (Exception e) {
onFailure(e);
}
Expand Down

0 comments on commit a53006f

Please sign in to comment.