A search with an empty fields param causes a NullPointerException or a runaway process. Changed logic for an empty fields array, where it won't return the source in this case. Closes #55.

This commit is contained in:
kimchy 2010-03-10 17:49:47 +02:00
parent 7c68489758
commit 4b04db9030
2 changed files with 9 additions and 5 deletions

View File

@ -152,7 +152,7 @@ public class FetchPhase implements SearchPhase {
}
private FieldSelector buildFieldSelectors(SearchContext context) {
if (context.fieldNames() == null || context.fieldNames().length == 0) {
if (context.fieldNames() == null) {
return new UidAndSourceFieldSelector();
}

View File

@ -23,6 +23,7 @@ import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.elasticsearch.search.SearchParseElement;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.util.Strings;
import java.util.ArrayList;
@ -34,12 +35,15 @@ public class FieldsParseElement implements SearchParseElement {
@Override public void parse(JsonParser jp, SearchContext context) throws Exception {
JsonToken token = jp.getCurrentToken();
if (token == JsonToken.START_ARRAY) {
jp.nextToken();
ArrayList<String> fieldNames = new ArrayList<String>();
do {
while ((token = jp.nextToken()) != JsonToken.END_ARRAY) {
fieldNames.add(jp.getText());
} while ((token = jp.nextToken()) != JsonToken.END_ARRAY);
context.fieldNames(fieldNames.toArray(new String[fieldNames.size()]));
}
if (fieldNames.isEmpty()) {
context.fieldNames(Strings.EMPTY_ARRAY);
} else {
context.fieldNames(fieldNames.toArray(new String[fieldNames.size()]));
}
} else if (token == JsonToken.VALUE_STRING) {
context.fieldNames(new String[]{jp.getText()});
}