The nonNestedFilter should always be included if no types have been specified and

null can only be returned if there are no type or alias filter and the index has nested fields.
This commit is contained in:
Martijn van Groningen 2016-01-20 23:16:42 +01:00
parent 7914d0f940
commit cdecf725f8
2 changed files with 17 additions and 5 deletions

View File

@ -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);

View File

@ -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));
}