diff --git a/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java index 261acff33ce..d9c277653e2 100644 --- a/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java @@ -263,13 +263,6 @@ public class BoolQueryBuilder extends AbstractQueryBuilder { @Override protected Query doToQuery(QueryShardContext context) throws IOException { - final int numClauses = mustNotClauses.size() + filterClauses.size() + shouldClauses.size() + mustClauses.size(); - if (numClauses == 1 && must().size() == 1 && boost() == DEFAULT_BOOST) { - // we really only optimize this for testing since we use this to rewrite - // named queries TemplateQueryBuilder and WrapperQueryBuilder. - // it's equivalent but will anyways happen later down the road in the BQ#rewrite method - return mustClauses.get(0).toQuery(context); - } BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder(); booleanQueryBuilder.setDisableCoord(disableCoord); addBooleanClauses(context, booleanQueryBuilder, mustClauses, BooleanClause.Occur.MUST); diff --git a/core/src/main/java/org/elasticsearch/index/query/EmptyQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/EmptyQueryBuilder.java index e810292a95c..b95befc0da8 100644 --- a/core/src/main/java/org/elasticsearch/index/query/EmptyQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/EmptyQueryBuilder.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.query; +import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.Query; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; diff --git a/core/src/main/java/org/elasticsearch/index/query/WrapperQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/WrapperQueryBuilder.java index 6c7ac0e9084..95086715f57 100644 --- a/core/src/main/java/org/elasticsearch/index/query/WrapperQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/WrapperQueryBuilder.java @@ -143,4 +143,6 @@ public class WrapperQueryBuilder extends AbstractQueryBuilder> assertLuceneQuery(secondQuery, secondLuceneQuery, context); SearchContext.removeCurrent(); - assertThat("two equivalent query builders lead to different lucene queries", secondLuceneQuery, equalTo(firstLuceneQuery)); + assertEquals("two equivalent query builders lead to different lucene queries", rewrite(secondLuceneQuery), rewrite(firstLuceneQuery)); // if the initial lucene query is null, changing its boost won't have any effect, we shouldn't test that if (firstLuceneQuery != null && supportsBoostAndQueryName()) { @@ -546,8 +547,8 @@ public abstract class AbstractQueryTestCase> setSearchContext(randomTypes); Query thirdLuceneQuery = rewriteQuery(secondQuery, context).toQuery(context); SearchContext.removeCurrent(); - assertThat("modifying the boost doesn't affect the corresponding lucene query", firstLuceneQuery, - not(equalTo(thirdLuceneQuery))); + assertNotEquals("modifying the boost doesn't affect the corresponding lucene query", rewrite(firstLuceneQuery), + rewrite(thirdLuceneQuery)); } } } @@ -988,4 +989,9 @@ public abstract class AbstractQueryTestCase> setSearchContext(randomTypes); // only set search context for toQuery to be more realistic queryBuilder.toQuery(context); } + + protected Query rewrite(Query query) throws IOException { + return query; + } + } diff --git a/core/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java index 7b3f3ecb7fd..ccdb09eb507 100644 --- a/core/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java @@ -195,14 +195,14 @@ public class BoolQueryBuilderTests extends AbstractQueryTestCase { @Override @@ -56,13 +54,9 @@ public class WrapperQueryBuilderTests extends AbstractQueryTestCase innerQuery = contextCopy.parseContext().parseInnerQueryBuilder(); - Query expected = innerQuery.toQuery(context); - assertThat(query, equalTo(expected)); - } + QueryBuilder innerQuery = queryBuilder.rewrite(queryShardContext()); + Query expected = rewrite(innerQuery.toQuery(context)); + assertEquals(rewrite(query), expected); } public void testIllegalArgument() { @@ -164,4 +158,16 @@ public class WrapperQueryBuilderTests extends AbstractQueryTestCase 0); + super.testMustRewrite(); + } + + public void testRewrite() throws IOException { + FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(new WrapperQueryBuilder(new TermQueryBuilder("foo", "bar").toString())); + FunctionScoreQueryBuilder rewrite = (FunctionScoreQueryBuilder) functionScoreQueryBuilder.rewrite(queryShardContext()); + assertNotSame(functionScoreQueryBuilder, rewrite); + assertEquals(rewrite.query(), new TermQueryBuilder("foo", "bar")); + } + + public void testRewriteWithFunction() throws IOException { + TermQueryBuilder secondFunction = new TermQueryBuilder("tq", "2"); + QueryBuilder queryBuilder = randomBoolean() ? new WrapperQueryBuilder(new TermQueryBuilder("foo", "bar").toString()) : new TermQueryBuilder("foo", "bar"); + FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder, + new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{ + new FunctionScoreQueryBuilder.FilterFunctionBuilder(new WrapperQueryBuilder(new TermQueryBuilder("tq", "1").toString()), new RandomScoreFunctionBuilder()), + new FunctionScoreQueryBuilder.FilterFunctionBuilder(secondFunction, new RandomScoreFunctionBuilder()) + + }); + FunctionScoreQueryBuilder rewrite = (FunctionScoreQueryBuilder) functionScoreQueryBuilder.rewrite(queryShardContext()); + assertNotSame(functionScoreQueryBuilder, rewrite); + assertEquals(rewrite.query(), new TermQueryBuilder("foo", "bar")); + assertEquals(rewrite.filterFunctionBuilders()[0].getFilter(), new TermQueryBuilder("tq", "1")); + assertSame(rewrite.filterFunctionBuilders()[1].getFilter(), secondFunction); + } }