From d07f7eea12d6ab5068b07dd884d8202460beaef4 Mon Sep 17 00:00:00 2001 From: Erik Hatcher Date: Wed, 8 Mar 2006 00:59:28 +0000 Subject: [PATCH] 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 --- CHANGES.txt | 7 +++ .../apache/lucene/search/FilteredQuery.java | 44 ++++++++++++++----- .../lucene/search/TestFilteredQuery.java | 3 -- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 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.1 Bug fixes diff --git a/src/java/org/apache/lucene/search/FilteredQuery.java b/src/java/org/apache/lucene/search/FilteredQuery.java index 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 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 { diff --git a/src/test/org/apache/lucene/search/TestFilteredQuery.java b/src/test/org/apache/lucene/search/TestFilteredQuery.java index 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()); } }