1 package org.apache.lucene.search.spans; 2 3 18 19 import java.io.IOException ; 20 21 import java.util.Iterator ; 22 import java.util.Collection ; 23 24 import org.apache.lucene.index.IndexReader; 25 import org.apache.lucene.index.Term; 26 27 import org.apache.lucene.search.Query; 28 import org.apache.lucene.search.Weight; 29 import org.apache.lucene.search.Searcher; 30 import org.apache.lucene.search.Scorer; 31 import org.apache.lucene.search.Explanation; 32 import org.apache.lucene.search.Similarity; 33 34 class SpanWeight implements Weight { 35 private Similarity similarity; 36 private float value; 37 private float idf; 38 private float queryNorm; 39 private float queryWeight; 40 41 private Collection terms; 42 private SpanQuery query; 43 44 public SpanWeight(SpanQuery query, Searcher searcher) 45 throws IOException { 46 this.similarity = query.getSimilarity(searcher); 47 this.query = query; 48 this.terms = query.getTerms(); 49 50 idf = this.query.getSimilarity(searcher).idf(terms, searcher); 51 } 52 53 public Query getQuery() { return query; } 54 public float getValue() { return value; } 55 56 public float sumOfSquaredWeights() throws IOException { 57 queryWeight = idf * query.getBoost(); return queryWeight * queryWeight; } 60 61 public void normalize(float queryNorm) { 62 this.queryNorm = queryNorm; 63 queryWeight *= queryNorm; value = queryWeight * idf; } 66 67 public Scorer scorer(IndexReader reader) throws IOException { 68 return new SpanScorer(query.getSpans(reader), this, 69 similarity, 70 reader.norms(query.getField())); 71 } 72 73 public Explanation explain(IndexReader reader, int doc) 74 throws IOException { 75 76 Explanation result = new Explanation(); 77 result.setDescription("weight("+getQuery()+" in "+doc+"), product of:"); 78 String field = ((SpanQuery)getQuery()).getField(); 79 80 StringBuffer docFreqs = new StringBuffer (); 81 Iterator i = terms.iterator(); 82 while (i.hasNext()) { 83 Term term = (Term)i.next(); 84 docFreqs.append(term.text()); 85 docFreqs.append("="); 86 docFreqs.append(reader.docFreq(term)); 87 88 if (i.hasNext()) { 89 docFreqs.append(" "); 90 } 91 } 92 93 Explanation idfExpl = 94 new Explanation(idf, "idf(" + field + ": " + docFreqs + ")"); 95 96 Explanation queryExpl = new Explanation(); 98 queryExpl.setDescription("queryWeight(" + getQuery() + "), product of:"); 99 100 Explanation boostExpl = new Explanation(getQuery().getBoost(), "boost"); 101 if (getQuery().getBoost() != 1.0f) 102 queryExpl.addDetail(boostExpl); 103 queryExpl.addDetail(idfExpl); 104 105 Explanation queryNormExpl = new Explanation(queryNorm,"queryNorm"); 106 queryExpl.addDetail(queryNormExpl); 107 108 queryExpl.setValue(boostExpl.getValue() * 109 idfExpl.getValue() * 110 queryNormExpl.getValue()); 111 112 result.addDetail(queryExpl); 113 114 Explanation fieldExpl = new Explanation(); 116 fieldExpl.setDescription("fieldWeight("+field+":"+query.toString(field)+ 117 " in "+doc+"), product of:"); 118 119 Explanation tfExpl = scorer(reader).explain(doc); 120 fieldExpl.addDetail(tfExpl); 121 fieldExpl.addDetail(idfExpl); 122 123 Explanation fieldNormExpl = new Explanation(); 124 byte[] fieldNorms = reader.norms(field); 125 float fieldNorm = 126 fieldNorms!=null ? Similarity.decodeNorm(fieldNorms[doc]) : 0.0f; 127 fieldNormExpl.setValue(fieldNorm); 128 fieldNormExpl.setDescription("fieldNorm(field="+field+", doc="+doc+")"); 129 fieldExpl.addDetail(fieldNormExpl); 130 131 fieldExpl.setValue(tfExpl.getValue() * 132 idfExpl.getValue() * 133 fieldNormExpl.getValue()); 134 135 result.addDetail(fieldExpl); 136 137 result.setValue(queryExpl.getValue() * fieldExpl.getValue()); 139 140 if (queryExpl.getValue() == 1.0f) 141 return fieldExpl; 142 143 return result; 144 } 145 } 146 | Popular Tags |