1 package org.apache.lucene.search; 2 3 18 19 import org.apache.lucene.index.IndexReader; 20 import org.apache.lucene.search.Explanation; 21 import org.apache.lucene.search.Query; 22 import org.apache.lucene.search.Scorer; 23 import org.apache.lucene.search.Searcher; 24 import org.apache.lucene.search.Similarity; 25 import org.apache.lucene.search.Weight; 26 import org.apache.lucene.util.ToStringUtils; 27 28 33 public class MatchAllDocsQuery extends Query { 34 35 public MatchAllDocsQuery() { 36 } 37 38 private class MatchAllScorer extends Scorer { 39 40 final IndexReader reader; 41 int id; 42 final int maxId; 43 final float score; 44 45 MatchAllScorer(IndexReader reader, Similarity similarity, Weight w) { 46 super(similarity); 47 this.reader = reader; 48 id = -1; 49 maxId = reader.maxDoc() - 1; 50 score = w.getValue(); 51 } 52 53 public Explanation explain(int doc) { 54 return null; } 56 57 public int doc() { 58 return id; 59 } 60 61 public boolean next() { 62 while (id < maxId) { 63 id++; 64 if (!reader.isDeleted(id)) { 65 return true; 66 } 67 } 68 return false; 69 } 70 71 public float score() { 72 return score; 73 } 74 75 public boolean skipTo(int target) { 76 id = target - 1; 77 return next(); 78 } 79 80 } 81 82 private class MatchAllDocsWeight implements Weight { 83 private Searcher searcher; 84 private float queryWeight; 85 private float queryNorm; 86 87 public MatchAllDocsWeight(Searcher searcher) { 88 this.searcher = searcher; 89 } 90 91 public String toString() { 92 return "weight(" + MatchAllDocsQuery.this + ")"; 93 } 94 95 public Query getQuery() { 96 return MatchAllDocsQuery.this; 97 } 98 99 public float getValue() { 100 return queryWeight; 101 } 102 103 public float sumOfSquaredWeights() { 104 queryWeight = getBoost(); 105 return queryWeight * queryWeight; 106 } 107 108 public void normalize(float queryNorm) { 109 this.queryNorm = queryNorm; 110 queryWeight *= this.queryNorm; 111 } 112 113 public Scorer scorer(IndexReader reader) { 114 return new MatchAllScorer(reader, getSimilarity(searcher), this); 115 } 116 117 public Explanation explain(IndexReader reader, int doc) { 118 Explanation queryExpl = new Explanation(); 120 queryExpl.setDescription("MatchAllDocsQuery, product of:"); 121 queryExpl.setValue(getValue()); 122 if (getBoost() != 1.0f) { 123 queryExpl.addDetail(new Explanation(getBoost(),"boost")); 124 } 125 queryExpl.addDetail(new Explanation(queryNorm,"queryNorm")); 126 127 return queryExpl; 128 } 129 } 130 131 protected Weight createWeight(Searcher searcher) { 132 return new MatchAllDocsWeight(searcher); 133 } 134 135 public String toString(String field) { 136 StringBuffer buffer = new StringBuffer (); 137 buffer.append("MatchAllDocsQuery"); 138 buffer.append(ToStringUtils.boost(getBoost())); 139 return buffer.toString(); 140 } 141 142 public boolean equals(Object o) { 143 if (!(o instanceof MatchAllDocsQuery)) 144 return false; 145 MatchAllDocsQuery other = (MatchAllDocsQuery) o; 146 return this.getBoost() == other.getBoost(); 147 } 148 149 public int hashCode() { 150 return Float.floatToIntBits(getBoost()) ^ 0x1AA71190; 151 } 152 } 153 | Popular Tags |