From bd219f3b342159884f9ff0f82903ecf529a52d63 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Sat, 14 Jul 2012 13:17:21 +0200 Subject: [PATCH] better failure when no filter is provided to bool filter --- .../elasticsearch/index/query/BoolFilterParser.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/java/org/elasticsearch/index/query/BoolFilterParser.java b/src/main/java/org/elasticsearch/index/query/BoolFilterParser.java index 2b85b2465ef..5313001311c 100644 --- a/src/main/java/org/elasticsearch/index/query/BoolFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/BoolFilterParser.java @@ -57,21 +57,27 @@ public class BoolFilterParser implements FilterParser { String filterName = null; String currentFieldName = null; XContentParser.Token token; + + boolean hasAnyFilter = false; + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_OBJECT) { if ("must".equals(currentFieldName)) { + hasAnyFilter = true; Filter filter = parseContext.parseInnerFilter(); if (filter != null) { boolFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST)); } } else if ("must_not".equals(currentFieldName) || "mustNot".equals(currentFieldName)) { + hasAnyFilter = true; Filter filter = parseContext.parseInnerFilter(); if (filter != null) { boolFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST_NOT)); } } else if ("should".equals(currentFieldName)) { + hasAnyFilter = true; Filter filter = parseContext.parseInnerFilter(); if (filter != null) { boolFilter.add(new FilterClause(filter, BooleanClause.Occur.SHOULD)); @@ -81,6 +87,7 @@ public class BoolFilterParser implements FilterParser { } } else if (token == XContentParser.Token.START_ARRAY) { if ("must".equals(currentFieldName)) { + hasAnyFilter = true; while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { Filter filter = parseContext.parseInnerFilter(); if (filter != null) { @@ -88,6 +95,7 @@ public class BoolFilterParser implements FilterParser { } } } else if ("must_not".equals(currentFieldName) || "mustNot".equals(currentFieldName)) { + hasAnyFilter = true; while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { Filter filter = parseContext.parseInnerFilter(); if (filter != null) { @@ -95,6 +103,7 @@ public class BoolFilterParser implements FilterParser { } } } else if ("should".equals(currentFieldName)) { + hasAnyFilter = true; while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { Filter filter = parseContext.parseInnerFilter(); 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()) && (boolFilter.getNotFilters() == null || boolFilter.getNotFilters().isEmpty()) && (boolFilter.getShouldFilters() == null || boolFilter.getShouldFilters().isEmpty())) {