Index: src/java/org/apache/lucene/search/ConstantScoreQuery.java =================================================================== --- src/java/org/apache/lucene/search/ConstantScoreQuery.java (revision 733441) +++ src/java/org/apache/lucene/search/ConstantScoreQuery.java (working copy) @@ -20,13 +20,12 @@ import org.apache.lucene.index.IndexReader; import java.io.IOException; -import java.util.Set; /** * A query that wraps a filter and simply returns a constant score equal to the * query boost for every document in the filter. * - * + * @deprecated Use {@link Filter} directly as {@link Query}. * @version $Id$ */ public class ConstantScoreQuery extends Query { @@ -40,114 +39,20 @@ public Filter getFilter() { return filter; } + + public void setBoost(float boost) { + super.setBoost(boost); + filter.setBoost(getBoost()); + } public Query rewrite(IndexReader reader) throws IOException { - return this; + return filter.rewrite(reader); } - public void extractTerms(Set terms) { - // OK to not add any terms when used for MultiSearcher, - // but may not be OK for highlighting - } - - protected class ConstantWeight implements Weight { - private Similarity similarity; - private float queryNorm; - private float queryWeight; - - public ConstantWeight(Searcher searcher) { - this.similarity = getSimilarity(searcher); - } - - public Query getQuery() { - return ConstantScoreQuery.this; - } - - public float getValue() { - return queryWeight; - } - - public float sumOfSquaredWeights() throws IOException { - queryWeight = getBoost(); - return queryWeight * queryWeight; - } - - public void normalize(float norm) { - this.queryNorm = norm; - queryWeight *= this.queryNorm; - } - - public Scorer scorer(IndexReader reader) throws IOException { - return new ConstantScorer(similarity, reader, this); - } - - public Explanation explain(IndexReader reader, int doc) throws IOException { - - ConstantScorer cs = (ConstantScorer)scorer(reader); - boolean exists = cs.docIdSetIterator.skipTo(doc) && (cs.docIdSetIterator.doc() == doc); - - ComplexExplanation result = new ComplexExplanation(); - - if (exists) { - result.setDescription("ConstantScoreQuery(" + filter - + "), product of:"); - result.setValue(queryWeight); - result.setMatch(Boolean.TRUE); - result.addDetail(new Explanation(getBoost(), "boost")); - result.addDetail(new Explanation(queryNorm,"queryNorm")); - } else { - result.setDescription("ConstantScoreQuery(" + filter - + ") doesn't match id " + doc); - result.setValue(0); - result.setMatch(Boolean.FALSE); - } - return result; - } - } - - protected class ConstantScorer extends Scorer { - final DocIdSetIterator docIdSetIterator; - final float theScore; - int doc=-1; - - public ConstantScorer(Similarity similarity, IndexReader reader, Weight w) throws IOException { - super(similarity); - theScore = w.getValue(); - docIdSetIterator = filter.getDocIdSet(reader).iterator(); - } - - public boolean next() throws IOException { - return docIdSetIterator.next(); - } - - public int doc() { - return docIdSetIterator.doc(); - } - - public float score() throws IOException { - return theScore; - } - - public boolean skipTo(int target) throws IOException { - return docIdSetIterator.skipTo(target); - } - - public Explanation explain(int doc) throws IOException { - throw new UnsupportedOperationException(); - } - } - - - protected Weight createWeight(Searcher searcher) { - return new ConstantScoreQuery.ConstantWeight(searcher); - } - - /** Prints a user-readable version of this query. */ public String toString(String field) { - return "ConstantScore(" + filter.toString() - + (getBoost()==1.0 ? ")" : "^" + getBoost()); + return filter.toString(field); } /** Returns true if o is equal to this. */ @@ -155,13 +60,12 @@ if (this == o) return true; if (!(o instanceof ConstantScoreQuery)) return false; ConstantScoreQuery other = (ConstantScoreQuery)o; - return this.getBoost()==other.getBoost() && filter.equals(other.filter); + return this.filter.equals(other.filter); } /** Returns a hash code value for this object. */ public int hashCode() { - // Simple add is OK since no existing filter hashcode has a float component. - return filter.hashCode() + Float.floatToIntBits(getBoost()); + return filter.hashCode(); } } Index: src/java/org/apache/lucene/search/Filter.java =================================================================== --- src/java/org/apache/lucene/search/Filter.java (revision 733441) +++ src/java/org/apache/lucene/search/Filter.java (working copy) @@ -17,8 +17,12 @@ * limitations under the License. */ +import java.io.IOException; +import java.util.Set; import java.util.BitSet; import java.io.IOException; +import java.io.Serializable; + import org.apache.lucene.index.IndexReader; import org.apache.lucene.util.DocIdBitSet; @@ -30,14 +34,14 @@ * All implementing classes must therefore implement {@link #getDocIdSet(IndexReader)} * in order to work with Lucene 3.0. */ -public abstract class Filter implements java.io.Serializable { +public abstract class Filter extends Query implements Serializable { /** * @return A BitSet with true for documents which should be permitted in * search results, and false for those that should not. * @deprecated Use {@link #getDocIdSet(IndexReader)} instead. */ public BitSet bits(IndexReader reader) throws IOException { - return null; + throw new UnsupportedOperationException(); } /** @@ -48,4 +52,120 @@ public DocIdSet getDocIdSet(IndexReader reader) throws IOException { return new DocIdBitSet(bits(reader)); } + + public Query rewrite(IndexReader reader) throws IOException { + return this; + } + + /** Prints a query to a string. */ + public String toString() { + // revert to default toString(), because of infinite loop + return getClass().getName() + '@' + Integer.toHexString(hashCode()); + } + + /** Prints a user-readable version of this query. */ + public String toString(String field) + { + // this method currently maps to the generic + // toString() method, because toString(field) is + // abstract in Query, but does not exist in current + // Filter! + return toString(); + } + + public void extractTerms(Set terms) { + // OK to not add any terms when used for MultiSearcher, + // but may not be OK for highlighting + } + + protected class ConstantWeight implements Weight { + private Similarity similarity; + private float queryNorm; + private float queryWeight; + + public ConstantWeight(Searcher searcher) { + this.similarity = getSimilarity(searcher); + } + + public Query getQuery() { + return Filter.this; + } + + public float getValue() { + return queryWeight; + } + + public float sumOfSquaredWeights() throws IOException { + queryWeight = getBoost(); + return queryWeight * queryWeight; + } + + public void normalize(float norm) { + this.queryNorm = norm; + queryWeight *= this.queryNorm; + } + + public Scorer scorer(IndexReader reader) throws IOException { + return new ConstantScorer(similarity, reader, this); + } + + public Explanation explain(IndexReader reader, int doc) throws IOException { + + ConstantScorer cs = (ConstantScorer)scorer(reader); + boolean exists = cs.docIdSetIterator.skipTo(doc) && (cs.docIdSetIterator.doc() == doc); + + ComplexExplanation result = new ComplexExplanation(); + + if (exists) { + result.setDescription(getQuery() + ", product of:"); + result.setValue(queryWeight); + result.setMatch(Boolean.TRUE); + result.addDetail(new Explanation(getBoost(), "boost")); + result.addDetail(new Explanation(queryNorm,"queryNorm")); + } else { + result.setDescription(getQuery() + " doesn't match id " + doc); + result.setValue(0); + result.setMatch(Boolean.FALSE); + } + return result; + } + } + + protected class ConstantScorer extends Scorer { + final DocIdSetIterator docIdSetIterator; + final float theScore; + int doc=-1; + + public ConstantScorer(Similarity similarity, IndexReader reader, Weight w) throws IOException { + super(similarity); + theScore = w.getValue(); + docIdSetIterator = Filter.this.getDocIdSet(reader).iterator(); + } + + public boolean next() throws IOException { + return docIdSetIterator.next(); + } + + public int doc() { + return docIdSetIterator.doc(); + } + + public float score() throws IOException { + return theScore; + } + + public boolean skipTo(int target) throws IOException { + return docIdSetIterator.skipTo(target); + } + + public Explanation explain(int doc) throws IOException { + throw new UnsupportedOperationException(); + } + } + + + protected Weight createWeight(Searcher searcher) { + return new ConstantWeight(searcher); + } + }