2 * Copyright 2004 The Apache Software Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using IndexReader
= Lucene
.Net
.Index
.IndexReader
;
20 namespace Lucene
.Net
.Search
23 /// <summary> A range query that returns a constant score equal to it's boost for
24 /// all documents in the range.
26 /// It does not have an upper bound on the number of clauses covered in the range.
28 /// If an endpoint is null, it is said to be "open".
29 /// Either or both endpoints may be open. Open endpoints may not be exclusive
30 /// (you can't select all but the first or last term without explicitly specifying the term to exclude.)
35 /// <version> $Id: ConstantScoreRangeQuery.cs,v 1.2 2006/10/02 17:09:02 joeshaw Exp $
39 public class ConstantScoreRangeQuery
: Query
41 private System
.String fieldName
;
42 private System
.String lowerVal
;
43 private System
.String upperVal
;
44 private bool includeLower
;
45 private bool includeUpper
;
48 public ConstantScoreRangeQuery(System
.String fieldName
, System
.String lowerVal
, System
.String upperVal
, bool includeLower
, bool includeUpper
)
50 // do a little bit of normalization...
51 // open ended range queries should always be inclusive.
56 else if (includeLower
&& lowerVal
.Equals(""))
66 this.fieldName
= String
.Intern(fieldName
); // intern it, just like terms...
67 this.lowerVal
= lowerVal
;
68 this.upperVal
= upperVal
;
69 this.includeLower
= includeLower
;
70 this.includeUpper
= includeUpper
;
73 /// <summary>Returns the field name for this query </summary>
74 public virtual System
.String
GetField()
78 /// <summary>Returns the value of the lower endpoint of this range query, null if open ended </summary>
79 public virtual System
.String
GetLowerVal()
83 /// <summary>Returns the value of the upper endpoint of this range query, null if open ended </summary>
84 public virtual System
.String
GetUpperVal()
88 /// <summary>Returns <code>true</code> if the lower endpoint is inclusive </summary>
89 public virtual bool IncludesLower()
93 /// <summary>Returns <code>true</code> if the upper endpoint is inclusive </summary>
94 public virtual bool IncludesUpper()
99 public override Query
Rewrite(IndexReader reader
)
101 // Map to RangeFilter semantics which are slightly different...
102 RangeFilter rangeFilt
= new RangeFilter(fieldName
, lowerVal
!= null ? lowerVal
: "", upperVal
, (System
.Object
) lowerVal
== (System
.Object
) ""?false:includeLower
, upperVal
== null?false:includeUpper
);
103 Query q
= new ConstantScoreQuery(rangeFilt
);
104 q
.SetBoost(GetBoost());
108 /// <summary>Prints a user-readable version of this query. </summary>
109 public override System
.String
ToString(System
.String field
)
111 System
.Text
.StringBuilder buffer
= new System
.Text
.StringBuilder();
112 if (!GetField().Equals(field
))
114 buffer
.Append(GetField());
117 buffer
.Append(includeLower
? '[' : '{');
118 buffer
.Append(lowerVal
!= null ? lowerVal
: "*");
119 buffer
.Append(" TO ");
120 buffer
.Append(upperVal
!= null ? upperVal
: "*");
121 buffer
.Append(includeUpper
? ']' : '}');
122 if (GetBoost() != 1.0f
)
125 buffer
.Append(GetBoost().ToString());
127 return buffer
.ToString();
130 /// <summary>Returns true if <code>o</code> is equal to this. </summary>
131 public override bool Equals(System
.Object o
)
135 if (!(o
is ConstantScoreRangeQuery
))
137 ConstantScoreRangeQuery other
= (ConstantScoreRangeQuery
) o
;
139 if ((System
.Object
) this.fieldName
!= (System
.Object
) other
.fieldName
|| this.includeLower
!= other
.includeLower
|| this.includeUpper
!= other
.includeUpper
)
143 if (this.lowerVal
!= null ? !this.lowerVal
.Equals(other
.lowerVal
) : other
.lowerVal
!= null)
145 if (this.upperVal
!= null ? !this.upperVal
.Equals(other
.upperVal
) : other
.upperVal
!= null)
147 return this.GetBoost() == other
.GetBoost();
150 /// <summary>Returns a hash code value for this object.</summary>
151 public override int GetHashCode()
153 int h
= BitConverter
.ToInt32(BitConverter
.GetBytes(GetBoost()), 0) ^ fieldName
.GetHashCode();
154 // hashCode of "" is 0, so don't use that for null...
155 h ^
= (lowerVal
!= null ? lowerVal
.GetHashCode() : unchecked((int) 0x965a965a)); // {{Aroush-1.9}} Is this OK?!
156 // don't just XOR upperVal with out mixing either it or h, as it will cancel
157 // out lowerVal if they are equal.
158 h ^
= ((h
<< 17) | (SupportClass
.Number
.URShift(h
, 16))); // a reversible (one to one) 32 bit mapping mix
159 h ^
= (upperVal
!= null ? (upperVal
.GetHashCode()) : 0x5a695a69);
160 h ^
= (includeLower
? 0x665599aa : 0) ^
(includeUpper
? unchecked((int) 0x99aa5566) : 0); // {{Aroush-1.9}} Is this OK?!
164 override public System
.Object
Clone()
166 // {{Aroush-1.9}} is this all that we need to clone?!
167 ConstantScoreRangeQuery clone
= (ConstantScoreRangeQuery
) base.Clone();
168 clone
.fieldName
= (System
.String
) this.fieldName
.Clone();
169 clone
.lowerVal
= (System
.String
) this.lowerVal
.Clone();
170 clone
.upperVal
= (System
.String
) this.upperVal
.Clone();
171 clone
.includeLower
= this.includeLower
;
172 clone
.includeUpper
= this.includeUpper
;