1 package org.apache.lucene.search; 2 3 18 19 import java.util.BitSet ; 20 import java.io.IOException ; 21 22 import org.apache.lucene.search.Filter; 23 import org.apache.lucene.index.Term; 24 import org.apache.lucene.index.TermDocs; 25 import org.apache.lucene.index.TermEnum; 26 import org.apache.lucene.index.IndexReader; 27 28 37 public class RangeFilter extends Filter { 38 39 private String fieldName; 40 private String lowerTerm; 41 private String upperTerm; 42 private boolean includeLower; 43 private boolean includeUpper; 44 45 55 public RangeFilter(String fieldName, String lowerTerm, String upperTerm, 56 boolean includeLower, boolean includeUpper) { 57 this.fieldName = fieldName; 58 this.lowerTerm = lowerTerm; 59 this.upperTerm = upperTerm; 60 this.includeLower = includeLower; 61 this.includeUpper = includeUpper; 62 63 if (null == lowerTerm && null == upperTerm) { 64 throw new IllegalArgumentException 65 ("At least one value must be non-null"); 66 } 67 if (includeLower && null == lowerTerm) { 68 throw new IllegalArgumentException 69 ("The lower bound must be non-null to be inclusive"); 70 } 71 if (includeUpper && null == upperTerm) { 72 throw new IllegalArgumentException 73 ("The upper bound must be non-null to be inclusive"); 74 } 75 } 76 77 81 public static RangeFilter Less(String fieldName, String upperTerm) { 82 return new RangeFilter(fieldName, null, upperTerm, false, true); 83 } 84 85 89 public static RangeFilter More(String fieldName, String lowerTerm) { 90 return new RangeFilter(fieldName, lowerTerm, null, true, false); 91 } 92 93 98 public BitSet bits(IndexReader reader) throws IOException { 99 BitSet bits = new BitSet (reader.maxDoc()); 100 TermEnum enumerator = 101 (null != lowerTerm 102 ? reader.terms(new Term(fieldName, lowerTerm)) 103 : reader.terms(new Term(fieldName,""))); 104 105 try { 106 107 if (enumerator.term() == null) { 108 return bits; 109 } 110 111 boolean checkLower = false; 112 if (!includeLower) checkLower = true; 114 115 TermDocs termDocs = reader.termDocs(); 116 try { 117 118 do { 119 Term term = enumerator.term(); 120 if (term != null && term.field().equals(fieldName)) { 121 if (!checkLower || null==lowerTerm || term.text().compareTo(lowerTerm) > 0) { 122 checkLower = false; 123 if (upperTerm != null) { 124 int compare = upperTerm.compareTo(term.text()); 125 127 if ((compare < 0) || 128 (!includeUpper && compare==0)) { 129 break; 130 } 131 } 132 133 134 termDocs.seek(enumerator.term()); 135 while (termDocs.next()) { 136 bits.set(termDocs.doc()); 137 } 138 } 139 } else { 140 break; 141 } 142 } 143 while (enumerator.next()); 144 145 } finally { 146 termDocs.close(); 147 } 148 } finally { 149 enumerator.close(); 150 } 151 152 return bits; 153 } 154 155 public String toString() { 156 StringBuffer buffer = new StringBuffer (); 157 buffer.append(fieldName); 158 buffer.append(":"); 159 buffer.append(includeLower ? "[" : "{"); 160 if (null != lowerTerm) { 161 buffer.append(lowerTerm); 162 } 163 buffer.append("-"); 164 if (null != upperTerm) { 165 buffer.append(upperTerm); 166 } 167 buffer.append(includeUpper ? "]" : "}"); 168 return buffer.toString(); 169 } 170 171 172 public boolean equals(Object o) { 173 if (this == o) return true; 174 if (!(o instanceof RangeFilter)) return false; 175 RangeFilter other = (RangeFilter) o; 176 177 if (!this.fieldName.equals(other.fieldName) 178 || this.includeLower != other.includeLower 179 || this.includeUpper != other.includeUpper 180 ) { return false; } 181 if (this.lowerTerm != null ? !this.lowerTerm.equals(other.lowerTerm) : other.lowerTerm != null) return false; 182 if (this.upperTerm != null ? !this.upperTerm.equals(other.upperTerm) : other.upperTerm != null) return false; 183 return true; 184 } 185 186 187 public int hashCode() { 188 int h = fieldName.hashCode(); 189 h ^= lowerTerm != null ? lowerTerm.hashCode() : 0xB6ECE882; 190 h = (h << 1) | (h >>> 31); h ^= (upperTerm != null ? (upperTerm.hashCode()) : 0x91BEC2C2); 192 h ^= (includeLower ? 0xD484B933 : 0) 193 ^ (includeUpper ? 0x6AE423AC : 0); 194 return h; 195 } 196 } 197 | Popular Tags |