Exemplo n.º 1
0
        private static string GetFilterValue(string value, bool isBooleanField)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(string.Empty);
            }

            if (isBooleanField)
            {
                return(bool.Parse(value).ToStringInvariant());
            }

            var dateValue = ConvertToDateTimeTicks(value);

            if (dateValue != null)
            {
                return(ConvertLongToString(dateValue.Value));
            }

            var dobuleValue = ConvertToDouble(value);

            if (dobuleValue.HasValue)
            {
                var longValue = NumericUtils.DoubleToSortableInt64(dobuleValue.Value);
                return(ConvertLongToString(longValue));
            }
            return(value);
        }
Exemplo n.º 2
0
        public override bool EqualsSameType(object other)
        {
            MutableValueDouble b = (MutableValueDouble)other;

            // LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
            return(NumericUtils.DoubleToSortableInt64(Value) == NumericUtils.DoubleToSortableInt64(b.Value) &&
                   Exists == b.Exists);
        }
Exemplo n.º 3
0
            /// <summary>
            /// Makes a TermQuery for the given MappedField and expression.
            /// </summary>
            /// <param name="field">
            /// The MappedField to create the query for.
            /// </param>
            /// <param name="term">
            /// The constant to match in the query.
            /// </param>
            /// <returns>
            /// An instance of TermQuery.
            /// </returns>
            private static TermQuery MakeTermQuery(MappedField field, Expression term)
            {
                Debug.Assert(null != field, "The mapped field must not be null.");
                Debug.Assert(null != term, "The term must not be null.");

                Term tm;

                switch (field.Type)
                {
                case MappedField.FieldType.Float:
                {
                    BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT64);
                    long     l     = NumericUtils.DoubleToSortableInt64(field.GetValueFromExpression <float>(term));
                    NumericUtils.Int64ToPrefixCoded(l, 0, bytes);
                    tm = new Term(field.Name, bytes);
                    break;
                }

                case MappedField.FieldType.Double:
                {
                    BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT64);
                    long     l     = NumericUtils.DoubleToSortableInt64(field.GetValueFromExpression <double>(term));
                    NumericUtils.Int64ToPrefixCoded(l, 0, bytes);
                    tm = new Term(field.Name, bytes);
                    break;
                }

                case MappedField.FieldType.Short:
                case MappedField.FieldType.Int:
                {
                    BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT32);
                    NumericUtils.Int32ToPrefixCoded(field.GetValueFromExpression <int>(term), 0, bytes);
                    tm = new Term(field.Name, bytes);
                    break;
                }

                case MappedField.FieldType.Long:
                {
                    BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT64);
                    NumericUtils.Int64ToPrefixCoded(field.GetValueFromExpression <long>(term), 0, bytes);
                    tm = new Term(field.Name, bytes);
                    break;
                }

                case MappedField.FieldType.String:
                {
                    tm = new Term(field.Name, field.GetValueFromExpression <string>(term));
                    break;
                }

                default:
                    throw new InvalidOperationException(String.Format(
                                                            "Cannot make a TermQuery for field '{0}' of type {1}.",
                                                            field.Name, field.Type));
                }

                return(new TermQuery(tm));
            }
Exemplo n.º 4
0
            public override DocIdSet GetDocIdSet(AtomicReaderContext context, IBits acceptDocs)
            {
                // 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 = (double)lowerVal;
                    if (!includeUpper && f > 0.0 && double.IsInfinity(f))
                    {
                        return(null);
                    }
                    long i = NumericUtils.DoubleToSortableInt64(f);
                    inclusiveLowerPoint = NumericUtils.SortableInt64ToDouble(includeLower ? i : (i + 1L));
                }
                else
                {
                    inclusiveLowerPoint = double.NegativeInfinity;
                }
                if (upperVal != null)
                {
                    double f = (double)upperVal;
                    if (!includeUpper && f < 0.0 && double.IsInfinity(f))
                    {
                        return(null);
                    }
                    long i = NumericUtils.DoubleToSortableInt64(f);
                    inclusiveUpperPoint = NumericUtils.SortableInt64ToDouble(includeUpper ? i : (i - 1L));
                }
                else
                {
                    inclusiveUpperPoint = double.PositiveInfinity;
                }

                if (inclusiveLowerPoint > inclusiveUpperPoint)
                {
                    return(null);
                }

                FieldCache.Doubles values = FieldCache.DEFAULT.GetDoubles(context.AtomicReader, field, (FieldCache.IDoubleParser)parser, false);

                // we only request the usage of termDocs, if the range contains 0
                return(new FieldCacheDocIdSet(context.Reader.MaxDoc, acceptDocs, (doc) =>
                {
                    double value = values.Get(doc);
                    return value >= inclusiveLowerPoint && value <= inclusiveUpperPoint;
                }));
            }
