Client: Make the query builder nullable in filteredQuery.

Close #7398
This commit is contained in:
Michael Brackx 2014-08-22 10:58:41 +02:00 committed by Adrien Grand
parent d7a068d02c
commit 0fd3ef6df0
3 changed files with 19 additions and 5 deletions

View File

@ -40,10 +40,10 @@ public class FilteredQueryBuilder extends BaseQueryBuilder implements BoostableQ
/**
* A query that applies a filter to the results of another query.
*
* @param queryBuilder The query to apply the filter to
* @param queryBuilder The query to apply the filter to (Can be null)
* @param filterBuilder The filter to apply on the query (Can be null)
*/
public FilteredQueryBuilder(QueryBuilder queryBuilder, @Nullable FilterBuilder filterBuilder) {
public FilteredQueryBuilder(@Nullable QueryBuilder queryBuilder, @Nullable FilterBuilder filterBuilder) {
this.queryBuilder = queryBuilder;
this.filterBuilder = filterBuilder;
}
@ -68,8 +68,10 @@ public class FilteredQueryBuilder extends BaseQueryBuilder implements BoostableQ
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(FilteredQueryParser.NAME);
builder.field("query");
queryBuilder.toXContent(builder, params);
if (queryBuilder != null) {
builder.field("query");
queryBuilder.toXContent(builder, params);
}
if (filterBuilder != null) {
builder.field("filter");
filterBuilder.toXContent(builder, params);

View File

@ -395,7 +395,7 @@ public abstract class QueryBuilders {
* @param queryBuilder The query to apply the filter to
* @param filterBuilder The filter to apply on the query
*/
public static FilteredQueryBuilder filteredQuery(QueryBuilder queryBuilder, @Nullable FilterBuilder filterBuilder) {
public static FilteredQueryBuilder filteredQuery(@Nullable QueryBuilder queryBuilder, @Nullable FilterBuilder filterBuilder) {
return new FilteredQueryBuilder(queryBuilder, filterBuilder);
}

View File

@ -2662,6 +2662,18 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
}
}
@Test // see #7365
public void testFilteredQueryWithoutQuery() throws Exception {
createIndex("test");
ensureYellow("test");
indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "value1"));
SearchResponse response = client().prepareSearch()
.setQuery(QueryBuilders.filteredQuery(null,
FilterBuilders.termFilter("field1", "value1"))).get();
assertSearchResponse(response);
assertHitCount(response, 1l);
}
@Test
public void testQueryStringParserCache() throws Exception {
createIndex("test");