LUCENE-6781: BoostingQuery implements rewrite().

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1701268 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrien Grand 2015-09-04 14:05:25 +00:00
parent 2fff441c5c
commit 51b3c33f82
3 changed files with 32 additions and 1 deletions

View File

@ -107,6 +107,8 @@ Bug Fixes
* LUCENE-6774: Remove classloader hack in MorfologikFilter. (Robert Muir,
Uwe Schindler)
* LUCENE-6781: Fixed BoostingQuery to rewrite wrapped queries. (Adrien Grand)
Other
* LUCENE-6174: Improve "ant eclipse" to select right JRE for building.

View File

@ -21,10 +21,10 @@ import java.io.IOException;
import java.util.Objects;
import java.util.Set;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.*;
import org.apache.lucene.util.Bits;
/**
* The BoostingQuery class can be used to effectively demote results that match a given query.
@ -54,6 +54,18 @@ public class BoostingQuery extends Query {
this.context.setBoost(0.0f); // ignore context-only matches
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
Query matchRewritten = match.rewrite(reader);
Query contextRewritten = context.rewrite(reader);
if (match != matchRewritten || context != contextRewritten) {
BoostingQuery rewritten = new BoostingQuery(matchRewritten, contextRewritten, boost);
rewritten.setBoost(getBoost());
return rewritten;
}
return super.rewrite(reader);
}
@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
if (needsScores == false) {

View File

@ -17,7 +17,15 @@ package org.apache.lucene.queries;
* limitations under the License.
*/
import java.io.IOException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.MultiReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryUtils;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.util.LuceneTestCase;
@ -35,4 +43,13 @@ public class BoostingQueryTest extends LuceneTestCase {
BoostingQuery bq2 = new BoostingQuery(q1, q2, 0.1f);
assertEquals("BoostingQuery with same attributes is not equal", bq1, bq2);
}
public void testRewrite() throws IOException {
IndexReader reader = new MultiReader();
BoostingQuery q = new BoostingQuery(new MatchNoDocsQuery(), new MatchAllDocsQuery(), 3);
Query rewritten = q.rewrite(reader);
Query expectedRewritten = new BoostingQuery(new BooleanQuery.Builder().build(), new MatchAllDocsQuery(), 3);
assertEquals(expectedRewritten, rewritten);
assertSame(rewritten, rewritten.rewrite(reader));
}
}