diff --git a/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java index 61901f0392e..e5113bbc542 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java @@ -255,14 +255,17 @@ public class DefaultSearchContext extends SearchContext { typesBytes[i] = new BytesRef(types[i]); } typesFilter = new TermsQuery(TypeFieldMapper.NAME, typesBytes); - } else if (hasNestedFields) { - typesFilter = Queries.newNonNestedFilter(); - } else if (aliasFilter == null) { + } + + if (typesFilter == null && aliasFilter == null && hasNestedFields == false) { return null; } + BooleanQuery.Builder bq = new BooleanQuery.Builder(); if (typesFilter != null) { bq.add(typesFilter, Occur.FILTER); + } else if (hasNestedFields) { + bq.add(Queries.newNonNestedFilter(), Occur.FILTER); } if (aliasFilter != null) { bq.add(aliasFilter, Occur.FILTER); diff --git a/core/src/test/java/org/elasticsearch/search/internal/DefaultSearchContextTests.java b/core/src/test/java/org/elasticsearch/search/internal/DefaultSearchContextTests.java index 1372aa73b49..d8fe2308bc7 100644 --- a/core/src/test/java/org/elasticsearch/search/internal/DefaultSearchContextTests.java +++ b/core/src/test/java/org/elasticsearch/search/internal/DefaultSearchContextTests.java @@ -55,8 +55,17 @@ public class DefaultSearchContextTests extends ESTestCase { expectedQuery = new BooleanQuery.Builder().add(Queries.newNonNestedFilter(), FILTER).build(); assertThat(searchFilter, equalTo(expectedQuery)); - searchFilter = DefaultSearchContext.createSearchFilter(null, new MatchAllDocsQuery(), randomBoolean()); - expectedQuery = new BooleanQuery.Builder().add(new MatchAllDocsQuery(), FILTER).build(); + searchFilter = DefaultSearchContext.createSearchFilter(null, new MatchAllDocsQuery(), true); + expectedQuery = new BooleanQuery.Builder() + .add(new MatchAllDocsQuery(), FILTER) + .add(Queries.newNonNestedFilter(), FILTER) + .build(); + assertThat(searchFilter, equalTo(expectedQuery)); + + searchFilter = DefaultSearchContext.createSearchFilter(null, new MatchAllDocsQuery(), false); + expectedQuery = new BooleanQuery.Builder() + .add(new MatchAllDocsQuery(), FILTER) + .build(); assertThat(searchFilter, equalTo(expectedQuery)); }