1 package org.apache.lucene.search; 2 3 18 19 import org.apache.lucene.index.IndexReader; 20 21 import java.io.IOException ; 22 23 36 37 public class ConstantScoreRangeQuery extends Query 38 { 39 private final String fieldName; 40 private final String lowerVal; 41 private final String upperVal; 42 private final boolean includeLower; 43 private final boolean includeUpper; 44 45 46 public ConstantScoreRangeQuery(String fieldName, String lowerVal, String upperVal, boolean includeLower, boolean includeUpper) 47 { 48 if (lowerVal==null) { 51 includeLower=true; 52 } else if (includeLower && lowerVal.equals("")) { 53 lowerVal=null; 54 } 55 if (upperVal==null) { 56 includeUpper=true; 57 } 58 59 60 this.fieldName = fieldName.intern(); this.lowerVal = lowerVal; 62 this.upperVal = upperVal; 63 this.includeLower = includeLower; 64 this.includeUpper = includeUpper; 65 } 66 67 68 public String getField() { return fieldName; } 69 70 public String getLowerVal() { return lowerVal; } 71 72 public String getUpperVal() { return upperVal; } 73 74 public boolean includesLower() { return includeLower; } 75 76 public boolean includesUpper() { return includeUpper; } 77 78 public Query rewrite(IndexReader reader) throws IOException { 79 RangeFilter rangeFilt = new RangeFilter(fieldName, 81 lowerVal!=null?lowerVal:"", 82 upperVal, lowerVal==""?false:includeLower, upperVal==null?false:includeUpper); 83 Query q = new ConstantScoreQuery(rangeFilt); 84 q.setBoost(getBoost()); 85 return q; 86 } 87 88 89 public String toString(String field) 90 { 91 StringBuffer buffer = new StringBuffer (); 92 if (!getField().equals(field)) 93 { 94 buffer.append(getField()); 95 buffer.append(":"); 96 } 97 buffer.append(includeLower ? '[' : '{'); 98 buffer.append(lowerVal != null ? lowerVal : "*"); 99 buffer.append(" TO "); 100 buffer.append(upperVal != null ? upperVal : "*"); 101 buffer.append(includeUpper ? ']' : '}'); 102 if (getBoost() != 1.0f) 103 { 104 buffer.append("^"); 105 buffer.append(Float.toString(getBoost())); 106 } 107 return buffer.toString(); 108 } 109 110 111 public boolean equals(Object o) { 112 if (this == o) return true; 113 if (!(o instanceof ConstantScoreRangeQuery)) return false; 114 ConstantScoreRangeQuery other = (ConstantScoreRangeQuery) o; 115 116 if (this.fieldName != other.fieldName || this.includeLower != other.includeLower 118 || this.includeUpper != other.includeUpper 119 ) { return false; } 120 if (this.lowerVal != null ? !this.lowerVal.equals(other.lowerVal) : other.lowerVal != null) return false; 121 if (this.upperVal != null ? !this.upperVal.equals(other.upperVal) : other.upperVal != null) return false; 122 return this.getBoost() == other.getBoost(); 123 } 124 125 126 public int hashCode() { 127 int h = Float.floatToIntBits(getBoost()) ^ fieldName.hashCode(); 128 h ^= lowerVal != null ? lowerVal.hashCode() : 0x965a965a; 130 h ^= (h << 17) | (h >>> 16); h ^= (upperVal != null ? (upperVal.hashCode()) : 0x5a695a69); 134 h ^= (includeLower ? 0x665599aa : 0) 135 ^ (includeUpper ? 0x99aa5566 : 0); 136 return h; 137 } 138 } 139 | Popular Tags |