WrapperQueryBuilder should also rewrite the parsed query.

Failing to do so can cause other errors later on during query execution.
For example if  `WrapperQueryBuilder` wraps a `GeoShapeQueryBuilder` that fetches the shape from an index then it will skip the shape fetching
and fail later with the error that no shapes have been fetched.
This commit is contained in:
Martijn van Groningen 2017-06-29 23:25:05 +02:00
parent 1fee1045b9
commit c8da7f84a2
No known key found for this signature in database
GPG Key ID: AB236F4FCF2AF12A
3 changed files with 22 additions and 2 deletions

View File

@ -160,7 +160,7 @@ public class WrapperQueryBuilder extends AbstractQueryBuilder<WrapperQueryBuilde
protected QueryBuilder doRewrite(QueryRewriteContext context) throws IOException {
try (XContentParser qSourceParser = XContentFactory.xContent(source).createParser(context.getXContentRegistry(), source)) {
final QueryBuilder queryBuilder = parseInnerQueryBuilder(qSourceParser);
final QueryBuilder queryBuilder = parseInnerQueryBuilder(qSourceParser).rewrite(context);
if (boost() != DEFAULT_BOOST || queryName() != null) {
final BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
boolQueryBuilder.must(queryBuilder);

View File

@ -409,7 +409,7 @@ public class BoolQueryBuilderTests extends AbstractQueryTestCase<BoolQueryBuilde
boolQueryBuilder.must(new WrapperQueryBuilder(new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()).toString()));
QueryBuilder rewritten = boolQueryBuilder.rewrite(createShardContext());
BoolQueryBuilder expected = new BoolQueryBuilder();
expected.must(new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()));
expected.must(new MatchAllQueryBuilder());
assertEquals(expected, rewritten);
expected = new BoolQueryBuilder();

View File

@ -19,9 +19,11 @@
package org.elasticsearch.index.query;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.memory.MemoryIndex;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.elasticsearch.action.support.ToXContentToBytes;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.bytes.BytesArray;
@ -129,6 +131,24 @@ public class WrapperQueryBuilderTests extends AbstractQueryTestCase<WrapperQuery
assertEquals(new BoolQueryBuilder().must(query).boost(3), builder.rewrite(shardContext));
}
public void testRewriteInnerQueryToo() throws IOException {
QueryShardContext shardContext = createShardContext();
QueryBuilder qb = new WrapperQueryBuilder(
new WrapperQueryBuilder(new TermQueryBuilder("foo", "bar").toString()).toString()
);
assertEquals(new TermQuery(new Term("foo", "bar")), qb.rewrite(shardContext).toQuery(shardContext));
qb = new WrapperQueryBuilder(
new WrapperQueryBuilder(
new WrapperQueryBuilder(new TermQueryBuilder("foo", "bar").toString()).toString()
).toString()
);
assertEquals(new TermQuery(new Term("foo", "bar")), qb.rewrite(shardContext).toQuery(shardContext));
qb = new WrapperQueryBuilder(new BoolQueryBuilder().toString());
assertEquals(new MatchAllDocsQuery(), qb.rewrite(shardContext).toQuery(shardContext));
}
@Override
protected Query rewrite(Query query) throws IOException {
// WrapperQueryBuilder adds some optimization if the wrapper and query builder have boosts / query names that wraps