/** * Constructs a {@link NodeNumericRangeQueryNode} object using the given * {@link NumericQueryNode} as its bounds and a {@link NumericAnalyzer}. * * @param lower the lower bound * @param upper the upper bound * @param lowerInclusive <code>true</code> if the lower bound is inclusive, otherwise, <code>false</code> * @param upperInclusive <code>true</code> if the upper bound is inclusive, otherwise, <code>false</code> * @param numericAnalyzer the {@link NumericAnalyzer} associated with the upper and lower bounds */ public NodeNumericRangeQueryNode(final NumericQueryNode lower, final NumericQueryNode upper, final boolean lowerInclusive, final boolean upperInclusive, final NumericAnalyzer numericAnalyzer) throws QueryNodeException { if (numericAnalyzer == null) { throw new IllegalArgumentException("numericAnalyzer cannot be null!"); } super.setBounds(lower, upper, lowerInclusive, upperInclusive); this.numericAnalyzer = numericAnalyzer; this.numericType = numericAnalyzer.getNumericParser().getNumericType(); }
@Override protected QueryNode postProcessNode(final QueryNode node) throws QueryNodeException { if (node instanceof FieldQueryNode && !(node.getParent() instanceof RangeQueryNode)) { final FieldQueryNode fieldNode = (FieldQueryNode) node; final Map<String, Analyzer> dts = this.getQueryConfigHandler().get(KeywordConfigurationKeys.DATATYPES_ANALYZERS); final Analyzer analyzer = dts.get(node.getTag(DatatypeQueryNode.DATATYPE_TAGID)); if (analyzer instanceof NumericAnalyzer) { final NumericAnalyzer na = (NumericAnalyzer) analyzer; final char[] text = fieldNode.getTextAsString().toCharArray(); final ReusableCharArrayReader textReader = new ReusableCharArrayReader(text); final Number number; try { number = na.getNumericParser().parse(textReader); } catch (final Exception e) { throw new QueryNodeParseException(new MessageImpl(QueryParserMessages.COULD_NOT_PARSE_NUMBER, text), e); } final CharSequence field = fieldNode.getField(); final NodeNumericQueryNode lowerNode = new NodeNumericQueryNode(field, number); final NodeNumericQueryNode upperNode = new NodeNumericQueryNode(field, number); return new NodeNumericRangeQueryNode(lowerNode, upperNode, true, true, na); } } return node; }
} else { final ReusableCharArrayReader lowerReader = new ReusableCharArrayReader(lowerText); lowerNumber = parser.parse(lowerReader); lowerText, parser.getNumericType() + " parser"), e); } else { final ReusableCharArrayReader upperReader = new ReusableCharArrayReader(upperText); upperNumber = parser.parse(upperReader); upperText, parser.getNumericType() + " parser"), e);
@Override public final boolean incrementToken() throws IOException { // initialise the numeric attribute if (!isInitialised) { final long value = parser.parseAndConvert(this.input); numericAtt.init(parser.getNumericType(), value, parser.getValueSize()); isInitialised = true; } // this will only clear all other attributes in this TokenStream this.clearAttributes(); // increment the shift and generate next token final boolean hasNext = numericAtt.incrementShift(termAtt); // set other attributes after the call to incrementShift since getShift // is undefined before first call typeAtt.setType((numericAtt.getShift() == 0) ? NumericTokenStream.TOKEN_TYPE_FULL_PREC : NumericTokenStream.TOKEN_TYPE_LOWER_PREC); posIncrAtt.setPositionIncrement((numericAtt.getShift() == 0) ? 1 : 0); return hasNext; }
@Test public void testParser() throws IOException { final DateNumericAnalyzer analyzer = new DateNumericAnalyzer(8); final NumericParser parser = analyzer.getNumericParser(); assertEquals(NumericType.LONG, parser.getNumericType()); assertEquals(64, parser.getValueSize()); final ReusableCharArrayReader input = new ReusableCharArrayReader("2012-09-21T00:00:00Z".toCharArray()); assertEquals(1348185600000l, parser.parseAndConvert(input)); }