1 package org.apache.lucene.search; 2 3 18 19 import java.io.IOException ; 20 import java.util.Set ; 21 22 import org.apache.lucene.index.Term; 23 import org.apache.lucene.index.TermDocs; 24 import org.apache.lucene.index.IndexReader; 25 import org.apache.lucene.util.ToStringUtils; 26 27 30 public class TermQuery extends Query { 31 private Term term; 32 33 private class TermWeight implements Weight { 34 private Similarity similarity; 35 private float value; 36 private float idf; 37 private float queryNorm; 38 private float queryWeight; 39 40 public TermWeight(Searcher searcher) 41 throws IOException { 42 this.similarity = getSimilarity(searcher); 43 idf = similarity.idf(term, searcher); } 45 46 public String toString() { return "weight(" + TermQuery.this + ")"; } 47 48 public Query getQuery() { return TermQuery.this; } 49 public float getValue() { return value; } 50 51 public float sumOfSquaredWeights() { 52 queryWeight = idf * getBoost(); return queryWeight * queryWeight; } 55 56 public void normalize(float queryNorm) { 57 this.queryNorm = queryNorm; 58 queryWeight *= queryNorm; value = queryWeight * idf; } 61 62 public Scorer scorer(IndexReader reader) throws IOException { 63 TermDocs termDocs = reader.termDocs(term); 64 65 if (termDocs == null) 66 return null; 67 68 return new TermScorer(this, termDocs, similarity, 69 reader.norms(term.field())); 70 } 71 72 public Explanation explain(IndexReader reader, int doc) 73 throws IOException { 74 75 Explanation result = new Explanation(); 76 result.setDescription("weight("+getQuery()+" in "+doc+"), product of:"); 77 78 Explanation idfExpl = 79 new Explanation(idf, "idf(docFreq=" + reader.docFreq(term) + ")"); 80 81 Explanation queryExpl = new Explanation(); 83 queryExpl.setDescription("queryWeight(" + getQuery() + "), product of:"); 84 85 Explanation boostExpl = new Explanation(getBoost(), "boost"); 86 if (getBoost() != 1.0f) 87 queryExpl.addDetail(boostExpl); 88 queryExpl.addDetail(idfExpl); 89 90 Explanation queryNormExpl = new Explanation(queryNorm,"queryNorm"); 91 queryExpl.addDetail(queryNormExpl); 92 93 queryExpl.setValue(boostExpl.getValue() * 94 idfExpl.getValue() * 95 queryNormExpl.getValue()); 96 97 result.addDetail(queryExpl); 98 99 String field = term.field(); 101 Explanation fieldExpl = new Explanation(); 102 fieldExpl.setDescription("fieldWeight("+term+" in "+doc+ 103 "), product of:"); 104 105 Explanation tfExpl = scorer(reader).explain(doc); 106 fieldExpl.addDetail(tfExpl); 107 fieldExpl.addDetail(idfExpl); 108 109 Explanation fieldNormExpl = new Explanation(); 110 byte[] fieldNorms = reader.norms(field); 111 float fieldNorm = 112 fieldNorms!=null ? Similarity.decodeNorm(fieldNorms[doc]) : 0.0f; 113 fieldNormExpl.setValue(fieldNorm); 114 fieldNormExpl.setDescription("fieldNorm(field="+field+", doc="+doc+")"); 115 fieldExpl.addDetail(fieldNormExpl); 116 117 fieldExpl.setValue(tfExpl.getValue() * 118 idfExpl.getValue() * 119 fieldNormExpl.getValue()); 120 121 result.addDetail(fieldExpl); 122 123 result.setValue(queryExpl.getValue() * fieldExpl.getValue()); 125 126 if (queryExpl.getValue() == 1.0f) 127 return fieldExpl; 128 129 return result; 130 } 131 } 132 133 134 public TermQuery(Term t) { 135 term = t; 136 } 137 138 139 public Term getTerm() { return term; } 140 141 protected Weight createWeight(Searcher searcher) throws IOException { 142 return new TermWeight(searcher); 143 } 144 145 public void extractTerms(Set terms) { 146 terms.add(getTerm()); 147 } 148 149 150 public String toString(String field) { 151 StringBuffer buffer = new StringBuffer (); 152 if (!term.field().equals(field)) { 153 buffer.append(term.field()); 154 buffer.append(":"); 155 } 156 buffer.append(term.text()); 157 buffer.append(ToStringUtils.boost(getBoost())); 158 return buffer.toString(); 159 } 160 161 162 public boolean equals(Object o) { 163 if (!(o instanceof TermQuery)) 164 return false; 165 TermQuery other = (TermQuery)o; 166 return (this.getBoost() == other.getBoost()) 167 && this.term.equals(other.term); 168 } 169 170 171 public int hashCode() { 172 return Float.floatToIntBits(getBoost()) ^ term.hashCode(); 173 } 174 175 } 176 | Popular Tags |