diff --git a/core/src/main/java/org/elasticsearch/index/query/AndQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/AndQueryBuilder.java index d309efa6e10..920e20f2be3 100644 --- a/core/src/main/java/org/elasticsearch/index/query/AndQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/AndQueryBuilder.java @@ -107,7 +107,11 @@ public class AndQueryBuilder extends AbstractQueryBuilder { BooleanQuery query = new BooleanQuery(); for (QueryBuilder f : filters) { - query.add(f.toQuery(parseContext), Occur.MUST); + Query innerQuery = f.toQuery(parseContext); + // ignore queries that are null + if (innerQuery != null) { + query.add(innerQuery, Occur.MUST); + } } if (queryName != null) { parseContext.addNamedQuery(queryName, query); diff --git a/core/src/main/java/org/elasticsearch/index/query/OrQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/OrQueryBuilder.java index fc26745cb8f..27d6298b01d 100644 --- a/core/src/main/java/org/elasticsearch/index/query/OrQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/OrQueryBuilder.java @@ -106,7 +106,11 @@ public class OrQueryBuilder extends AbstractQueryBuilder { BooleanQuery query = new BooleanQuery(); for (QueryBuilder f : filters) { - query.add(f.toQuery(parseContext), Occur.SHOULD); + Query innerQuery = f.toQuery(parseContext); + // ignore queries that are null + if (innerQuery != null) { + query.add(innerQuery, Occur.SHOULD); + } } if (queryName != null) { parseContext.addNamedQuery(queryName, query); diff --git a/core/src/test/java/org/elasticsearch/index/query/AndQueryBuilderTest.java b/core/src/test/java/org/elasticsearch/index/query/AndQueryBuilderTest.java index 98185b124a7..9c175bd5600 100644 --- a/core/src/test/java/org/elasticsearch/index/query/AndQueryBuilderTest.java +++ b/core/src/test/java/org/elasticsearch/index/query/AndQueryBuilderTest.java @@ -40,7 +40,10 @@ public class AndQueryBuilderTest extends BaseQueryTestCase { } BooleanQuery query = new BooleanQuery(); for (QueryBuilder subQuery : queryBuilder.filters()) { - query.add(subQuery.toQuery(context), Occur.MUST); + Query innerQuery = subQuery.toQuery(context); + if (innerQuery != null) { + query.add(innerQuery, Occur.MUST); + } } return query; } diff --git a/core/src/test/java/org/elasticsearch/index/query/OrQueryBuilderTest.java b/core/src/test/java/org/elasticsearch/index/query/OrQueryBuilderTest.java index 8169cbd37ad..db9b7ba1b27 100644 --- a/core/src/test/java/org/elasticsearch/index/query/OrQueryBuilderTest.java +++ b/core/src/test/java/org/elasticsearch/index/query/OrQueryBuilderTest.java @@ -40,7 +40,11 @@ public class OrQueryBuilderTest extends BaseQueryTestCase { } BooleanQuery query = new BooleanQuery(); for (QueryBuilder subQuery : queryBuilder.filters()) { - query.add(subQuery.toQuery(context), Occur.SHOULD); + Query innerQuery = subQuery.toQuery(context); + // ignore queries that are null + if (innerQuery != null) { + query.add(innerQuery, Occur.SHOULD); + } } return query; }