19 using System.Collections.Generic;
20 using Lucene.Net.Index;
29 namespace Lucene.Net.Search
157 where T : struct, IComparable<T>
159 internal NumericRangeQuery(System.String field,
int precisionStep,
int valSize, T? min, T? max,
bool minInclusive,
bool maxInclusive)
161 System.Diagnostics.Debug.Assert((valSize == 32 || valSize == 64));
162 if (precisionStep < 1)
163 throw new System.ArgumentException(
"precisionStep must be >=1");
165 this.precisionStep = precisionStep;
166 this.valSize = valSize;
169 this.minInclusive = minInclusive;
170 this.maxInclusive = maxInclusive;
180 RewriteMethod = (precisionStep > 6)?CONSTANT_SCORE_FILTER_REWRITE:CONSTANT_SCORE_AUTO_REWRITE_DEFAULT;
184 RewriteMethod = (precisionStep > 8)?CONSTANT_SCORE_FILTER_REWRITE:CONSTANT_SCORE_AUTO_REWRITE_DEFAULT;
189 throw new System.ArgumentException(
"valSize must be 32 or 64");
194 if (min != null && min.Equals(max))
203 return new NumericRangeTermEnum(
this, reader);
209 get {
return field; }
213 public bool IncludesMin
215 get {
return minInclusive; }
219 public bool IncludesMax
221 get {
return maxInclusive; }
236 public override System.String ToString(System.String field)
238 System.Text.StringBuilder sb =
new System.Text.StringBuilder();
239 if (!this.field.Equals(field))
240 sb.Append(
this.field).Append(
':');
241 return sb.Append(minInclusive ?
'[' :
'{').Append((min == null) ?
"*" : min.ToString()).Append(
" TO ").Append((max == null) ?
"*" : max.ToString()).Append(maxInclusive ?
']' :
'}').Append(
ToStringUtils.Boost(Boost)).ToString();
244 public override bool Equals(System.Object o)
252 NumericRangeQuery<T> q = (NumericRangeQuery<T>)o;
253 return ((System.Object)field == (System.Object)q.field && (q.min == null ? min == null : q.min.Equals(min)) && (q.max == null ? max == null : q.max.Equals(max)) && minInclusive == q.minInclusive && maxInclusive == q.maxInclusive && precisionStep == q.precisionStep);
258 public override int GetHashCode()
260 int hash = base.GetHashCode();
261 hash += (field.GetHashCode() ^ 0x4565fd66 + precisionStep ^ 0x64365465);
263 hash += (min.GetHashCode() ^ 0x14fa55fb);
265 hash += (max.GetHashCode() ^ 0x733fa5fe);
266 return hash + (minInclusive.GetHashCode() ^ 0x14fa55fb) + (maxInclusive.GetHashCode() ^ 0x733fa5fe);
277 [System.Runtime.Serialization.OnDeserialized]
278 internal void OnDeserialized(System.Runtime.Serialization.StreamingContext context)
284 internal System.String field;
285 internal int precisionStep;
286 internal int valSize;
289 internal bool minInclusive;
290 internal bool maxInclusive;
301 private sealed
class NumericRangeTermEnum:FilteredTermEnum
303 private class AnonymousClassLongRangeBuilder:
NumericUtils.LongRangeBuilder
305 public AnonymousClassLongRangeBuilder(NumericRangeTermEnum enclosingInstance)
307 InitBlock(enclosingInstance);
309 private void InitBlock(NumericRangeTermEnum enclosingInstance)
311 this.enclosingInstance = enclosingInstance;
313 private NumericRangeTermEnum enclosingInstance;
314 public NumericRangeTermEnum Enclosing_Instance
318 return enclosingInstance;
323 public override void AddRange(System.String minPrefixCoded, System.String maxPrefixCoded)
325 Enclosing_Instance.rangeBounds.AddLast(minPrefixCoded);
326 Enclosing_Instance.rangeBounds.AddLast(maxPrefixCoded);
329 private class AnonymousClassIntRangeBuilder:
NumericUtils.IntRangeBuilder
331 public AnonymousClassIntRangeBuilder(NumericRangeTermEnum enclosingInstance)
333 InitBlock(enclosingInstance);
335 private void InitBlock(NumericRangeTermEnum enclosingInstance)
337 this.enclosingInstance = enclosingInstance;
339 private NumericRangeTermEnum enclosingInstance;
340 public NumericRangeTermEnum Enclosing_Instance
344 return enclosingInstance;
349 public override void AddRange(System.String minPrefixCoded, System.String maxPrefixCoded)
351 Enclosing_Instance.rangeBounds.AddLast(minPrefixCoded);
352 Enclosing_Instance.rangeBounds.AddLast(maxPrefixCoded);
355 private void InitBlock(NumericRangeQuery<T> enclosingInstance)
357 this.enclosingInstance = enclosingInstance;
358 termTemplate =
new Term(Enclosing_Instance.field);
360 private NumericRangeQuery<T> enclosingInstance;
361 public NumericRangeQuery<T> Enclosing_Instance
365 return enclosingInstance;
371 private LinkedList<string> rangeBounds =
new LinkedList<string>();
372 private Term termTemplate;
373 private System.String currentUpperBound = null;
375 private bool isDisposed;
377 internal NumericRangeTermEnum(NumericRangeQuery<T> enclosingInstance,
IndexReader reader)
379 InitBlock(enclosingInstance);
380 this.reader = reader;
382 Type rangeType = Nullable.GetUnderlyingType(typeof(T?));
383 switch (Enclosing_Instance.valSize)
387 long minBound = System.Int64.MinValue;
388 if (rangeType == typeof(System.Int64))
392 if (Enclosing_Instance.min != null)
393 minBound = System.Convert.ToInt64(Enclosing_Instance.min);
395 else if (rangeType == typeof(System.Double))
397 if (Enclosing_Instance.min != null)
398 minBound =
NumericUtils.DoubleToSortableLong(System.Convert.ToDouble(Enclosing_Instance.min));
400 if (!Enclosing_Instance.minInclusive && Enclosing_Instance.min != null)
402 if (minBound == System.Int64.MaxValue)
408 long maxBound = System.Int64.MaxValue;
409 if (rangeType == typeof(System.Int64))
411 if (Enclosing_Instance.max != null)
412 maxBound = System.Convert.ToInt64(Enclosing_Instance.max);
414 else if (rangeType == typeof(System.Double))
416 if (Enclosing_Instance.max != null)
417 maxBound =
NumericUtils.DoubleToSortableLong(System.Convert.ToDouble(Enclosing_Instance.max));
419 if (!Enclosing_Instance.maxInclusive && Enclosing_Instance.max != null)
421 if (maxBound == System.Int64.MinValue)
426 NumericUtils.SplitLongRange(
new AnonymousClassLongRangeBuilder(
this), Enclosing_Instance.precisionStep, minBound, maxBound);
433 int minBound = System.Int32.MinValue;
434 if (rangeType == typeof(System.Int32))
436 if (Enclosing_Instance.min != null)
437 minBound = System.Convert.ToInt32(Enclosing_Instance.min);
439 else if (rangeType == typeof(System.Single))
441 if (Enclosing_Instance.min != null)
442 minBound =
NumericUtils.FloatToSortableInt(System.Convert.ToSingle(Enclosing_Instance.min));
444 if (!Enclosing_Instance.minInclusive && Enclosing_Instance.min != null)
446 if (minBound == System.Int32.MaxValue)
452 int maxBound = System.Int32.MaxValue;
453 if (rangeType == typeof(System.Int32))
455 if (Enclosing_Instance.max != null)
456 maxBound = System.Convert.ToInt32(Enclosing_Instance.max);
458 else if (rangeType == typeof(System.Single))
460 if (Enclosing_Instance.max != null)
461 maxBound =
NumericUtils.FloatToSortableInt(System.Convert.ToSingle(Enclosing_Instance.max));
463 if (!Enclosing_Instance.maxInclusive && Enclosing_Instance.max != null)
465 if (maxBound == System.Int32.MinValue)
470 NumericUtils.SplitIntRange(
new AnonymousClassIntRangeBuilder(
this), Enclosing_Instance.precisionStep, minBound, maxBound);
477 throw new System.ArgumentException(
"valSize must be 32 or 64");
486 public override float Difference()
493 public override bool EndEnum()
495 throw new NotSupportedException(
"not implemented");
499 protected internal override void SetEnum(
TermEnum tenum)
501 throw new NotSupportedException(
"not implemented");
511 protected internal override bool TermCompare(
Term term)
513 return (term.
Field == Enclosing_Instance.field && String.CompareOrdinal(term.
Text, currentUpperBound) <= 0);
518 public override bool Next()
522 if (currentTerm != null)
524 System.Diagnostics.Debug.Assert(actualEnum != null);
525 if (actualEnum.Next())
527 currentTerm = actualEnum.Term;
528 if (TermCompare(currentTerm))
535 while (rangeBounds.Count >= 2)
538 if (actualEnum != null)
543 string lowerBound = rangeBounds.First.Value;
544 rangeBounds.RemoveFirst();
545 this.currentUpperBound = rangeBounds.First.Value;
546 rangeBounds.RemoveFirst();
548 actualEnum = reader.
Terms(termTemplate.CreateTerm(lowerBound));
549 currentTerm = actualEnum.
Term;
550 if (currentTerm != null && TermCompare(currentTerm))
557 System.Diagnostics.Debug.Assert(rangeBounds.Count == 0 && currentTerm == null);
562 protected override void Dispose(
bool disposing)
564 if (isDisposed)
return;
567 currentUpperBound = null;
570 base.Dispose(disposing);
575 public static class NumericRangeQuery
583 public static NumericRangeQuery<long> NewLongRange(System.String field,
int precisionStep,
long? min,
long? max,
bool minInclusive,
bool maxInclusive)
585 return new NumericRangeQuery<long>(field, precisionStep, 64, min, max, minInclusive, maxInclusive);
594 public static NumericRangeQuery<long> NewLongRange(System.String field,
long? min,
long? max,
bool minInclusive,
bool maxInclusive)
596 return new NumericRangeQuery<long>(field,
NumericUtils.PRECISION_STEP_DEFAULT, 64, min, max, minInclusive, maxInclusive);
605 public static NumericRangeQuery<int> NewIntRange(System.String field,
int precisionStep,
int? min,
int? max,
bool minInclusive,
bool maxInclusive)
607 return new NumericRangeQuery<int>(field, precisionStep, 32, min, max, minInclusive, maxInclusive);
616 public static NumericRangeQuery<int> NewIntRange(System.String field,
int? min,
int? max,
bool minInclusive,
bool maxInclusive)
618 return new NumericRangeQuery<int>(field,
NumericUtils.PRECISION_STEP_DEFAULT, 32, min, max, minInclusive, maxInclusive);
627 public static NumericRangeQuery<double> NewDoubleRange(System.String field,
int precisionStep,
double? min,
double? max,
bool minInclusive,
bool maxInclusive)
629 return new NumericRangeQuery<double>(field, precisionStep, 64, min, max, minInclusive, maxInclusive);
638 public static NumericRangeQuery<double> NewDoubleRange(System.String field,
double? min,
double? max,
bool minInclusive,
bool maxInclusive)
640 return new NumericRangeQuery<double>(field,
NumericUtils.PRECISION_STEP_DEFAULT, 64, min, max, minInclusive, maxInclusive);
649 public static NumericRangeQuery<float> NewFloatRange(System.String field,
int precisionStep,
float? min,
float? max,
bool minInclusive,
bool maxInclusive)
651 return new NumericRangeQuery<float>(field, precisionStep, 32, min, max, minInclusive, maxInclusive);
660 public static NumericRangeQuery<float> NewFloatRange(System.String field,
float? min,
float? max,
bool minInclusive,
bool maxInclusive)
662 return new NumericRangeQuery<float>(field,
NumericUtils.PRECISION_STEP_DEFAULT, 32, min, max, minInclusive, maxInclusive);