Exemplo n.º 1
0
        public virtual Query Build(IQueryNode queryNode)
        {
            NumericRangeQueryNode numericRangeNode = (NumericRangeQueryNode)queryNode;

            NumericQueryNode lowerNumericNode = (NumericQueryNode)numericRangeNode.LowerBound;
            NumericQueryNode upperNumericNode = (NumericQueryNode)numericRangeNode.UpperBound;

            J2N.Numerics.Number lowerNumber = lowerNumericNode.Value;
            J2N.Numerics.Number upperNumber = upperNumericNode.Value;

            NumericConfig numericConfig = numericRangeNode.NumericConfig;
            NumericType   numberType    = numericConfig.Type;
            string        field         = StringUtils.ToString(numericRangeNode.Field);
            bool          minInclusive  = numericRangeNode.IsLowerInclusive;
            bool          maxInclusive  = numericRangeNode.IsUpperInclusive;
            int           precisionStep = numericConfig.PrecisionStep;

            switch (numberType)
            {
            case NumericType.INT64:
                return(NumericRangeQuery.NewInt64Range(field, precisionStep,
                                                       lowerNumber?.ToInt64(), upperNumber?.ToInt64(), minInclusive, maxInclusive));

            case NumericType.INT32:
                return(NumericRangeQuery.NewInt32Range(field, precisionStep,
                                                       lowerNumber?.ToInt32(), upperNumber?.ToInt32(), minInclusive,
                                                       maxInclusive));

            case NumericType.SINGLE:
                return(NumericRangeQuery.NewSingleRange(field, precisionStep,
                                                        lowerNumber?.ToSingle(), upperNumber?.ToSingle(), minInclusive,
                                                        maxInclusive));

            case NumericType.DOUBLE:
                return(NumericRangeQuery.NewDoubleRange(field, precisionStep,
                                                        lowerNumber?.ToDouble(), upperNumber?.ToDouble(), minInclusive,
                                                        maxInclusive));

            default:
                // LUCENENET: Factored out NLS/Message/IMessage so end users can optionally utilize the built-in .NET localization.
                throw new QueryNodeException(string.Format(
                                                 QueryParserMessages.UNSUPPORTED_NUMERIC_DATA_TYPE, numberType));
            }
        }
Exemplo n.º 2
0
        protected override IQueryNode PostProcessNode(IQueryNode node)
        {
            if (node is TermRangeQueryNode termRangeNode)
            {
                QueryConfigHandler config = GetQueryConfigHandler();

                if (config != null)
                {
                    FieldConfig fieldConfig = config.GetFieldConfig(StringUtils
                                                                    .ToString(termRangeNode.Field));

                    if (fieldConfig != null)
                    {
                        NumericConfig numericConfig = fieldConfig
                                                      .Get(ConfigurationKeys.NUMERIC_CONFIG);

                        if (numericConfig != null)
                        {
                            FieldQueryNode lower = (FieldQueryNode)termRangeNode.LowerBound;
                            FieldQueryNode upper = (FieldQueryNode)termRangeNode.UpperBound;

                            string              lowerText = lower.GetTextAsString();
                            string              upperText = upper.GetTextAsString();
                            NumberFormat        numberFormat = numericConfig.NumberFormat;
                            J2N.Numerics.Number lowerNumber = null, upperNumber = null;

                            if (lowerText.Length > 0)
                            {
                                try
                                {
                                    lowerNumber = numberFormat.Parse(lowerText);
                                }
                                catch (FormatException e) // LUCENENET: In .NET we are expecting the framework to throw FormatException, not ParseException
                                {
                                    // LUCENENET: Factored out NLS/Message/IMessage so end users can optionally utilize the built-in .NET localization.
                                    throw new QueryNodeParseException(string.Format(
                                                                          QueryParserMessages.COULD_NOT_PARSE_NUMBER, lower
                                                                          .GetTextAsString(), numberFormat.ToString()), e);
                                }
                            }

                            if (upperText.Length > 0)
                            {
                                try
                                {
                                    upperNumber = numberFormat.Parse(upperText);
                                }
                                catch (FormatException e) // LUCENENET: In .NET we are expecting the framework to throw FormatException, not ParseException
                                {
                                    // LUCENENET: Factored out NLS/Message/IMessage so end users can optionally utilize the built-in .NET localization.
                                    throw new QueryNodeParseException(string.Format(
                                                                          QueryParserMessages.COULD_NOT_PARSE_NUMBER, upper
                                                                          .GetTextAsString(), numberFormat.ToString()), e);
                                }
                            }

                            switch (numericConfig.Type)
                            {
                            case NumericType.INT64:
                                if (upperNumber != null)
                                {
                                    upperNumber = J2N.Numerics.Int64.GetInstance(upperNumber.ToInt64());
                                }
                                if (lowerNumber != null)
                                {
                                    lowerNumber = J2N.Numerics.Int64.GetInstance(lowerNumber.ToInt64());
                                }
                                break;

                            case NumericType.INT32:
                                if (upperNumber != null)
                                {
                                    upperNumber = J2N.Numerics.Int32.GetInstance(upperNumber.ToInt32());
                                }
                                if (lowerNumber != null)
                                {
                                    lowerNumber = J2N.Numerics.Int32.GetInstance(lowerNumber.ToInt32());
                                }
                                break;

                            case NumericType.DOUBLE:
                                if (upperNumber != null)
                                {
                                    upperNumber = J2N.Numerics.Double.GetInstance(upperNumber.ToDouble());
                                }
                                if (lowerNumber != null)
                                {
                                    lowerNumber = J2N.Numerics.Double.GetInstance(lowerNumber.ToDouble());
                                }
                                break;

                            case NumericType.SINGLE:
                                if (upperNumber != null)
                                {
                                    upperNumber = J2N.Numerics.Single.GetInstance(upperNumber.ToSingle());
                                }
                                if (lowerNumber != null)
                                {
                                    lowerNumber = J2N.Numerics.Single.GetInstance(lowerNumber.ToSingle());
                                }
                                break;
                            }

                            NumericQueryNode lowerNode = new NumericQueryNode(
                                termRangeNode.Field, lowerNumber, numberFormat);
                            NumericQueryNode upperNode = new NumericQueryNode(
                                termRangeNode.Field, upperNumber, numberFormat);

                            bool lowerInclusive = termRangeNode.IsLowerInclusive;
                            bool upperInclusive = termRangeNode.IsUpperInclusive;

                            return(new NumericRangeQueryNode(lowerNode, upperNode,
                                                             lowerInclusive, upperInclusive, numericConfig));
                        }
                    }
                }
            }

            return(node);
        }