Exemplo n.º 5
0
        protected override Query GetFieldQuery(string field, string queryText, bool quoted)
        {
            try
            {
                string fieldTypeName = PuckCache.TypeFields[TypeName][field];
                if (fieldTypeName.Equals(typeof(int).AssemblyQualifiedName))
                {
                    BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT32);
                    NumericUtils.Int32ToPrefixCoded(int.Parse(queryText), 0, bytes);
                    return(new TermQuery(new Term(field, bytes)));
                }
                else if (fieldTypeName.Equals(typeof(long).AssemblyQualifiedName))
                {
                    BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT64);
                    NumericUtils.Int64ToPrefixCoded(long.Parse(queryText), 0, bytes);
                    return(new TermQuery(new Term(field, bytes)));
                }
                else if (fieldTypeName.Equals(typeof(float).AssemblyQualifiedName))
                {
                    BytesRef bytes    = new BytesRef(NumericUtils.BUF_SIZE_INT32);
                    int      intFloat = NumericUtils.SingleToSortableInt32(float.Parse(queryText));
                    NumericUtils.Int32ToPrefixCoded(intFloat, 0, bytes);
                    return(new TermQuery(new Term(field, bytes)));
                }
                else if (fieldTypeName.Equals(typeof(double).AssemblyQualifiedName))
                {
                    BytesRef bytes      = new BytesRef(NumericUtils.BUF_SIZE_INT64);
                    long     longDouble = NumericUtils.DoubleToSortableInt64(double.Parse(queryText));
                    NumericUtils.Int64ToPrefixCoded(longDouble, 0, bytes);
                    return(new TermQuery(new Term(field, bytes)));
                }
            }
            catch (Exception ex)
            {
            }

            return(base.GetFieldQuery(field, queryText, quoted));
        }
Exemplo n.º 6
0
            internal NumericRangeTermsEnum(NumericRangeQuery <T> outerInstance, TermsEnum tenum)
                : base(tenum)
            {
                this.outerInstance = outerInstance;
                switch (this.outerInstance.dataType)
                {
                case NumericType.INT64:
                case NumericType.DOUBLE:
                {
                    // lower
                    long minBound;
                    if (this.outerInstance.dataType == NumericType.INT64)
                    {
                        minBound = (this.outerInstance.min == null) ? long.MinValue : Convert.ToInt64(this.outerInstance.min.Value);
                    }
                    else
                    {
                        Debug.Assert(this.outerInstance.dataType == NumericType.DOUBLE);
                        minBound = (this.outerInstance.min == null) ? INT64_NEGATIVE_INFINITY : NumericUtils.DoubleToSortableInt64(Convert.ToDouble(this.outerInstance.min.Value));
                    }
                    if (!this.outerInstance.minInclusive && this.outerInstance.min != null)
                    {
                        if (minBound == long.MaxValue)
                        {
                            break;
                        }
                        minBound++;
                    }

                    // upper
                    long maxBound;
                    if (this.outerInstance.dataType == NumericType.INT64)
                    {
                        maxBound = (this.outerInstance.max == null) ? long.MaxValue : Convert.ToInt64(this.outerInstance.max);
                    }
                    else
                    {
                        Debug.Assert(this.outerInstance.dataType == NumericType.DOUBLE);
                        maxBound = (this.outerInstance.max == null) ? INT64_POSITIVE_INFINITY : NumericUtils.DoubleToSortableInt64(Convert.ToDouble(this.outerInstance.max));
                    }
                    if (!this.outerInstance.maxInclusive && this.outerInstance.max != null)
                    {
                        if (maxBound == long.MinValue)
                        {
                            break;
                        }
                        maxBound--;
                    }

                    NumericUtils.SplitInt64Range(new Int64RangeBuilderAnonymousInnerClassHelper(this), this.outerInstance.precisionStep, minBound, maxBound);
                    break;
                }

                case NumericType.INT32:
                case NumericType.SINGLE:
                {
                    // lower
                    int minBound;
                    if (this.outerInstance.dataType == NumericType.INT32)
                    {
                        minBound = (this.outerInstance.min == null) ? int.MinValue : Convert.ToInt32(this.outerInstance.min);
                    }
                    else
                    {
                        Debug.Assert(this.outerInstance.dataType == NumericType.SINGLE);
                        minBound = (this.outerInstance.min == null) ? INT32_NEGATIVE_INFINITY : NumericUtils.SingleToSortableInt32(Convert.ToSingle(this.outerInstance.min));
                    }
                    if (!this.outerInstance.minInclusive && this.outerInstance.min != null)
                    {
                        if (minBound == int.MaxValue)
                        {
                            break;
                        }
                        minBound++;
                    }

                    // upper
                    int maxBound;
                    if (this.outerInstance.dataType == NumericType.INT32)
                    {
                        maxBound = (this.outerInstance.max == null) ? int.MaxValue : Convert.ToInt32(this.outerInstance.max);
                    }
                    else
                    {
                        Debug.Assert(this.outerInstance.dataType == NumericType.SINGLE);
                        maxBound = (this.outerInstance.max == null) ? INT32_POSITIVE_INFINITY : NumericUtils.SingleToSortableInt32(Convert.ToSingle(this.outerInstance.max));
                    }
                    if (!this.outerInstance.maxInclusive && this.outerInstance.max != null)
                    {
                        if (maxBound == int.MinValue)
                        {
                            break;
                        }
                        maxBound--;
                    }

                    NumericUtils.SplitInt32Range(new Int32RangeBuilderAnonymousInnerClassHelper(this), this.outerInstance.precisionStep, minBound, maxBound);
                    break;
                }

                default:
                    // should never happen
                    throw new System.ArgumentException("Invalid NumericType");
                }

                termComp = Comparer;
            }
