LUCENE-4705: Pass on FilterStrategy in FilteredQuery if the filtered query is rewritten

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1436859 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Willnauer 2013-01-22 12:08:30 +00:00
parent ae76248870
commit 13d1d21951
3 changed files with 27 additions and 3 deletions

View File

@ -64,6 +64,11 @@ New Features
* LUCENE-4599: New oal.codecs.compressing.CompressingTermVectorsFormat which
compresses term vectors into chunks of documents similarly to
CompressingStoredFieldsFormat. (Adrien Grand)
Bug Fixes
* LUCENE-4705: Pass on FilterStrategy in FilteredQuery if the filtered query is
rewritten. (Simon Willnauer)
======================= Lucene 4.1.0 =======================

View File

@ -344,7 +344,7 @@ public class FilteredQuery extends Query {
if (queryRewritten != query) {
// rewrite to a new FilteredQuery wrapping the rewritten query
final Query rewritten = new FilteredQuery(queryRewritten, filter);
final Query rewritten = new FilteredQuery(queryRewritten, filter, strategy);
rewritten.setBoost(this.getBoost());
return rewritten;
} else {
@ -362,6 +362,11 @@ public class FilteredQuery extends Query {
public final Filter getFilter() {
return filter;
}
/** Returns this FilteredQuery's {@link FilterStrategy} */
public FilterStrategy getFilterStrategy() {
return this.strategy;
}
// inherit javadoc
@Override

View File

@ -31,6 +31,7 @@ import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.FilteredQuery.FilterStrategy;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.DocIdBitSet;
@ -341,11 +342,16 @@ public class TestFilteredQuery extends LuceneTestCase {
}
}
private FilterStrategy randomFilterStrategy() {
return randomFilterStrategy(random(), true);
}
private void assertRewrite(FilteredQuery fq, Class<? extends Query> clazz) throws Exception {
// assign crazy boost to FQ
final float boost = random().nextFloat() * 100.f;
fq.setBoost(boost);
// assign crazy boost to inner
final float innerBoost = random().nextFloat() * 100.f;
fq.getQuery().setBoost(innerBoost);
@ -356,6 +362,7 @@ public class TestFilteredQuery extends LuceneTestCase {
if (rewritten instanceof FilteredQuery) {
assertEquals(boost, rewritten.getBoost(), 1.E-5f);
assertEquals(innerBoost, ((FilteredQuery) rewritten).getQuery().getBoost(), 1.E-5f);
assertEquals(fq.getFilterStrategy(), ((FilteredQuery) rewritten).getFilterStrategy());
} else {
assertEquals(boost * innerBoost, rewritten.getBoost(), 1.E-5f);
}
@ -366,8 +373,15 @@ public class TestFilteredQuery extends LuceneTestCase {
}
public void testRewrite() throws Exception {
assertRewrite(new FilteredQuery(new TermQuery(new Term("field", "one")), new PrefixFilter(new Term("field", "o"))), FilteredQuery.class);
assertRewrite(new FilteredQuery(new MatchAllDocsQuery(), new PrefixFilter(new Term("field", "o"))), ConstantScoreQuery.class);
assertRewrite(new FilteredQuery(new TermQuery(new Term("field", "one")), new PrefixFilter(new Term("field", "o")), randomFilterStrategy()), FilteredQuery.class);
assertRewrite(new FilteredQuery(new PrefixQuery(new Term("field", "one")), new PrefixFilter(new Term("field", "o")), randomFilterStrategy()), FilteredQuery.class);
assertRewrite(new FilteredQuery(new MatchAllDocsQuery(), new PrefixFilter(new Term("field", "o")), randomFilterStrategy()), ConstantScoreQuery.class);
}
public void testGetFilterStrategy() {
FilterStrategy randomFilterStrategy = randomFilterStrategy();
FilteredQuery filteredQuery = new FilteredQuery(new TermQuery(new Term("field", "one")), new PrefixFilter(new Term("field", "o")), randomFilterStrategy);
assertSame(randomFilterStrategy, filteredQuery.getFilterStrategy());
}
private static FilteredQuery.FilterStrategy randomFilterStrategy(Random random, final boolean useRandomAccess) {