예제 #1
0
        public virtual Query GetQuery(XmlElement e)
        {
            string field          = DOMUtils.GetAttributeWithInheritanceOrFail(e, "fieldName");
            string lowerTerm      = DOMUtils.GetAttributeOrFail(e, "lowerTerm");
            string upperTerm      = DOMUtils.GetAttributeOrFail(e, "upperTerm");
            bool   lowerInclusive = DOMUtils.GetAttribute(e, "includeLower", true);
            bool   upperInclusive = DOMUtils.GetAttribute(e, "includeUpper", true);
            int    precisionStep  = DOMUtils.GetAttribute(e, "precisionStep", NumericUtils.PRECISION_STEP_DEFAULT);

            string type = DOMUtils.GetAttribute(e, "type", "int");

            try
            {
                Query filter;
                if (type.Equals("int", StringComparison.OrdinalIgnoreCase))
                {
                    filter = NumericRangeQuery.NewInt32Range(field, precisionStep,
                                                             J2N.Numerics.Int32.Parse(lowerTerm, NumberFormatInfo.InvariantInfo),
                                                             J2N.Numerics.Int32.Parse(upperTerm, NumberFormatInfo.InvariantInfo),
                                                             lowerInclusive,
                                                             upperInclusive);
                }
                else if (type.Equals("long", StringComparison.OrdinalIgnoreCase))
                {
                    filter = NumericRangeQuery.NewInt64Range(field, precisionStep,
                                                             J2N.Numerics.Int64.Parse(lowerTerm, NumberFormatInfo.InvariantInfo),
                                                             J2N.Numerics.Int64.Parse(upperTerm, NumberFormatInfo.InvariantInfo),
                                                             lowerInclusive,
                                                             upperInclusive);
                }
                else if (type.Equals("double", StringComparison.OrdinalIgnoreCase))
                {
                    filter = NumericRangeQuery.NewDoubleRange(field, precisionStep,
                                                              J2N.Numerics.Double.Parse(lowerTerm, NumberFormatInfo.InvariantInfo),
                                                              J2N.Numerics.Double.Parse(upperTerm, NumberFormatInfo.InvariantInfo),
                                                              lowerInclusive,
                                                              upperInclusive);
                }
                else if (type.Equals("float", StringComparison.OrdinalIgnoreCase))
                {
                    filter = NumericRangeQuery.NewSingleRange(field, precisionStep,
                                                              J2N.Numerics.Single.Parse(lowerTerm, NumberFormatInfo.InvariantInfo),
                                                              J2N.Numerics.Single.Parse(upperTerm, NumberFormatInfo.InvariantInfo),
                                                              lowerInclusive,
                                                              upperInclusive);
                }
                else
                {
                    throw new ParserException("type attribute must be one of: [long, int, double, float]");
                }
                return(filter);
            }
            catch (Exception nfe) when(nfe.IsNumberFormatException())
            {
                throw new ParserException("Could not parse lowerTerm or upperTerm into a number", nfe);
            }
        }
예제 #2
0
        public virtual Filter GetFilter(XmlElement e)
        {
            string field          = DOMUtils.GetAttributeWithInheritanceOrFail(e, "fieldName");
            string lowerTerm      = DOMUtils.GetAttributeOrFail(e, "lowerTerm");
            string upperTerm      = DOMUtils.GetAttributeOrFail(e, "upperTerm");
            bool   lowerInclusive = DOMUtils.GetAttribute(e, "includeLower", true);
            bool   upperInclusive = DOMUtils.GetAttribute(e, "includeUpper", true);
            int    precisionStep  = DOMUtils.GetAttribute(e, "precisionStep", NumericUtils.PRECISION_STEP_DEFAULT);

            string type = DOMUtils.GetAttribute(e, "type", "int");

            try
            {
                Filter filter;
                if (type.Equals("int", StringComparison.OrdinalIgnoreCase))
                {
                    filter = NumericRangeFilter.NewInt32Range(field, precisionStep, Convert
                                                              .ToInt32(lowerTerm, CultureInfo.InvariantCulture), Convert.ToInt32(upperTerm, CultureInfo.InvariantCulture), lowerInclusive,
                                                              upperInclusive);
                }
                else if (type.Equals("long", StringComparison.OrdinalIgnoreCase))
                {
                    filter = NumericRangeFilter.NewInt64Range(field, precisionStep, Convert
                                                              .ToInt64(lowerTerm, CultureInfo.InvariantCulture), Convert.ToInt64(upperTerm, CultureInfo.InvariantCulture), lowerInclusive,
                                                              upperInclusive);
                }
                else if (type.Equals("double", StringComparison.OrdinalIgnoreCase))
                {
                    filter = NumericRangeFilter.NewDoubleRange(field, precisionStep, Convert
                                                               .ToDouble(lowerTerm, CultureInfo.InvariantCulture), Convert.ToDouble(upperTerm, CultureInfo.InvariantCulture), lowerInclusive,
                                                               upperInclusive);
                }
                else if (type.Equals("float", StringComparison.OrdinalIgnoreCase))
                {
                    filter = NumericRangeFilter.NewSingleRange(field, precisionStep, Convert
                                                               .ToSingle(lowerTerm, CultureInfo.InvariantCulture), Convert.ToSingle(upperTerm, CultureInfo.InvariantCulture), lowerInclusive,
                                                               upperInclusive);
                }
                else
                {
                    throw new ParserException("type attribute must be one of: [long, int, double, float]");
                }
                return(filter);
            }
            catch (FormatException nfe)
            {
                if (strictMode)
                {
                    throw new ParserException("Could not parse lowerTerm or upperTerm into a number", nfe);
                }
                return(NO_MATCH_FILTER);
            }
        }
예제 #3
0
        public override SpanQuery GetSpanQuery(XmlElement e)
        {
            string slopString = DOMUtils.GetAttributeOrFail(e, "slop");
            int    slop       = int.Parse(slopString, CultureInfo.InvariantCulture);
            bool   inOrder    = DOMUtils.GetAttribute(e, "inOrder", false);

            JCG.List <SpanQuery> spans = new JCG.List <SpanQuery>();
            for (XmlNode kid = e.FirstChild; kid != null; kid = kid.NextSibling)
            {
                if (kid.NodeType == XmlNodeType.Element)
                {
                    spans.Add(factory.GetSpanQuery((XmlElement)kid));
                }
            }
            SpanQuery[] spanQueries = spans.ToArray(/*new SpanQuery[spans.size()]*/);
            return(new SpanNearQuery(spanQueries, slop, inOrder));
        }