Exemplo n.º 7
0
 /// <summary>
 /// Initializes the token stream with the supplied <see cref="double"/> value. </summary>
 /// <param name="value"> the value, for which this <see cref="TokenStream"/> should enumerate tokens. </param>
 /// <returns> this instance, because of this you can use it the following way:
 /// <code>new Field(name, new NumericTokenStream(precisionStep).SetDoubleValue(value))</code> </returns>
 public NumericTokenStream SetDoubleValue(double value)
 {
     numericAtt.Init(NumericUtils.DoubleToSortableInt64(value), valSize = 64, precisionStep, -precisionStep);
     return(this);
 }
Exemplo n.º 8
0
 /// <summary>
 /// NOTE: This was toLongRange() in Lucene
 /// </summary>
 internal Int64Range ToInt64Range()
 {
     return(new Int64Range(Label, NumericUtils.DoubleToSortableInt64(minIncl), true, NumericUtils.DoubleToSortableInt64(maxIncl), true));
 }
Exemplo n.º 9
0
        private void Count(ValueSource valueSource, IEnumerable <MatchingDocs> matchingDocs)
        {
            DoubleRange[] ranges = (DoubleRange[])this.m_ranges;

            Int64Range[] longRanges = new Int64Range[ranges.Length];
            for (int i = 0; i < ranges.Length; i++)
            {
                DoubleRange range = ranges[i];
                longRanges[i] = new Int64Range(range.Label, NumericUtils.DoubleToSortableInt64(range.minIncl), true, NumericUtils.DoubleToSortableInt64(range.maxIncl), true);
            }

            Int64RangeCounter counter = new Int64RangeCounter(longRanges);

            int missingCount = 0;

            foreach (MatchingDocs hits in matchingDocs)
            {
                FunctionValues fv = valueSource.GetValues(new Dictionary <string, object>(), hits.Context);

                m_totCount += hits.TotalHits;
                IBits bits;
                if (m_fastMatchFilter != null)
                {
                    DocIdSet dis = m_fastMatchFilter.GetDocIdSet(hits.Context, null);
                    if (dis == null)
                    {
                        // No documents match
                        continue;
                    }
                    bits = dis.Bits;
                    if (bits == null)
                    {
                        throw new System.ArgumentException("fastMatchFilter does not implement DocIdSet.bits");
                    }
                }
                else
                {
                    bits = null;
                }

                DocIdSetIterator docs = hits.Bits.GetIterator();

                int doc;
                while ((doc = docs.NextDoc()) != DocIdSetIterator.NO_MORE_DOCS)
                {
                    if (bits != null && bits.Get(doc) == false)
                    {
                        doc++;
                        continue;
                    }
                    // Skip missing docs:
                    if (fv.Exists(doc))
                    {
                        counter.Add(NumericUtils.DoubleToSortableInt64(fv.DoubleVal(doc)));
                    }
                    else
                    {
                        missingCount++;
                    }
                }
            }

            missingCount += counter.FillCounts(m_counts);
            m_totCount   -= missingCount;
        }
