Apache Solr open-source search software
Part of LUCENE-330: fixing FilteredQuery when nested within BooleanQuery
Erik Hatcherd07f7eea12d6
diff --git a/CHANGES.txt b/CHANGES.txtindex 9c5a481a4a4..c289200aad2 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -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.1Bug fixesdiff --git a/src/java/org/apache/lucene/search/FilteredQuery.java b/src/java/org/apache/lucene/search/FilteredQuery.javaindex cb7d9990651..ac319a81462 100644--- a/src/java/org/apache/lucene/search/FilteredQuery.java+++ b/src/java/org/apache/lucene/search/FilteredQuery.java@@ -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 querypublic 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 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 { 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 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 filteredpublic Explanation explain (int i) throws IOException {diff --git a/src/test/org/apache/lucene/search/TestFilteredQuery.java b/src/test/org/apache/lucene/search/TestFilteredQuery.javaindex 84a1f6e5f89..89d05575c73 100644--- a/src/test/org/apache/lucene/search/TestFilteredQuery.java+++ b/src/test/org/apache/lucene/search/TestFilteredQuery.java@@ -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());}}