1 package org.apache.lucene.search; 2 3 18 19 import java.io.IOException ; 20 21 import org.apache.lucene.index.Term; 22 import org.apache.lucene.index.TermEnum; 23 import org.apache.lucene.index.IndexReader; 24 import org.apache.lucene.util.ToStringUtils; 25 26 32 public class RangeQuery extends Query 33 { 34 private Term lowerTerm; 35 private Term upperTerm; 36 private boolean inclusive; 37 38 44 public RangeQuery(Term lowerTerm, Term upperTerm, boolean inclusive) 45 { 46 if (lowerTerm == null && upperTerm == null) 47 { 48 throw new IllegalArgumentException ("At least one term must be non-null"); 49 } 50 if (lowerTerm != null && upperTerm != null && lowerTerm.field() != upperTerm.field()) 51 { 52 throw new IllegalArgumentException ("Both terms must be for the same field"); 53 } 54 55 if (lowerTerm != null) { 57 this.lowerTerm = lowerTerm; 58 } 59 else { 60 this.lowerTerm = new Term(upperTerm.field(), ""); 61 } 62 63 this.upperTerm = upperTerm; 64 this.inclusive = inclusive; 65 } 66 67 public Query rewrite(IndexReader reader) throws IOException { 68 69 BooleanQuery query = new BooleanQuery(true); 70 TermEnum enumerator = reader.terms(lowerTerm); 71 72 try { 73 74 boolean checkLower = false; 75 if (!inclusive) checkLower = true; 77 78 String testField = getField(); 79 80 do { 81 Term term = enumerator.term(); 82 if (term != null && term.field() == testField) { 83 if (!checkLower || term.text().compareTo(lowerTerm.text()) > 0) { 84 checkLower = false; 85 if (upperTerm != null) { 86 int compare = upperTerm.text().compareTo(term.text()); 87 89 if ((compare < 0) || (!inclusive && compare == 0)) 90 break; 91 } 92 TermQuery tq = new TermQuery(term); tq.setBoost(getBoost()); query.add(tq, BooleanClause.Occur.SHOULD); } 96 } 97 else { 98 break; 99 } 100 } 101 while (enumerator.next()); 102 } 103 finally { 104 enumerator.close(); 105 } 106 return query; 107 } 108 109 110 public String getField() { 111 return (lowerTerm != null ? lowerTerm.field() : upperTerm.field()); 112 } 113 114 115 public Term getLowerTerm() { return lowerTerm; } 116 117 118 public Term getUpperTerm() { return upperTerm; } 119 120 121 public boolean isInclusive() { return inclusive; } 122 123 124 125 public String toString(String field) 126 { 127 StringBuffer buffer = new StringBuffer (); 128 if (!getField().equals(field)) 129 { 130 buffer.append(getField()); 131 buffer.append(":"); 132 } 133 buffer.append(inclusive ? "[" : "{"); 134 buffer.append(lowerTerm != null ? lowerTerm.text() : "null"); 135 buffer.append(" TO "); 136 buffer.append(upperTerm != null ? upperTerm.text() : "null"); 137 buffer.append(inclusive ? "]" : "}"); 138 buffer.append(ToStringUtils.boost(getBoost())); 139 return buffer.toString(); 140 } 141 142 143 public boolean equals(Object o) { 144 if (this == o) return true; 145 if (!(o instanceof RangeQuery)) return false; 146 147 final RangeQuery other = (RangeQuery) o; 148 if (this.getBoost() != other.getBoost()) return false; 149 if (this.inclusive != other.inclusive) return false; 150 if (this.lowerTerm != null ? !this.lowerTerm.equals(other.lowerTerm) : other.lowerTerm != null) return false; 152 if (this.upperTerm != null ? !this.upperTerm.equals(other.upperTerm) : other.upperTerm != null) return false; 153 return true; 154 } 155 156 157 public int hashCode() { 158 int h = Float.floatToIntBits(getBoost()); 159 h ^= lowerTerm != null ? lowerTerm.hashCode() : 0; 160 h ^= (h << 25) | (h >>> 8); 163 h ^= upperTerm != null ? upperTerm.hashCode() : 0; 164 h ^= this.inclusive ? 0x2742E74A : 0; 165 return h; 166 } 167 } 168 | Popular Tags |