Exemplo n.º 10
0
        protected override Query GetFieldQuery(string field, string queryText, bool quoted)
        {
            try
            {
                if ((field == "PuckGeoK" && !PuckCache.TypeFields[TypeName].ContainsKey("PuckGeoK")) || (field == "PuckGeoM" && !PuckCache.TypeFields[TypeName].ContainsKey("PuckGeoM")))
                {
                    var    parameters = queryText.Split(',', StringSplitOptions.RemoveEmptyEntries);
                    double longitude;
                    double latitude;
                    int    distance;
                    if (parameters.Length == 5 && double.TryParse(parameters[1].Trim(), out longitude) && double.TryParse(parameters[2].Trim(), out latitude) && int.TryParse(parameters[3].Trim(), out distance))
                    {
                        var    qh = new QueryHelper <T>();
                        double radius;

                        if (field == "PuckGeoK")
                        {
                            radius = DistanceUtils.EARTH_MEAN_RADIUS_KM;
                        }
                        else
                        {
                            radius = DistanceUtils.EARTH_MEAN_RADIUS_MI;
                        }

                        var distDEG = DistanceUtils.Dist2Degrees(distance, radius);
                        qh.GeoFilter(parameters[0].Trim(), longitude, latitude, distance);
                        this.filter = qh.GetFilter();
                        if (parameters[4] == "asc")
                        {
                            qh.SortByDistanceFromPoint(parameters[0].Trim(), longitude, latitude, desc: false);
                            sort = qh.GetSort();
                        }
                        else if (parameters[4] == "desc")
                        {
                            qh.SortByDistanceFromPoint(parameters[0].Trim(), longitude, latitude, desc: true);
                            sort = qh.GetSort();
                        }
                    }
                }
                else
                {
                    Type fieldType = null;
                    if (FieldTypeMappings != null)
                    {
                        if (FieldTypeMappings.TryGetValue(field, out fieldType))
                        {
                        }
                        else
                        {
                            PuckCache.TypeFields[TypeName]?.TryGetValue(field, out fieldType);
                        }
                    }
                    else
                    {
                        PuckCache.TypeFields[TypeName]?.TryGetValue(field, out fieldType);
                    }
                    if (fieldType != null)
                    {
                        if (fieldType.Equals(typeof(int)) || fieldType.Equals(typeof(int?)))
                        {
                            BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT32);
                            NumericUtils.Int32ToPrefixCoded(int.Parse(queryText), 0, bytes);
                            return(new TermQuery(new Term(field, bytes)));
                        }
                        else if (fieldType.Equals(typeof(long)) || fieldType.Equals(typeof(long?)))
                        {
                            BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT64);
                            NumericUtils.Int64ToPrefixCoded(long.Parse(queryText), 0, bytes);
                            return(new TermQuery(new Term(field, bytes)));
                        }
                        else if (fieldType.Equals(typeof(float)) || fieldType.Equals(typeof(float?)))
                        {
                            BytesRef bytes    = new BytesRef(NumericUtils.BUF_SIZE_INT32);
                            int      intFloat = NumericUtils.SingleToSortableInt32(float.Parse(queryText));
                            NumericUtils.Int32ToPrefixCoded(intFloat, 0, bytes);
                            return(new TermQuery(new Term(field, bytes)));
                        }
                        else if (fieldType.Equals(typeof(double)) || fieldType.Equals(typeof(double?)))
                        {
                            BytesRef bytes      = new BytesRef(NumericUtils.BUF_SIZE_INT64);
                            long     longDouble = NumericUtils.DoubleToSortableInt64(double.Parse(queryText));
                            NumericUtils.Int64ToPrefixCoded(longDouble, 0, bytes);
                            return(new TermQuery(new Term(field, bytes)));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return(base.GetFieldQuery(field, queryText, quoted));
        }