Part of LUCENE-330: fixing FilteredQuery when nested within BooleanQuery

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@384072 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2006-03-08 00:59:28 +00:00
parent 9ad7f217ce
commit d07f7eea12
3 changed files with 39 additions and 15 deletions

View File

@ -2,6 +2,13 @@ Lucene Change Log
$Id$
2.0 RC1
Bug fixes
1. LUCENE-330: Fix issue of FilteredQuery not working properly within
BooleanQuery. (Paul Elschot via Erik Hatcher)
1.9.1
Bug fixes

View File

@ -1,7 +1,7 @@
package org.apache.lucene.search;
/**
* Copyright 2004 The Apache Software Foundation
* Copyright 2004,2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -75,22 +75,42 @@ extends Query {
// return this query
public Query getQuery() { return FilteredQuery.this; }
// return a scorer that overrides the enclosed query's score if
// the given hit has been filtered out.
public Scorer scorer (IndexReader indexReader) throws IOException {
// return a filtering scorer
public Scorer scorer (IndexReader indexReader) throws IOException {
final Scorer scorer = weight.scorer (indexReader);
final BitSet bitset = filter.bits (indexReader);
return new Scorer (similarity) {
// pass these methods through to the enclosed scorer
public boolean next() throws IOException { return scorer.next(); }
public int doc() { return scorer.doc(); }
public boolean skipTo (int i) throws IOException { return scorer.skipTo(i); }
// if the document has been filtered out, set score to 0.0
public float score() throws IOException {
return (bitset.get(scorer.doc())) ? scorer.score() : 0.0f;
public boolean next() throws IOException {
do {
if (! scorer.next()) {
return false;
}
} while (! bitset.get(scorer.doc()));
/* When skipTo() is allowed on scorer it should be used here
* in combination with bitset.nextSetBit(...)
* See the while loop in skipTo() below.
*/
return true;
}
public int doc() { return scorer.doc(); }
public boolean skipTo(int i) throws IOException {
if (! scorer.skipTo(i)) {
return false;
}
while (! bitset.get(scorer.doc())) {
int nextFiltered = bitset.nextSetBit(scorer.doc() + 1);
if (nextFiltered == -1) {
return false;
} else if (! scorer.skipTo(nextFiltered)) {
return false;
}
}
return true;
}
public float score() throws IOException { return scorer.score(); }
// add an explanation about whether the document was filtered
public Explanation explain (int i) throws IOException {

View File

@ -126,7 +126,6 @@ extends TestCase {
assertEquals(2, hits.length());
}
public void testBoolean() throws Exception {
BooleanQuery bq = new BooleanQuery();
Query query = new FilteredQuery(new MatchAllDocsQuery(),
@ -136,8 +135,6 @@ extends TestCase {
new SingleDocTestFilter(1));
bq.add(query, BooleanClause.Occur.MUST);
Hits hits = searcher.search(bq);
System.out.println(hits.id(0));
System.out.println(hits.id(1));
assertEquals(0, hits.length());
}
}