1 package org.apache.lucene.search; 2 3 18 19 import org.apache.lucene.index.IndexReader; 20 21 import java.io.IOException ; 22 import java.util.BitSet ; 23 24 31 public class ConstantScoreQuery extends Query { 32 protected final Filter filter; 33 34 public ConstantScoreQuery(Filter filter) { 35 this.filter=filter; 36 } 37 38 public Query rewrite(IndexReader reader) throws IOException { 39 return this; 40 } 41 42 protected class ConstantWeight implements Weight { 43 private Searcher searcher; 44 private float queryNorm; 45 private float queryWeight; 46 47 public ConstantWeight(Searcher searcher) { 48 this.searcher = searcher; 49 } 50 51 public Query getQuery() { 52 return ConstantScoreQuery.this; 53 } 54 55 public float getValue() { 56 return queryWeight; 57 } 58 59 public float sumOfSquaredWeights() throws IOException { 60 queryWeight = getBoost(); 61 return queryWeight * queryWeight; 62 } 63 64 public void normalize(float norm) { 65 this.queryNorm = norm; 66 queryWeight *= this.queryNorm; 67 } 68 69 public Scorer scorer(IndexReader reader) throws IOException { 70 return new ConstantScorer(getSimilarity(searcher), reader, this); 71 } 72 73 public Explanation explain(IndexReader reader, int doc) throws IOException { 74 75 ConstantScorer cs = (ConstantScorer)scorer(reader); 76 boolean exists = cs.bits.get(doc); 77 78 Explanation result = new Explanation(); 79 80 if (exists) { 81 result.setDescription("ConstantScoreQuery(" + filter 82 + "), product of:"); 83 result.setValue(queryWeight); 84 result.addDetail(new Explanation(getBoost(), "boost")); 85 result.addDetail(new Explanation(queryNorm,"queryNorm")); 86 } else { 87 result.setDescription("ConstantScoreQuery(" + filter 88 + ") doesn't match id " + doc); 89 result.setValue(0); 90 } 91 return result; 92 } 93 } 94 95 protected class ConstantScorer extends Scorer { 96 final BitSet bits; 97 final float theScore; 98 int doc=-1; 99 100 public ConstantScorer(Similarity similarity, IndexReader reader, Weight w) throws IOException { 101 super(similarity); 102 theScore = w.getValue(); 103 bits = filter.bits(reader); 104 } 105 106 public boolean next() throws IOException { 107 doc = bits.nextSetBit(doc+1); 108 return doc >= 0; 109 } 110 111 public int doc() { 112 return doc; 113 } 114 115 public float score() throws IOException { 116 return theScore; 117 } 118 119 public boolean skipTo(int target) throws IOException { 120 doc = bits.nextSetBit(target); return doc >= 0; 122 } 123 124 public Explanation explain(int doc) throws IOException { 125 throw new UnsupportedOperationException (); 126 } 127 } 128 129 130 protected Weight createWeight(Searcher searcher) { 131 return new ConstantScoreQuery.ConstantWeight(searcher); 132 } 133 134 135 136 public String toString(String field) 137 { 138 return "ConstantScore(" + filter.toString() 139 + (getBoost()==1.0 ? ")" : "^" + getBoost()); 140 } 141 142 143 public boolean equals(Object o) { 144 if (this == o) return true; 145 if (!(o instanceof ConstantScoreQuery)) return false; 146 ConstantScoreQuery other = (ConstantScoreQuery)o; 147 return this.getBoost()==other.getBoost() && filter.equals(other.filter); 148 } 149 150 151 public int hashCode() { 152 return filter.hashCode() + Float.floatToIntBits(getBoost()); 154 } 155 156 } 157 158 159 | Popular Tags |