Example #1
0
        protected override IQueryNode PostProcessNode(IQueryNode node)
        {
            if (node is IFieldableNode fieldNode &&
                (node.Parent == null || !(node.Parent is IFieldableNode)))
            {
                QueryConfigHandler config = GetQueryConfigHandler();

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

                    if (fieldConfig != null)
                    {
                        float?boost = fieldConfig.Get(ConfigurationKeys.BOOST);

                        if (boost != null)
                        {
                            return(new BoostQueryNode(node, boost.Value));
                        }
                    }
                }
            }

            return(node);
        }
        protected override IQueryNode PostProcessNode(IQueryNode node)
        {
            if (node is TermRangeQueryNode)
            {
                TermRangeQueryNode termRangeNode = (TermRangeQueryNode)node;
                FieldQueryNode     upper         = (FieldQueryNode)termRangeNode.UpperBound;
                FieldQueryNode     lower         = (FieldQueryNode)termRangeNode.LowerBound;

                // LUCENENET specific - set to 0 (instead of null), since it doesn't correspond to any valid setting
                DateTools.Resolution dateRes = 0 /* = null*/;
                bool        inclusive        = false;
                CultureInfo locale           = GetQueryConfigHandler().Get(ConfigurationKeys.LOCALE);

                if (locale == null)
                {
                    locale = CultureInfo.CurrentCulture; //Locale.getDefault();
                }

                TimeZoneInfo timeZone = GetQueryConfigHandler().Get(ConfigurationKeys.TIMEZONE);

                if (timeZone == null)
                {
                    timeZone = TimeZoneInfo.Local; //TimeZone.getDefault();
                }

                string field    = termRangeNode.Field;
                string fieldStr = null;

                if (field != null)
                {
                    fieldStr = field.ToString();
                }

                FieldConfig fieldConfig = GetQueryConfigHandler()
                                          .GetFieldConfig(fieldStr);

                if (fieldConfig != null)
                {
                    dateRes = fieldConfig.Get(ConfigurationKeys.DATE_RESOLUTION);
                }

                if (termRangeNode.IsUpperInclusive)
                {
                    inclusive = true;
                }

                string part1 = lower.GetTextAsString();
                string part2 = upper.GetTextAsString();

                try
                {
                    string   shortDateFormat = locale.DateTimeFormat.ShortDatePattern;
                    DateTime d1;
                    DateTime d2 = DateTime.MaxValue; // We really don't care what we set this to, but we need something or the compiler will complain below

                    if (DateTime.TryParseExact(part1, shortDateFormat, locale, DateTimeStyles.None, out d1))
                    {
                        part1      = DateTools.DateToString(d1, dateRes);
                        lower.Text = new StringCharSequenceWrapper(part1);
                    }

                    if (DateTime.TryParseExact(part2, shortDateFormat, locale, DateTimeStyles.None, out d2))
                    {
                        if (inclusive)
                        {
                            // The user can only specify the date, not the time, so make sure
                            // the time is set to the latest possible time of that date to
                            // really
                            // include all documents:
                            //Calendar cal = Calendar.getInstance(timeZone, locale);
                            //cal.setTime(d2);
                            //cal.set(Calendar.HOUR_OF_DAY, 23);
                            //cal.set(Calendar.MINUTE, 59);
                            //cal.set(Calendar.SECOND, 59);
                            //cal.set(Calendar.MILLISECOND, 999);
                            //d2 = cal.getTime();

                            d2 = TimeZoneInfo.ConvertTime(d2, timeZone);
                            var cal = locale.Calendar;
                            d2 = cal.AddHours(d2, 23);
                            d2 = cal.AddMinutes(d2, 59);
                            d2 = cal.AddSeconds(d2, 59);
                            d2 = cal.AddMilliseconds(d2, 999);
                        }

                        part2      = DateTools.DateToString(d2, dateRes);
                        upper.Text = new StringCharSequenceWrapper(part2);
                    }
                }
#pragma warning disable 168
                catch (Exception e)
#pragma warning restore 168
                {
                    // do nothing
                }
            }

            return(node);
        }
        protected override IQueryNode PostProcessNode(IQueryNode node)
        {
            if (node is FieldQueryNode &&
                !(node.Parent is IRangeQueryNode))
            {
                QueryConfigHandler config = GetQueryConfigHandler();

                if (config != null)
                {
                    FieldQueryNode fieldNode   = (FieldQueryNode)node;
                    FieldConfig    fieldConfig = config.GetFieldConfig(fieldNode
                                                                       .GetFieldAsString());

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

                        if (numericConfig != null)
                        {
                            NumberFormat numberFormat = numericConfig.NumberFormat;
                            string       text         = fieldNode.GetTextAsString();
                            /*Number*/
                            object number = null;

                            if (text.Length > 0)
                            {
                                try
                                {
                                    number = numberFormat.Parse(text);
                                }
                                catch (FormatException e)
                                {
                                    throw new QueryNodeParseException(new MessageImpl(
                                                                          QueryParserMessages.COULD_NOT_PARSE_NUMBER, fieldNode
                                                                          .GetTextAsString(), numberFormat.GetType()
                                                                          .AssemblyQualifiedName), e);
                                }

                                switch (numericConfig.Type)
                                {
                                case FieldType.NumericType.LONG:
                                    number = Convert.ToInt64(number);
                                    break;

                                case FieldType.NumericType.INT:
                                    number = Convert.ToInt32(number);
                                    break;

                                case FieldType.NumericType.DOUBLE:
                                    number = Convert.ToDouble(number);
                                    break;

                                case FieldType.NumericType.FLOAT:
                                    number = Convert.ToSingle(number);
                                    break;
                                }
                            }
                            else
                            {
                                throw new QueryNodeParseException(new MessageImpl(
                                                                      QueryParserMessages.NUMERIC_CANNOT_BE_EMPTY, fieldNode.GetFieldAsString()));
                            }

                            NumericQueryNode lowerNode = new NumericQueryNode(fieldNode
                                                                              .Field, number, numberFormat);
                            NumericQueryNode upperNode = new NumericQueryNode(fieldNode
                                                                              .Field, number, numberFormat);

                            return(new NumericRangeQueryNode(lowerNode, upperNode, true, true,
                                                             numericConfig));
                        }
                    }
                }
            }

            return(node);
        }
        protected override IQueryNode PostProcessNode(IQueryNode node)
        {
            if (node is TermRangeQueryNode)
            {
                QueryConfigHandler config = GetQueryConfigHandler();

                if (config != null)
                {
                    TermRangeQueryNode termRangeNode = (TermRangeQueryNode)node;
                    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;
                            /*Number*/
                            object lowerNumber = null, upperNumber = null;

                            if (lowerText.Length > 0)
                            {
                                try
                                {
                                    lowerNumber = numberFormat.Parse(lowerText);
                                }
                                catch (FormatException e)
                                {
                                    throw new QueryNodeParseException(new Message(
                                                                          QueryParserMessages.COULD_NOT_PARSE_NUMBER, lower
                                                                          .GetTextAsString(), numberFormat.ToString()), e);
                                }
                            }

                            if (upperText.Length > 0)
                            {
                                try
                                {
                                    upperNumber = numberFormat.Parse(upperText);
                                }
                                catch (FormatException e)
                                {
                                    throw new QueryNodeParseException(new Message(
                                                                          QueryParserMessages.COULD_NOT_PARSE_NUMBER, upper
                                                                          .GetTextAsString(), numberFormat.ToString()), e);
                                }
                            }

                            switch (numericConfig.Type)
                            {
                            case NumericType.INT64:
                                if (upperNumber != null)
                                {
                                    upperNumber = Convert.ToInt64(upperNumber);                          // LUCENENET TODO: Find a way to pass culture
                                }
                                if (lowerNumber != null)
                                {
                                    lowerNumber = Convert.ToInt64(lowerNumber);
                                }
                                break;

                            case NumericType.INT32:
                                if (upperNumber != null)
                                {
                                    upperNumber = Convert.ToInt32(upperNumber);                          // LUCENENET TODO: Find a way to pass culture
                                }
                                if (lowerNumber != null)
                                {
                                    lowerNumber = Convert.ToInt32(lowerNumber);
                                }
                                break;

                            case NumericType.DOUBLE:
                                if (upperNumber != null)
                                {
                                    upperNumber = Convert.ToDouble(upperNumber);                          // LUCENENET TODO: Find a way to pass culture
                                }
                                if (lowerNumber != null)
                                {
                                    lowerNumber = Convert.ToDouble(lowerNumber);
                                }
                                break;

                            case NumericType.SINGLE:
                                if (upperNumber != null)
                                {
                                    upperNumber = Convert.ToSingle(upperNumber);                          // LUCENENET TODO: Find a way to pass culture
                                }
                                if (lowerNumber != null)
                                {
                                    lowerNumber = Convert.ToSingle(lowerNumber);
                                }
                                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);
        }
Example #5
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);
        }
        protected override IQueryNode PostProcessNode(IQueryNode node)
        {
            if (node is FieldQueryNode fieldNode &&
                !(node.Parent is IRangeQueryNode))
            {
                QueryConfigHandler config = GetQueryConfigHandler();

                if (config != null)
                {
                    FieldConfig fieldConfig = config.GetFieldConfig(fieldNode
                                                                    .GetFieldAsString());

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

                        if (numericConfig != null)
                        {
                            NumberFormat numberFormat = numericConfig.NumberFormat;
                            string       text         = fieldNode.GetTextAsString();
                            Number       number; // LUCENENET: IDE0059: Remove unnecessary value assignment

                            if (text.Length > 0)
                            {
                                try
                                {
                                    number = numberFormat.Parse(text);
                                }
                                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, fieldNode
                                                                          .GetTextAsString(), numberFormat.GetType()
                                                                          .AssemblyQualifiedName), e);
                                }

                                switch (numericConfig.Type)
                                {
                                case NumericType.INT64:
                                    number = J2N.Numerics.Int64.GetInstance(number.ToInt64());
                                    break;

                                case NumericType.INT32:
                                    number = J2N.Numerics.Int32.GetInstance(number.ToInt32());
                                    break;

                                case NumericType.DOUBLE:
                                    number = J2N.Numerics.Double.GetInstance(number.ToDouble());
                                    break;

                                case NumericType.SINGLE:
                                    number = J2N.Numerics.Single.GetInstance(number.ToSingle());
                                    break;
                                }
                            }
                            else
                            {
                                // LUCENENET: Factored out NLS/Message/IMessage so end users can optionally utilize the built-in .NET localization.
                                throw new QueryNodeParseException(string.Format(
                                                                      QueryParserMessages.NUMERIC_CANNOT_BE_EMPTY, fieldNode.GetFieldAsString()));
                            }

                            NumericQueryNode lowerNode = new NumericQueryNode(fieldNode
                                                                              .Field, number, numberFormat);
                            NumericQueryNode upperNode = new NumericQueryNode(fieldNode
                                                                              .Field, number, numberFormat);

                            return(new NumericRangeQueryNode(lowerNode, upperNode, true, true,
                                                             numericConfig));
                        }
                    }
                }
            }

            return(node);
        }
        protected override IQueryNode PostProcessNode(IQueryNode node)
        {
            if (node is TermRangeQueryNode)
            {
                TermRangeQueryNode termRangeNode = (TermRangeQueryNode)node;
                FieldQueryNode     upper         = (FieldQueryNode)termRangeNode.UpperBound;
                FieldQueryNode     lower         = (FieldQueryNode)termRangeNode.LowerBound;

                // LUCENENET TODO: Add a NOT_SET value so we have a logical default?
                DateTools.Resolution dateRes = DateTools.Resolution.MINUTE /* = null*/;
                bool        inclusive        = false;
                CultureInfo locale           = GetQueryConfigHandler().Get(ConfigurationKeys.LOCALE);

                if (locale == null)
                {
                    locale = CultureInfo.CurrentCulture; //Locale.getDefault();
                }

                TimeZoneInfo timeZone = GetQueryConfigHandler().Get(ConfigurationKeys.TIMEZONE);

                if (timeZone == null)
                {
                    timeZone = TimeZoneInfo.Local; //TimeZone.getDefault();
                }

                string field    = termRangeNode.Field;
                string fieldStr = null;

                if (field != null)
                {
                    fieldStr = field.ToString();
                }

                FieldConfig fieldConfig = GetQueryConfigHandler()
                                          .GetFieldConfig(fieldStr);

                if (fieldConfig != null)
                {
                    dateRes = fieldConfig.Get(ConfigurationKeys.DATE_RESOLUTION);
                }

                if (termRangeNode.IsUpperInclusive)
                {
                    inclusive = true;
                }

                string part1 = lower.GetTextAsString();
                string part2 = upper.GetTextAsString();

                try
                {
                    //DateFormat df = DateFormat.GetDateInstance(DateFormat.SHORT, locale);
                    //df.setLenient(true);

                    if (part1.Length > 0)
                    {
                        //DateTime d1 = df.parse(part1);
                        DateTime d1 = DateTime.Parse(part1, locale);
                        part1      = DateTools.DateToString(d1, dateRes);
                        lower.Text = new StringCharSequenceWrapper(part1);
                    }

                    if (part2.Length > 0)
                    {
                        //DateTime d2 = df.parse(part2);
                        DateTime d2 = DateTime.Parse(part2, locale);
                        if (inclusive)
                        {
                            // The user can only specify the date, not the time, so make sure
                            // the time is set to the latest possible time of that date to
                            // really
                            // include all documents:
                            //Calendar cal = Calendar.getInstance(timeZone, locale);
                            //cal.setTime(d2);
                            //cal.set(Calendar.HOUR_OF_DAY, 23);
                            //cal.set(Calendar.MINUTE, 59);
                            //cal.set(Calendar.SECOND, 59);
                            //cal.set(Calendar.MILLISECOND, 999);
                            //d2 = cal.getTime();

                            var cal = locale.Calendar;
                            d2 = cal.AddHours(d2, 23);
                            d2 = cal.AddMinutes(d2, 59);
                            d2 = cal.AddSeconds(d2, 59);
                            d2 = cal.AddMilliseconds(d2, 999);
                        }

                        part2      = DateTools.DateToString(d2, dateRes);
                        upper.Text = new StringCharSequenceWrapper(part2);
                    }
                }
#pragma warning disable 168
                catch (Exception e)
#pragma warning restore 168
                {
                    // do nothing
                }
            }

            return(node);
        }