1 package org.apache.lucene.search; 2 3 18 19 import org.apache.lucene.index.IndexReader; 20 import org.apache.lucene.util.ToStringUtils; 21 22 import java.io.IOException ; 23 import java.util.BitSet ; 24 import java.util.Set ; 25 26 27 41 public class FilteredQuery 42 extends Query { 43 44 Query query; 45 Filter filter; 46 47 53 public FilteredQuery (Query query, Filter filter) { 54 this.query = query; 55 this.filter = filter; 56 } 57 58 59 60 64 protected Weight createWeight (final Searcher searcher) throws IOException { 65 final Weight weight = query.createWeight (searcher); 66 final Similarity similarity = query.getSimilarity(searcher); 67 return new Weight() { 68 69 public float getValue() { return weight.getValue(); } 71 public float sumOfSquaredWeights() throws IOException { return weight.sumOfSquaredWeights(); } 72 public void normalize (float v) { weight.normalize(v); } 73 public Explanation explain (IndexReader ir, int i) throws IOException { return weight.explain (ir, i); } 74 75 public Query getQuery() { return FilteredQuery.this; } 77 78 public Scorer scorer (IndexReader indexReader) throws IOException { 81 final Scorer scorer = weight.scorer (indexReader); 82 final BitSet bitset = filter.bits (indexReader); 83 return new Scorer (similarity) { 84 85 public boolean next() throws IOException { return scorer.next(); } 87 public int doc() { return scorer.doc(); } 88 public boolean skipTo (int i) throws IOException { return scorer.skipTo(i); } 89 90 public float score() throws IOException { 92 return (bitset.get(scorer.doc())) ? scorer.score() : 0.0f; 93 } 94 95 public Explanation explain (int i) throws IOException { 97 Explanation exp = scorer.explain (i); 98 if (bitset.get(i)) 99 exp.setDescription ("allowed by filter: "+exp.getDescription()); 100 else 101 exp.setDescription ("removed by filter: "+exp.getDescription()); 102 return exp; 103 } 104 }; 105 } 106 }; 107 } 108 109 110 public Query rewrite(IndexReader reader) throws IOException { 111 Query rewritten = query.rewrite(reader); 112 if (rewritten != query) { 113 FilteredQuery clone = (FilteredQuery)this.clone(); 114 clone.query = rewritten; 115 return clone; 116 } else { 117 return this; 118 } 119 } 120 121 public Query getQuery() { 122 return query; 123 } 124 125 public Filter getFilter() { 126 return filter; 127 } 128 129 public void extractTerms(Set terms) { 131 getQuery().extractTerms(terms); 132 } 133 134 135 public String toString (String s) { 136 StringBuffer buffer = new StringBuffer (); 137 buffer.append("filtered("); 138 buffer.append(query.toString(s)); 139 buffer.append(")->"); 140 buffer.append(filter); 141 buffer.append(ToStringUtils.boost(getBoost())); 142 return buffer.toString(); 143 } 144 145 146 public boolean equals(Object o) { 147 if (o instanceof FilteredQuery) { 148 FilteredQuery fq = (FilteredQuery) o; 149 return (query.equals(fq.query) && filter.equals(fq.filter)); 150 } 151 return false; 152 } 153 154 155 public int hashCode() { 156 return query.hashCode() ^ filter.hashCode(); 157 } 158 } 159 | Popular Tags |