From 0fd3ef6df0e403c0e527a24cde2b3103a5dc8a5f Mon Sep 17 00:00:00 2001 From: Michael Brackx Date: Fri, 22 Aug 2014 10:58:41 +0200 Subject: [PATCH] Client: Make the query builder nullable in filteredQuery. Close #7398 --- .../index/query/FilteredQueryBuilder.java | 10 ++++++---- .../org/elasticsearch/index/query/QueryBuilders.java | 2 +- .../elasticsearch/search/query/SimpleQueryTests.java | 12 ++++++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/query/FilteredQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/FilteredQueryBuilder.java index 2819df55fdb..179cea860e4 100644 --- a/src/main/java/org/elasticsearch/index/query/FilteredQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/FilteredQueryBuilder.java @@ -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); diff --git a/src/main/java/org/elasticsearch/index/query/QueryBuilders.java b/src/main/java/org/elasticsearch/index/query/QueryBuilders.java index 0e9243b4955..63f0bbb7deb 100644 --- a/src/main/java/org/elasticsearch/index/query/QueryBuilders.java +++ b/src/main/java/org/elasticsearch/index/query/QueryBuilders.java @@ -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); } diff --git a/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java b/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java index ae6d8addb5a..30038ce18cf 100644 --- a/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java +++ b/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java @@ -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");