better failure when no filter is provided to bool filter

This commit is contained in:
Shay Banon 2012-07-14 13:17:21 +02:00
parent c3ccfe001a
commit bd219f3b34
1 changed files with 13 additions and 0 deletions

View File

@ -57,21 +57,27 @@ public class BoolFilterParser implements FilterParser {
String filterName = null; String filterName = null;
String currentFieldName = null; String currentFieldName = null;
XContentParser.Token token; XContentParser.Token token;
boolean hasAnyFilter = false;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName(); currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) { } else if (token == XContentParser.Token.START_OBJECT) {
if ("must".equals(currentFieldName)) { if ("must".equals(currentFieldName)) {
hasAnyFilter = true;
Filter filter = parseContext.parseInnerFilter(); Filter filter = parseContext.parseInnerFilter();
if (filter != null) { if (filter != null) {
boolFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST)); boolFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST));
} }
} else if ("must_not".equals(currentFieldName) || "mustNot".equals(currentFieldName)) { } else if ("must_not".equals(currentFieldName) || "mustNot".equals(currentFieldName)) {
hasAnyFilter = true;
Filter filter = parseContext.parseInnerFilter(); Filter filter = parseContext.parseInnerFilter();
if (filter != null) { if (filter != null) {
boolFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST_NOT)); boolFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST_NOT));
} }
} else if ("should".equals(currentFieldName)) { } else if ("should".equals(currentFieldName)) {
hasAnyFilter = true;
Filter filter = parseContext.parseInnerFilter(); Filter filter = parseContext.parseInnerFilter();
if (filter != null) { if (filter != null) {
boolFilter.add(new FilterClause(filter, BooleanClause.Occur.SHOULD)); boolFilter.add(new FilterClause(filter, BooleanClause.Occur.SHOULD));
@ -81,6 +87,7 @@ public class BoolFilterParser implements FilterParser {
} }
} else if (token == XContentParser.Token.START_ARRAY) { } else if (token == XContentParser.Token.START_ARRAY) {
if ("must".equals(currentFieldName)) { if ("must".equals(currentFieldName)) {
hasAnyFilter = true;
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
Filter filter = parseContext.parseInnerFilter(); Filter filter = parseContext.parseInnerFilter();
if (filter != null) { if (filter != null) {
@ -88,6 +95,7 @@ public class BoolFilterParser implements FilterParser {
} }
} }
} else if ("must_not".equals(currentFieldName) || "mustNot".equals(currentFieldName)) { } else if ("must_not".equals(currentFieldName) || "mustNot".equals(currentFieldName)) {
hasAnyFilter = true;
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
Filter filter = parseContext.parseInnerFilter(); Filter filter = parseContext.parseInnerFilter();
if (filter != null) { if (filter != null) {
@ -95,6 +103,7 @@ public class BoolFilterParser implements FilterParser {
} }
} }
} else if ("should".equals(currentFieldName)) { } else if ("should".equals(currentFieldName)) {
hasAnyFilter = true;
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
Filter filter = parseContext.parseInnerFilter(); Filter filter = parseContext.parseInnerFilter();
if (filter != null) { if (filter != null) {
@ -117,6 +126,10 @@ public class BoolFilterParser implements FilterParser {
} }
} }
if (!hasAnyFilter) {
throw new QueryParsingException(parseContext.index(), "[bool] filter has no inner should/must/must_not elements");
}
if ((boolFilter.getMustFilters() == null || boolFilter.getMustFilters().isEmpty()) if ((boolFilter.getMustFilters() == null || boolFilter.getMustFilters().isEmpty())
&& (boolFilter.getNotFilters() == null || boolFilter.getNotFilters().isEmpty()) && (boolFilter.getNotFilters() == null || boolFilter.getNotFilters().isEmpty())
&& (boolFilter.getShouldFilters() == null || boolFilter.getShouldFilters().isEmpty())) { && (boolFilter.getShouldFilters() == null || boolFilter.getShouldFilters().isEmpty())) {