Skip to content

Commit 4b04db9

Browse files
committed
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.
1 parent 7c68489 commit 4b04db9

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ private Document loadDocument(SearchContext context, FieldSelector fieldSelector
152152
}
153153

154154
private FieldSelector buildFieldSelectors(SearchContext context) {
155-
if (context.fieldNames() == null || context.fieldNames().length == 0) {
155+
if (context.fieldNames() == null) {
156156
return new UidAndSourceFieldSelector();
157157
}
158158

modules/elasticsearch/src/main/java/org/elasticsearch/search/fetch/FieldsParseElement.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.codehaus.jackson.JsonToken;
2424
import org.elasticsearch.search.SearchParseElement;
2525
import org.elasticsearch.search.internal.SearchContext;
26+
import org.elasticsearch.util.Strings;
2627

2728
import java.util.ArrayList;
2829

@@ -34,12 +35,15 @@ public class FieldsParseElement implements SearchParseElement {
3435
@Override public void parse(JsonParser jp, SearchContext context) throws Exception {
3536
JsonToken token = jp.getCurrentToken();
3637
if (token == JsonToken.START_ARRAY) {
37-
jp.nextToken();
3838
ArrayList<String> fieldNames = new ArrayList<String>();
39-
do {
39+
while ((token = jp.nextToken()) != JsonToken.END_ARRAY) {
4040
fieldNames.add(jp.getText());
41-
} while ((token = jp.nextToken()) != JsonToken.END_ARRAY);
42-
context.fieldNames(fieldNames.toArray(new String[fieldNames.size()]));
41+
}
42+
if (fieldNames.isEmpty()) {
43+
context.fieldNames(Strings.EMPTY_ARRAY);
44+
} else {
45+
context.fieldNames(fieldNames.toArray(new String[fieldNames.size()]));
46+
}
4347
} else if (token == JsonToken.VALUE_STRING) {
4448
context.fieldNames(new String[]{jp.getText()});
4549
}

0 commit comments

Comments
 (0)