1 package org.apache.lucene.search; 2 3 18 19 import java.io.IOException ; 20 21 import org.apache.lucene.index.TermDocs; 22 23 25 final class TermScorer extends Scorer { 26 private Weight weight; 27 private TermDocs termDocs; 28 private byte[] norms; 29 private float weightValue; 30 private int doc; 31 32 private final int[] docs = new int[32]; private final int[] freqs = new int[32]; private int pointer; 35 private int pointerMax; 36 37 private static final int SCORE_CACHE_SIZE = 32; 38 private float[] scoreCache = new float[SCORE_CACHE_SIZE]; 39 40 46 TermScorer(Weight weight, TermDocs td, Similarity similarity, 47 byte[] norms) { 48 super(similarity); 49 this.weight = weight; 50 this.termDocs = td; 51 this.norms = norms; 52 this.weightValue = weight.getValue(); 53 54 for (int i = 0; i < SCORE_CACHE_SIZE; i++) 55 scoreCache[i] = getSimilarity().tf(i) * weightValue; 56 } 57 58 public void score(HitCollector hc) throws IOException { 59 next(); 60 score(hc, Integer.MAX_VALUE); 61 } 62 63 protected boolean score(HitCollector c, int end) throws IOException { 64 Similarity similarity = getSimilarity(); float[] normDecoder = Similarity.getNormDecoder(); 66 while (doc < end) { int f = freqs[pointer]; 68 float score = f < SCORE_CACHE_SIZE ? scoreCache[f] : similarity.tf(f)*weightValue; 73 score *= normDecoder[norms[doc] & 0xFF]; 75 c.collect(doc, score); 77 if (++pointer >= pointerMax) { 78 pointerMax = termDocs.read(docs, freqs); if (pointerMax != 0) { 80 pointer = 0; 81 } else { 82 termDocs.close(); doc = Integer.MAX_VALUE; return false; 85 } 86 } 87 doc = docs[pointer]; 88 } 89 return true; 90 } 91 92 95 public int doc() { return doc; } 96 97 102 public boolean next() throws IOException { 103 pointer++; 104 if (pointer >= pointerMax) { 105 pointerMax = termDocs.read(docs, freqs); if (pointerMax != 0) { 107 pointer = 0; 108 } else { 109 termDocs.close(); doc = Integer.MAX_VALUE; return false; 112 } 113 } 114 doc = docs[pointer]; 115 return true; 116 } 117 118 public float score() { 119 int f = freqs[pointer]; 120 float raw = f < SCORE_CACHE_SIZE ? scoreCache[f] : getSimilarity().tf(f)*weightValue; 125 return raw * Similarity.decodeNorm(norms[doc]); } 127 128 134 public boolean skipTo(int target) throws IOException { 135 for (pointer++; pointer < pointerMax; pointer++) { 137 if (docs[pointer] >= target) { 138 doc = docs[pointer]; 139 return true; 140 } 141 } 142 143 boolean result = termDocs.skipTo(target); 145 if (result) { 146 pointerMax = 1; 147 pointer = 0; 148 docs[pointer] = doc = termDocs.doc(); 149 freqs[pointer] = termDocs.freq(); 150 } else { 151 doc = Integer.MAX_VALUE; 152 } 153 return result; 154 } 155 156 162 public Explanation explain(int doc) throws IOException { 163 TermQuery query = (TermQuery)weight.getQuery(); 164 Explanation tfExplanation = new Explanation(); 165 int tf = 0; 166 while (pointer < pointerMax) { 167 if (docs[pointer] == doc) 168 tf = freqs[pointer]; 169 pointer++; 170 } 171 if (tf == 0) { 172 while (termDocs.next()) { 173 if (termDocs.doc() == doc) { 174 tf = termDocs.freq(); 175 } 176 } 177 } 178 termDocs.close(); 179 tfExplanation.setValue(getSimilarity().tf(tf)); 180 tfExplanation.setDescription("tf(termFreq("+query.getTerm()+")="+tf+")"); 181 182 return tfExplanation; 183 } 184 185 186 public String toString() { return "scorer(" + weight + ")"; } 187 } 188 | Popular Tags |