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