/// <summary> Initializes the token stream with the supplied <c>double</c> value.</summary> /// <param name="value_Renamed">the value, for which this TokenStream should enumerate tokens. /// </param> /// <returns> this instance, because of this you can use it the following way: /// <c>new Field(name, new NumericTokenStream(precisionStep).SetDoubleValue(value))</c> /// </returns> public NumericTokenStream SetDoubleValue(double value_Renamed) { this.value_Renamed = NumericUtils.DoubleToSortableLong(value_Renamed); valSize = 64; shift = 0; return(this); }
private void testEnum(int lower, int upper) { NumericRangeQuery <int> q = NumericRangeQuery.NewIntRange("field4", 4, lower, upper, true, true); FilteredTermEnum termEnum = q.GetEnum(searcher.IndexReader, null); try { int count = 0; do { Term t = termEnum.Term; if (t != null) { int val = NumericUtils.PrefixCodedToInt(t.Text); Assert.True(val >= lower && val <= upper, "value not in bounds"); count++; } else { break; } } while (termEnum.Next(null)); Assert.False(termEnum.Next(null)); Console.WriteLine("TermEnum on 'field4' for range [" + lower + "," + upper + "] contained " + count + " terms."); } finally { termEnum.Close(); } }
public virtual float ParseFloat(System.String val) { int shift = val[0] - NumericUtils.SHIFT_START_INT; if (shift > 0 && shift <= 31) throw new FieldCacheImpl.StopFillCacheException(); return NumericUtils.SortableIntToFloat(NumericUtils.PrefixCodedToInt(val)); }
/// <summary> Initializes the token stream with the supplied <c>float</c> value.</summary> /// <param name="value_Renamed">the value, for which this TokenStream should enumerate tokens. /// </param> /// <returns> this instance, because of this you can use it the following way: /// <c>new Field(name, new NumericTokenStream(precisionStep).SetFloatValue(value))</c> /// </returns> public NumericTokenStream SetFloatValue(float value_Renamed) { this.value_Renamed = (long)NumericUtils.FloatToSortableInt(value_Renamed); valSize = 32; shift = 0; return(this); }
public virtual double ParseDouble(System.String val) { int shift = val[0] - NumericUtils.SHIFT_START_LONG; if (shift > 0 && shift <= 63) throw new FieldCacheImpl.StopFillCacheException(); return NumericUtils.SortableLongToDouble(NumericUtils.PrefixCodedToLong(val)); }
public virtual int ParseInt(string val) { int shift = val[0] - NumericUtils.SHIFT_START_INT; if (shift > 0 && shift <= 31) { throw new FieldCacheImpl.StopFillCacheException(); } return(NumericUtils.PrefixCodedToInt(val)); }
public virtual long ParseLong(System.String val) { int shift = val[0] - NumericUtils.SHIFT_START_LONG; if (shift > 0 && shift <= 63) { throw new FieldCacheImpl.StopFillCacheException(); } return(NumericUtils.PrefixCodedToLong(val)); }
public virtual void TestIntStream() { NumericTokenStream stream = new NumericTokenStream().SetIntValue(ivalue); // use getAttribute to test if attributes really exist, if not an IAE will be throwed TermAttribute termAtt = (TermAttribute)stream.GetAttribute(typeof(TermAttribute)); TypeAttribute typeAtt = (TypeAttribute)stream.GetAttribute(typeof(TypeAttribute)); for (int shift = 0; shift < 32; shift += NumericUtils.PRECISION_STEP_DEFAULT) { Assert.IsTrue(stream.IncrementToken(), "New token is available"); Assert.AreEqual(NumericUtils.IntToPrefixCoded(ivalue, shift), termAtt.Term(), "Term is correctly encoded"); Assert.AreEqual((shift == 0)?NumericTokenStream.TOKEN_TYPE_FULL_PREC:NumericTokenStream.TOKEN_TYPE_LOWER_PREC, typeAtt.Type(), "Type correct"); } Assert.IsFalse(stream.IncrementToken(), "No more tokens available"); }
public override DocIdSet GetDocIdSet(IndexReader reader) { // we transform the floating point numbers to sortable integers // using NumericUtils to easier find the next bigger/lower value double inclusiveLowerPoint; double inclusiveUpperPoint; if (lowerVal != null) { double f = System.Convert.ToDouble(((System.ValueType)lowerVal)); if (!includeUpper && f > 0.0 && System.Double.IsInfinity(f)) { return(DocIdSet.EMPTY_DOCIDSET); } long i = NumericUtils.DoubleToSortableLong(f); inclusiveLowerPoint = NumericUtils.SortableLongToDouble(includeLower?i:(i + 1L)); } else { inclusiveLowerPoint = System.Double.NegativeInfinity; } if (upperVal != null) { double f = System.Convert.ToDouble(((System.ValueType)upperVal)); if (!includeUpper && f < 0.0 && System.Double.IsInfinity(f)) { return(DocIdSet.EMPTY_DOCIDSET); } long i = NumericUtils.DoubleToSortableLong(f); inclusiveUpperPoint = NumericUtils.SortableLongToDouble(includeUpper?i:(i - 1L)); } else { inclusiveUpperPoint = System.Double.PositiveInfinity; } if (inclusiveLowerPoint > inclusiveUpperPoint) { return(DocIdSet.EMPTY_DOCIDSET); } double[] values = Lucene.Net.Search.FieldCache_Fields.DEFAULT.GetDoubles(reader, field, (Lucene.Net.Search.DoubleParser)parser); // we only request the usage of termDocs, if the range contains 0 return(new AnonymousClassFieldCacheDocIdSet(values, inclusiveLowerPoint, inclusiveUpperPoint, this, reader, (inclusiveLowerPoint <= 0.0 && inclusiveUpperPoint >= 0.0))); }
public override DocIdSet GetDocIdSet(IndexReader reader) { // we transform the floating point numbers to sortable integers // using NumericUtils to easier find the next bigger/lower value float inclusiveLowerPoint; float inclusiveUpperPoint; if (lowerVal != null) { float f = (float)lowerVal; if (!includeUpper && f > 0.0f && float.IsInfinity(f)) { return(DocIdSet.EMPTY_DOCIDSET); } int i = NumericUtils.FloatToSortableInt(f); inclusiveLowerPoint = NumericUtils.SortableIntToFloat(includeLower ? i : (i + 1)); } else { inclusiveLowerPoint = float.NegativeInfinity; } if (upperVal != null) { float f = (float)upperVal; if (!includeUpper && f < 0.0f && float.IsInfinity(f)) { return(DocIdSet.EMPTY_DOCIDSET); } int i = NumericUtils.FloatToSortableInt(f); inclusiveUpperPoint = NumericUtils.SortableIntToFloat(includeUpper ? i : (i - 1)); } else { inclusiveUpperPoint = float.PositiveInfinity; } if (inclusiveLowerPoint > inclusiveUpperPoint) { return(DocIdSet.EMPTY_DOCIDSET); } float[] values = Lucene.Net.Search.FieldCache_Fields.DEFAULT.GetFloats(reader, field, (Lucene.Net.Search.FloatParser)parser); // we only request the usage of termDocs, if the range contains 0 return(new AnonymousClassFieldCacheDocIdSet(values, inclusiveLowerPoint, inclusiveUpperPoint, this, reader, (inclusiveLowerPoint <= 0.0f && inclusiveUpperPoint >= 0.0f))); }
/// <summary>we fake a float test using int2float conversion of NumericUtils </summary> private void TestFloatRange(int precisionStep) { System.String field = "ascfield" + precisionStep; int lower = -1000; int upper = +2000; System.Single tempAux = (float)NumericUtils.SortableIntToFloat(lower); System.Single tempAux2 = (float)NumericUtils.SortableIntToFloat(upper); Query tq = NumericRangeQuery.NewFloatRange(field, precisionStep, tempAux, tempAux2, true, true); TopDocs tTopDocs = searcher.Search(tq, 1, null); Assert.AreEqual(upper - lower + 1, tTopDocs.TotalHits, "Returned count of range query must be equal to inclusive range length"); System.Single tempAux3 = (float)NumericUtils.SortableIntToFloat(lower); System.Single tempAux4 = (float)NumericUtils.SortableIntToFloat(upper); Filter tf = NumericRangeFilter.NewFloatRange(field, precisionStep, tempAux3, tempAux4, true, true); tTopDocs = searcher.Search((Query) new MatchAllDocsQuery(), tf, (int)1, (IState)null); Assert.AreEqual(upper - lower + 1, tTopDocs.TotalHits, "Returned count of range filter must be equal to inclusive range length"); }
/// <summary>we fake a double test using long2double conversion of NumericUtils </summary> private void TestDoubleRange(int precisionStep) { System.String field = "ascfield" + precisionStep; long lower = -1000L; long upper = +2000L; System.Double tempAux = (double)NumericUtils.SortableLongToDouble(lower); System.Double tempAux2 = (double)NumericUtils.SortableLongToDouble(upper); Query tq = NumericRangeQuery.NewDoubleRange(field, precisionStep, tempAux, tempAux2, true, true); TopDocs tTopDocs = searcher.Search(tq, 1); Assert.AreEqual(upper - lower + 1, tTopDocs.totalHits, "Returned count of range query must be equal to inclusive range length"); System.Double tempAux3 = (double)NumericUtils.SortableLongToDouble(lower); System.Double tempAux4 = (double)NumericUtils.SortableLongToDouble(upper); Filter tf = NumericRangeFilter.NewDoubleRange(field, precisionStep, tempAux3, tempAux4, true, true); tTopDocs = searcher.Search(new MatchAllDocsQuery(), tf, 1); Assert.AreEqual(upper - lower + 1, tTopDocs.totalHits, "Returned count of range filter must be equal to inclusive range length"); }
private bool IncrementTokenUnlikely() { char[] buffer; if (valSize == 64) { buffer = termAtt.ResizeTermBuffer(NumericUtils.BUF_SIZE_LONG); termAtt.SetTermLength(NumericUtils.LongToPrefixCoded(value_Renamed, shift, buffer)); } else if (valSize == 32) { buffer = termAtt.ResizeTermBuffer(NumericUtils.BUF_SIZE_INT); termAtt.SetTermLength(NumericUtils.IntToPrefixCoded((int)value_Renamed, shift, buffer)); } else { return(ThrowValueSizeIsNotValid <bool> ()); } typeAtt.Type = (shift == 0) ? TOKEN_TYPE_FULL_PREC : TOKEN_TYPE_LOWER_PREC; posIncrAtt.PositionIncrement = (shift == 0) ? 1 : 0; shift += precisionStep; return(true); }
// @Override public override bool IncrementToken() { if (valSize == 0) { throw new System.SystemException("call set???Value() before usage"); } if (shift >= valSize) { return(false); } ClearAttributes(); char[] buffer; switch (valSize) { case 64: buffer = termAtt.ResizeTermBuffer(NumericUtils.BUF_SIZE_LONG); termAtt.SetTermLength(NumericUtils.LongToPrefixCoded(value_Renamed, shift, buffer)); break; case 32: buffer = termAtt.ResizeTermBuffer(NumericUtils.BUF_SIZE_INT); termAtt.SetTermLength(NumericUtils.IntToPrefixCoded((int)value_Renamed, shift, buffer)); break; default: // should not happen throw new System.ArgumentException("valSize must be 32 or 64"); } typeAtt.Type = (shift == 0)?TOKEN_TYPE_FULL_PREC:TOKEN_TYPE_LOWER_PREC; posIncrAtt.PositionIncrement = (shift == 0)?1:0; shift += precisionStep; return(true); }
// @Override public override bool IncrementToken() { if (valSize == 0) { goto Error; } if (shift >= valSize) { return(false); } ClearAttributes(); var termAttLikely = termAtt as TermAttribute; if (termAttLikely == null) { goto Unlikely; } char[] buffer; if (valSize == 64) { buffer = termAttLikely.ResizeTermBuffer(NumericUtils.BUF_SIZE_LONG); termAttLikely.SetTermLength(NumericUtils.LongToPrefixCoded(value_Renamed, shift, buffer)); } else if (valSize == 32) { buffer = termAttLikely.ResizeTermBuffer(NumericUtils.BUF_SIZE_INT); termAttLikely.SetTermLength(NumericUtils.IntToPrefixCoded((int)value_Renamed, shift, buffer)); } else { goto Error; } string type = (shift == 0) ? TOKEN_TYPE_FULL_PREC : TOKEN_TYPE_LOWER_PREC; int increment = (shift == 0) ? 1 : 0; // PERF: Try to avoid as much as possible the virtual calls here. var typeAttConcrete = typeAtt as TypeAttribute; if (typeAttConcrete != null) { typeAttConcrete.Type = type; } else { typeAtt.Type = type; } // PERF: Try to avoid as much as possible the virtual calls here. var posIncrAttConcrete = posIncrAtt as PositionIncrementAttribute; if (posIncrAttConcrete != null) { posIncrAttConcrete.PositionIncrement = increment; } else { posIncrAtt.PositionIncrement = increment; } shift += precisionStep; return(true); Unlikely: return(IncrementTokenUnlikely()); Error: return(ThrowValueSizeIsNotValid <bool>()); }
internal NumericRangeTermEnum(NumericRangeQuery enclosingInstance, IndexReader reader) { InitBlock(enclosingInstance); this.reader = reader; switch (Enclosing_Instance.valSize) { case 64: { // lower long minBound = System.Int64.MinValue; if (Enclosing_Instance.min is System.Int64) { minBound = System.Convert.ToInt64(Enclosing_Instance.min); } else if (Enclosing_Instance.min is System.Double) { minBound = NumericUtils.DoubleToSortableLong(System.Convert.ToDouble(Enclosing_Instance.min)); } if (!Enclosing_Instance.minInclusive && Enclosing_Instance.min != null) { if (minBound == System.Int64.MaxValue) { break; } minBound++; } // upper long maxBound = System.Int64.MaxValue; if (Enclosing_Instance.max is System.Int64) { maxBound = System.Convert.ToInt64(Enclosing_Instance.max); } else if (Enclosing_Instance.max is System.Double) { maxBound = NumericUtils.DoubleToSortableLong(System.Convert.ToDouble(Enclosing_Instance.max)); } if (!Enclosing_Instance.maxInclusive && Enclosing_Instance.max != null) { if (maxBound == System.Int64.MinValue) { break; } maxBound--; } NumericUtils.SplitLongRange(new AnonymousClassLongRangeBuilder(this), Enclosing_Instance.precisionStep, minBound, maxBound); break; } case 32: { // lower int minBound = System.Int32.MinValue; if (Enclosing_Instance.min is System.Int32) { minBound = System.Convert.ToInt32(Enclosing_Instance.min); } else if (Enclosing_Instance.min is System.Single) { minBound = NumericUtils.FloatToSortableInt(System.Convert.ToSingle(Enclosing_Instance.min)); } if (!Enclosing_Instance.minInclusive && Enclosing_Instance.min != null) { if (minBound == System.Int32.MaxValue) { break; } minBound++; } // upper int maxBound = System.Int32.MaxValue; if (Enclosing_Instance.max is System.Int32) { maxBound = System.Convert.ToInt32(Enclosing_Instance.max); } else if (Enclosing_Instance.max is System.Single) { maxBound = NumericUtils.FloatToSortableInt(System.Convert.ToSingle(Enclosing_Instance.max)); } if (!Enclosing_Instance.maxInclusive && Enclosing_Instance.max != null) { if (maxBound == System.Int32.MinValue) { break; } maxBound--; } NumericUtils.SplitIntRange(new AnonymousClassIntRangeBuilder(this), Enclosing_Instance.precisionStep, minBound, maxBound); break; } default: // should never happen throw new System.ArgumentException("valSize must be 32 or 64"); } // seek to first term Next(); }
internal NumericRangeTermEnum(NumericRangeQuery <T> enclosingInstance, IndexReader reader) { InitBlock(enclosingInstance); this.reader = reader; Type rangeType = Nullable.GetUnderlyingType(typeof(T?)); switch (Enclosing_Instance.valSize) { case 64: { // lower long minBound = long.MinValue; if (rangeType == typeof(long)) { // added in these checks to emulate java. passing null give it no type (in old code), // but .net can identifies it with generics and sets the bounds to 0, causing tests to fail if (Enclosing_Instance.min != null) { minBound = System.Convert.ToInt64(Enclosing_Instance.min); } } else if (rangeType == typeof(double)) { if (Enclosing_Instance.min != null) { minBound = NumericUtils.DoubleToSortableLong(System.Convert.ToDouble(Enclosing_Instance.min)); } } if (!Enclosing_Instance.minInclusive && Enclosing_Instance.min != null) { if (minBound == long.MaxValue) { break; } minBound++; } // upper long maxBound = long.MaxValue; if (rangeType == typeof(long)) { if (Enclosing_Instance.max != null) { maxBound = System.Convert.ToInt64(Enclosing_Instance.max); } } else if (rangeType == typeof(double)) { if (Enclosing_Instance.max != null) { maxBound = NumericUtils.DoubleToSortableLong(System.Convert.ToDouble(Enclosing_Instance.max)); } } if (!Enclosing_Instance.maxInclusive && Enclosing_Instance.max != null) { if (maxBound == long.MinValue) { break; } maxBound--; } NumericUtils.SplitLongRange(new AnonymousClassLongRangeBuilder(this), Enclosing_Instance.precisionStep, minBound, maxBound); break; } case 32: { // lower int minBound = int.MinValue; if (rangeType == typeof(int)) { if (Enclosing_Instance.min != null) { minBound = System.Convert.ToInt32(Enclosing_Instance.min); } } else if (rangeType == typeof(float)) { if (Enclosing_Instance.min != null) { minBound = NumericUtils.FloatToSortableInt(System.Convert.ToSingle(Enclosing_Instance.min)); } } if (!Enclosing_Instance.minInclusive && Enclosing_Instance.min != null) { if (minBound == int.MaxValue) { break; } minBound++; } // upper int maxBound = int.MaxValue; if (rangeType == typeof(int)) { if (Enclosing_Instance.max != null) { maxBound = System.Convert.ToInt32(Enclosing_Instance.max); } } else if (rangeType == typeof(float)) { if (Enclosing_Instance.max != null) { maxBound = NumericUtils.FloatToSortableInt(System.Convert.ToSingle(Enclosing_Instance.max)); } } if (!Enclosing_Instance.maxInclusive && Enclosing_Instance.max != null) { if (maxBound == int.MinValue) { break; } maxBound--; } NumericUtils.SplitIntRange(new AnonymousClassIntRangeBuilder(this), Enclosing_Instance.precisionStep, minBound, maxBound); break; } default: // should never happen throw new System.ArgumentException("valSize must be 32 or 64"); } // seek to first term Next(); }
private void TestRandomTrieAndClassicRangeQuery(int precisionStep) { System.Random rnd = NewRandom(); System.String field = "field" + precisionStep; int termCountT = 0, termCountC = 0; for (int i = 0; i < 50; i++) { long lower = (long)(rnd.NextDouble() * noDocs * distance) + startOffset; long upper = (long)(rnd.NextDouble() * noDocs * distance) + startOffset; if (lower > upper) { long a = lower; lower = upper; upper = a; } // test inclusive range System.Int64 tempAux = (long)lower; System.Int64 tempAux2 = (long)upper; NumericRangeQuery tq = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux, tempAux2, true, true); TermRangeQuery cq = new TermRangeQuery(field, NumericUtils.LongToPrefixCoded(lower), NumericUtils.LongToPrefixCoded(upper), true, true); TopDocs tTopDocs = searcher.Search(tq, 1); TopDocs cTopDocs = searcher.Search(cq, 1); Assert.AreEqual(cTopDocs.totalHits, tTopDocs.totalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal"); termCountT += tq.GetTotalNumberOfTerms(); termCountC += cq.GetTotalNumberOfTerms(); // test exclusive range System.Int64 tempAux3 = (long)lower; System.Int64 tempAux4 = (long)upper; tq = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux3, tempAux4, false, false); cq = new TermRangeQuery(field, NumericUtils.LongToPrefixCoded(lower), NumericUtils.LongToPrefixCoded(upper), false, false); tTopDocs = searcher.Search(tq, 1); cTopDocs = searcher.Search(cq, 1); Assert.AreEqual(cTopDocs.totalHits, tTopDocs.totalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal"); termCountT += tq.GetTotalNumberOfTerms(); termCountC += cq.GetTotalNumberOfTerms(); // test left exclusive range System.Int64 tempAux5 = (long)lower; System.Int64 tempAux6 = (long)upper; tq = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux5, tempAux6, false, true); cq = new TermRangeQuery(field, NumericUtils.LongToPrefixCoded(lower), NumericUtils.LongToPrefixCoded(upper), false, true); tTopDocs = searcher.Search(tq, 1); cTopDocs = searcher.Search(cq, 1); Assert.AreEqual(cTopDocs.totalHits, tTopDocs.totalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal"); termCountT += tq.GetTotalNumberOfTerms(); termCountC += cq.GetTotalNumberOfTerms(); // test right exclusive range System.Int64 tempAux7 = (long)lower; System.Int64 tempAux8 = (long)upper; tq = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux7, tempAux8, true, false); cq = new TermRangeQuery(field, NumericUtils.LongToPrefixCoded(lower), NumericUtils.LongToPrefixCoded(upper), true, false); tTopDocs = searcher.Search(tq, 1); cTopDocs = searcher.Search(cq, 1); Assert.AreEqual(cTopDocs.totalHits, tTopDocs.totalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal"); termCountT += tq.GetTotalNumberOfTerms(); termCountC += cq.GetTotalNumberOfTerms(); } if (precisionStep == System.Int32.MaxValue) { Assert.AreEqual(termCountT, termCountC, "Total number of terms should be equal for unlimited precStep"); } else { System.Console.Out.WriteLine("Average number of terms during random search on '" + field + "':"); System.Console.Out.WriteLine(" Trie query: " + (((double)termCountT) / (50 * 4))); System.Console.Out.WriteLine(" Classical query: " + (((double)termCountC) / (50 * 4))); } }