mirror of https://github.com/apache/lucene.git
LUCENE-698: FilteredQuery now takes the query boost into account for scoring.
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@542968 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
cb38e0c06c
commit
f5c80265e0
|
@ -147,6 +147,9 @@ Bug fixes
|
|||
anymore and ignored, but re-thrown. Some javadoc improvements.
|
||||
(Daniel Naber)
|
||||
|
||||
19. LUCENE-698: FilteredQuery now takes the query boost into account for
|
||||
scoring. (Michael Busch)
|
||||
|
||||
New features
|
||||
|
||||
1. LUCENE-759: Added two n-gram-producing TokenFilters.
|
||||
|
|
|
@ -66,13 +66,20 @@ extends Query {
|
|||
final Weight weight = query.createWeight (searcher);
|
||||
final Similarity similarity = query.getSimilarity(searcher);
|
||||
return new Weight() {
|
||||
|
||||
private float value;
|
||||
|
||||
// pass these methods through to enclosed query's weight
|
||||
public float getValue() { return weight.getValue(); }
|
||||
public float sumOfSquaredWeights() throws IOException { return weight.sumOfSquaredWeights(); }
|
||||
public void normalize (float v) { weight.normalize(v); }
|
||||
public float getValue() { return value; }
|
||||
public float sumOfSquaredWeights() throws IOException {
|
||||
return weight.sumOfSquaredWeights() * getBoost() * getBoost();
|
||||
}
|
||||
public void normalize (float v) {
|
||||
weight.normalize(v);
|
||||
value = weight.getValue() * getBoost();
|
||||
}
|
||||
public Explanation explain (IndexReader ir, int i) throws IOException {
|
||||
Explanation inner = weight.explain (ir, i);
|
||||
inner.setValue(getBoost() * inner.getValue());
|
||||
Filter f = FilteredQuery.this.filter;
|
||||
BitSet matches = f.bits(ir);
|
||||
if (matches.get(i))
|
||||
|
@ -121,11 +128,13 @@ extends Query {
|
|||
return true;
|
||||
}
|
||||
|
||||
public float score() throws IOException { return scorer.score(); }
|
||||
public float score() throws IOException { return getBoost() * scorer.score(); }
|
||||
|
||||
// add an explanation about whether the document was filtered
|
||||
public Explanation explain (int i) throws IOException {
|
||||
Explanation exp = scorer.explain (i);
|
||||
exp.setValue(getBoost() * exp.getValue());
|
||||
|
||||
if (bitset.get(i))
|
||||
exp.setDescription ("allowed by filter: "+exp.getDescription());
|
||||
else
|
||||
|
|
|
@ -24,6 +24,7 @@ import org.apache.lucene.document.Field;
|
|||
import org.apache.lucene.index.IndexWriter;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.BooleanClause.Occur;
|
||||
import org.apache.lucene.store.RAMDirectory;
|
||||
import java.util.BitSet;
|
||||
|
||||
|
@ -116,7 +117,48 @@ extends TestCase {
|
|||
filteredquery = new FilteredQuery (new TermQuery (new Term ("field", "y")), filter);
|
||||
hits = searcher.search (filteredquery);
|
||||
assertEquals (0, hits.length());
|
||||
QueryUtils.check(filteredquery,searcher);
|
||||
QueryUtils.check(filteredquery,searcher);
|
||||
|
||||
// test boost
|
||||
Filter f = new Filter() {
|
||||
public BitSet bits (IndexReader reader) {
|
||||
BitSet bitset = new BitSet(5);
|
||||
bitset.set(0, 5);
|
||||
return bitset;
|
||||
}
|
||||
};
|
||||
|
||||
float boost = 2.5f;
|
||||
BooleanQuery bq1 = new BooleanQuery();
|
||||
TermQuery tq = new TermQuery (new Term ("field", "one"));
|
||||
tq.setBoost(boost);
|
||||
bq1.add(tq, Occur.MUST);
|
||||
bq1.add(new TermQuery (new Term ("field", "five")), Occur.MUST);
|
||||
|
||||
BooleanQuery bq2 = new BooleanQuery();
|
||||
tq = new TermQuery (new Term ("field", "one"));
|
||||
filteredquery = new FilteredQuery(tq, f);
|
||||
filteredquery.setBoost(boost);
|
||||
bq2.add(filteredquery, Occur.MUST);
|
||||
bq2.add(new TermQuery (new Term ("field", "five")), Occur.MUST);
|
||||
assertScoreEquals(bq1, bq2);
|
||||
|
||||
assertEquals(boost, filteredquery.getBoost(), 0);
|
||||
assertEquals(1.0f, tq.getBoost(), 0); // the boost value of the underlying query shouldn't have changed
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether the scores of the two queries are the same.
|
||||
*/
|
||||
public void assertScoreEquals(Query q1, Query q2) throws Exception {
|
||||
Hits hits1 = searcher.search (q1);
|
||||
Hits hits2 = searcher.search (q2);
|
||||
|
||||
assertEquals(hits1.length(), hits2.length());
|
||||
|
||||
for (int i = 0; i < hits1.length(); i++) {
|
||||
assertEquals(hits1.score(i), hits2.score(i), 0.0000001f);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -146,3 +188,4 @@ extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -119,12 +119,6 @@ public class TestSimpleExplanations extends TestExplanations {
|
|||
q.setBoost(1000);
|
||||
qtest(q, new int[] {3});
|
||||
}
|
||||
public void testFQ7() throws Exception {
|
||||
Query q = new FilteredQuery(qp.parse("xx"),
|
||||
new ItemizedFilter(new int[] {1,3}));
|
||||
q.setBoost(0);
|
||||
qtest(q, new int[] {3});
|
||||
}
|
||||
|
||||
/* ConstantScoreQueries */
|
||||
|
||||
|
|
Loading…
Reference in New Issue