Example usage for org.apache.lucene.queryparser.flexible.core.nodes QueryNode getParent

List of usage examples for org.apache.lucene.queryparser.flexible.core.nodes QueryNode getParent

Introduction

In this page you can find the example usage for org.apache.lucene.queryparser.flexible.core.nodes QueryNode getParent.

Prototype

public QueryNode getParent();

Source Link

Usage

From source file:at.ac.univie.mminf.luceneSKOS.queryparser.flexible.standard.processors.SKOSQueryNodeProcessor.java

License:Apache License

@Override
protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {

    if (node instanceof TextableQueryNode && !(node instanceof WildcardQueryNode)
            && !(node instanceof FuzzyQueryNode) && !(node instanceof RegexpQueryNode)
            && !(node.getParent() instanceof RangeQueryNode)) {

        FieldQueryNode fieldNode = ((FieldQueryNode) node);
        String text = fieldNode.getTextAsString();
        String field = fieldNode.getFieldAsString();

        TokenStream source;// w ww. j  a  va 2 s  .  co m
        try {
            source = this.analyzer.tokenStream(field, text);
            source.reset();
        } catch (IOException e1) {
            throw new RuntimeException(e1);
        }
        CachingTokenFilter buffer = new CachingTokenFilter(source);

        PositionIncrementAttribute posIncrAtt = null;
        int numTokens = 0;
        int positionCount = 0;
        boolean severalTokensAtSamePosition = false;

        if (buffer.hasAttribute(PositionIncrementAttribute.class)) {
            posIncrAtt = buffer.getAttribute(PositionIncrementAttribute.class);
        }

        try {

            while (buffer.incrementToken()) {
                numTokens++;
                int positionIncrement = (posIncrAtt != null) ? posIncrAtt.getPositionIncrement() : 1;
                if (positionIncrement != 0) {
                    positionCount += positionIncrement;

                } else {
                    severalTokensAtSamePosition = true;
                }

            }

        } catch (IOException e) {
            // ignore
        }

        try {
            // rewind the buffer stream
            buffer.reset();

            // close original stream - all tokens buffered
            source.close();
        } catch (IOException e) {
            // ignore
        }

        if (!buffer.hasAttribute(CharTermAttribute.class)) {
            return new NoTokenFoundQueryNode();
        }

        CharTermAttribute termAtt = buffer.getAttribute(CharTermAttribute.class);

        if (numTokens == 0) {
            return new NoTokenFoundQueryNode();

        } else if (numTokens == 1) {
            String term = null;
            try {
                boolean hasNext;
                hasNext = buffer.incrementToken();
                assert hasNext == true;
                term = termAtt.toString();

            } catch (IOException e) {
                // safe to ignore, because we know the number of tokens
            }

            fieldNode.setText(term);

            return fieldNode;

        } else if (severalTokensAtSamePosition || !(node instanceof QuotedFieldQueryNode)) {
            if (positionCount == 1 || !(node instanceof QuotedFieldQueryNode)) {
                // no phrase query:
                LinkedList<QueryNode> children = new LinkedList<QueryNode>();

                for (int i = 0; i < numTokens; i++) {
                    String term = null;
                    try {
                        boolean hasNext = buffer.incrementToken();
                        assert hasNext == true;
                        term = termAtt.toString();

                    } catch (IOException e) {
                        // safe to ignore, because we know the number of tokens
                    }

                    if (buffer.hasAttribute(SKOSTypeAttribute.class) && boosts != null) {

                        SKOSTypeAttribute skosAttr = buffer.getAttribute(SKOSTypeAttribute.class);
                        children.add(new BoostQueryNode(new FieldQueryNode(field, term, -1, -1),
                                getBoost(skosAttr.getSkosType())));

                    } else {

                        children.add(new FieldQueryNode(field, term, -1, -1));

                    }

                }
                return new GroupQueryNode(new StandardBooleanQueryNode(children, positionCount == 1));
            } else {
                // phrase query:
                MultiPhraseQueryNode mpq = new MultiPhraseQueryNode();

                List<FieldQueryNode> multiTerms = new ArrayList<FieldQueryNode>();
                int position = -1;
                int i = 0;
                int termGroupCount = 0;
                for (; i < numTokens; i++) {
                    String term = null;
                    int positionIncrement = 1;
                    try {
                        boolean hasNext = buffer.incrementToken();
                        assert hasNext == true;
                        term = termAtt.toString();
                        if (posIncrAtt != null) {
                            positionIncrement = posIncrAtt.getPositionIncrement();
                        }

                    } catch (IOException e) {
                        // safe to ignore, because we know the number of tokens
                    }

                    if (positionIncrement > 0 && multiTerms.size() > 0) {

                        for (FieldQueryNode termNode : multiTerms) {

                            if (this.positionIncrementsEnabled) {
                                termNode.setPositionIncrement(position);
                            } else {
                                termNode.setPositionIncrement(termGroupCount);
                            }

                            mpq.add(termNode);

                        }

                        // Only increment once for each "group" of
                        // terms that were in the same position:
                        termGroupCount++;

                        multiTerms.clear();

                    }

                    position += positionIncrement;
                    multiTerms.add(new FieldQueryNode(field, term, -1, -1));

                }

                for (FieldQueryNode termNode : multiTerms) {

                    if (this.positionIncrementsEnabled) {
                        termNode.setPositionIncrement(position);

                    } else {
                        termNode.setPositionIncrement(termGroupCount);
                    }

                    mpq.add(termNode);

                }

                return mpq;

            }

        } else {

            TokenizedPhraseQueryNode pq = new TokenizedPhraseQueryNode();

            int position = -1;

            for (int i = 0; i < numTokens; i++) {
                String term = null;
                int positionIncrement = 1;

                try {
                    boolean hasNext = buffer.incrementToken();
                    assert hasNext == true;
                    term = termAtt.toString();

                    if (posIncrAtt != null) {
                        positionIncrement = posIncrAtt.getPositionIncrement();
                    }

                } catch (IOException e) {
                    // safe to ignore, because we know the number of tokens
                }

                FieldQueryNode newFieldNode = new FieldQueryNode(field, term, -1, -1);

                if (this.positionIncrementsEnabled) {
                    position += positionIncrement;
                    newFieldNode.setPositionIncrement(position);

                } else {
                    newFieldNode.setPositionIncrement(i);
                }

                pq.add(newFieldNode);

            }

            return pq;

        }

    }

    return node;

}

From source file:com.sindicetech.siren.qparser.keyword.processors.ChangeDefaultOperatorNodeProcessor.java

License:Open Source License

@Override
protected QueryNode postProcessNode(final QueryNode node) throws QueryNodeException {

    if (node.getParent() == null) { // node is root, we processed the tree
        if (hasUnaryReqOperator) { // we found a req modifier in the tree

            final QueryConfigHandler conf = this.getQueryConfigHandler();
            if (!conf.has(ConfigurationKeys.DEFAULT_OPERATOR)) {
                throw new IllegalArgumentException(
                        "ConfigurationKeys.DEFAULT_OPERATOR should be set on the QueryConfigHandler");
            }/*w  ww.j  av a 2  s.co  m*/
            conf.set(ConfigurationKeys.DEFAULT_OPERATOR, Operator.OR);
        }
    }
    return node;

}

From source file:com.sindicetech.siren.qparser.keyword.processors.DatatypeAnalyzerProcessor.java

License:Open Source License

@Override
protected QueryNode postProcessNode(final QueryNode node) throws QueryNodeException {
    if (node instanceof TextableQueryNode && !(node instanceof WildcardQueryNode)
            && !(node instanceof FuzzyQueryNode) && !(node instanceof RegexpQueryNode)
            && !(node.getParent() instanceof RangeQueryNode)) {

        final FieldQueryNode fieldNode = ((FieldQueryNode) node);
        final String field = fieldNode.getFieldAsString();
        final String datatype = DatatypeProcessor.getDatatype(this.getQueryConfigHandler(), node);
        if (datatype == null) {
            return node;
        }//from   w  ww.j  ava 2 s.  com
        final Analyzer analyzer = this.getAnalyzer(datatype);

        TokenBuffer buffer = new TokenBuffer(analyzer, fieldNode);

        if (!buffer.hasCharTermAttribute()) {
            return new NoTokenFoundQueryNode();
        }

        switch (buffer.getNumTokens()) {
        case 0:
            return this.toEmptyQueryNode();

        case 1:
            fieldNode.setText(buffer.getFirstTerm());
            return fieldNode;

        default:
            final LinkedList<QueryNode> children = buffer.getFieldQueryNodes(field, datatype,
                    this.isPositionIncrementsEnabled());

            // Check for phrase query
            if (node.getParent() instanceof TokenizedPhraseQueryNode) {
                throw new QueryNodeException(new MessageImpl("Cannot build a MultiPhraseQuery"));
            }

            // If multiple terms at one single position, this must be a query
            // expansion. Perform a OR between the terms.
            if (buffer.hasSeveralTokensAtSamePosition() && buffer.getPositionCount() == 1) {
                OrQueryNode or = new OrQueryNode(children);
                GroupQueryNode group = new GroupQueryNode(or);
                // assign datatype
                or.setTag(DatatypeQueryNode.DATATYPE_TAGID, datatype);
                group.setTag(DatatypeQueryNode.DATATYPE_TAGID, datatype);
                return group;
            }
            // if several tokens at same position && position count > 1, then
            // results can be unexpected
            else {
                final TokenizedPhraseQueryNode pq = new TokenizedPhraseQueryNode();
                for (int i = 0; i < children.size(); i++) {
                    pq.add(children.get(i));
                }
                // assign datatype
                pq.setTag(DatatypeQueryNode.DATATYPE_TAGID, datatype);
                return pq;
            }
        }
    } else if (node instanceof TwigQueryNode) {
        nbTwigs--;
        assert nbTwigs >= 0;
    }
    return node;
}

From source file:com.sindicetech.siren.qparser.keyword.processors.GroupQueryNodeProcessor.java

License:Open Source License

private void processNode(final QueryNode node) throws QueryNodeException {
    if (node instanceof AndQueryNode || node instanceof OrQueryNode) {
        if (!this.latestNodeVerified && !this.queryNodeList.isEmpty()) {
            this.queryNodeList
                    .add(this.applyModifier(this.queryNodeList.remove(this.queryNodeList.size() - 1), node));
            this.latestNodeVerified = true;
        }//from  w w  w . j a v a2 s. com
    } else if (node instanceof GroupQueryNode) {
        final ArrayList<QueryNode> actualQueryNodeList = this.queryNodeList;
        actualQueryNodeList.add(this.applyModifier(this.process(node), node.getParent()));
        this.queryNodeList = actualQueryNodeList;
        this.latestNodeVerified = false;
    } else if (node instanceof TwigQueryNode) {
        final ArrayList<QueryNode> actualQueryNodeList = this.queryNodeList;
        final TwigQueryNode twigNode = (TwigQueryNode) node;
        final QueryNode root = twigNode.getRoot();
        final QueryNode child = twigNode.getChild();
        if (!(root instanceof WildcardNodeQueryNode)) { // the root is not empty
            twigNode.setRoot(this.process(root));
        }
        if (!(child instanceof WildcardNodeQueryNode)) { // the child is not empty
            twigNode.setChild(this.process(child));
        }
        actualQueryNodeList.add(twigNode);
        this.queryNodeList = actualQueryNodeList;
        this.latestNodeVerified = false;
    } else if (node instanceof ArrayQueryNode) {
        final ArrayList<QueryNode> actualQueryNodeList = this.queryNodeList;
        final ArrayQueryNode arrayNode = (ArrayQueryNode) node;
        final List<QueryNode> children = arrayNode.getChildren();
        final List<QueryNode> newChildren = new ArrayList<QueryNode>();
        for (final QueryNode child : children) {
            // The unary modifier sets the occurrence of this value in the TwigQuery
            if (!(child instanceof ModifierQueryNode)) {
                newChildren.add(this.process(child));
            } else {
                newChildren.add(child);
            }
        }
        arrayNode.set(newChildren);
        actualQueryNodeList.add(arrayNode);
        this.queryNodeList = actualQueryNodeList;
        this.latestNodeVerified = false;
    } else if (node instanceof TopLevelQueryNode) {
        final ArrayList<QueryNode> actualQueryNodeList = this.queryNodeList;
        final TopLevelQueryNode topNode = (TopLevelQueryNode) node;
        final QueryNode child = topNode.getChildren().get(0);
        topNode.set(Arrays.asList(this.process(child)));
        actualQueryNodeList.add(topNode);
        this.queryNodeList = actualQueryNodeList;
        this.latestNodeVerified = false;
    } else if (node instanceof DatatypeQueryNode) {
        final ArrayList<QueryNode> actualQueryNodeList = this.queryNodeList;
        final DatatypeQueryNode dtNode = (DatatypeQueryNode) node;
        final QueryNode child = dtNode.getChild();
        dtNode.set(Arrays.asList(this.applyModifier(this.process(child), node.getParent())));
        actualQueryNodeList.add(dtNode);
        this.queryNodeList = actualQueryNodeList;
        this.latestNodeVerified = false;
    } else if (!(node instanceof BooleanQueryNode)) {
        // if the parent node is a datatype query node, we have to skip it and return its parent
        QueryNode parent = node.getParent();
        parent = (parent instanceof DatatypeQueryNode) ? parent.getParent() : parent;
        // Apply the modifier and add it back to the list
        this.queryNodeList.add(this.applyModifier(node, parent));
        this.latestNodeVerified = false;
    }
}

From source file:com.sindicetech.siren.qparser.keyword.processors.NodeNumericQueryNodeProcessor.java

License:Open Source License

@Override
protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {

    if (node instanceof FieldQueryNode && !(node.getParent() instanceof RangeQueryNode)) {
        final FieldQueryNode fieldNode = (FieldQueryNode) node;

        String datatype = (String) DatatypeProcessor.getDatatype(this.getQueryConfigHandler(), 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);
            }//w  w  w .  j a v  a 2  s .c o  m

            final CharSequence field = fieldNode.getField();
            final NodeNumericQueryNode lowerNode = new NodeNumericQueryNode(field, number);
            // assign datatype
            lowerNode.setTag(DatatypeQueryNode.DATATYPE_TAGID, datatype);
            final NodeNumericQueryNode upperNode = new NodeNumericQueryNode(field, number);
            // assign datatype
            upperNode.setTag(DatatypeQueryNode.DATATYPE_TAGID, datatype);

            // Create the NodeNumericRangeQueryNode
            node = new NodeNumericRangeQueryNode(lowerNode, upperNode, true, true, na);
            // assign datatype
            node.setTag(DatatypeQueryNode.DATATYPE_TAGID, datatype);
        }
    }
    return node;
}

From source file:com.sindicetech.siren.qparser.keyword.processors.PhraseQueryNodeProcessor.java

License:Open Source License

@Override
protected QueryNode postProcessNode(final QueryNode node) throws QueryNodeException {
    if (node instanceof TextableQueryNode && !(node instanceof WildcardQueryNode)
            && !(node instanceof FuzzyQueryNode) && !(node instanceof RegexpQueryNode)
            && !(node instanceof ProtectedQueryNode) && !(node.getParent() instanceof RangeQueryNode)) {

        final FieldQueryNode fieldNode = ((FieldQueryNode) node);
        final String text = fieldNode.getTextAsString();
        final String field = fieldNode.getFieldAsString();

        final TokenStream source;
        try {//from w  ww  .  j a va2 s. com
            source = this.analyzer.tokenStream(field, new StringReader(text));
            source.reset();
        } catch (final IOException e1) {
            throw new RuntimeException(e1);
        }
        final CachingTokenFilter buffer = new CachingTokenFilter(source);

        int numTokens = 0;
        try {
            while (buffer.incrementToken()) {
                numTokens++;
            }
        } catch (final IOException e) {
            // ignore
        }

        try {
            // rewind the buffer stream
            buffer.reset();
            // close original stream - all tokens buffered
            source.close();
        } catch (final IOException e) {
            // ignore
        }

        if (!buffer.hasAttribute(CharTermAttribute.class)) {
            return new NoTokenFoundQueryNode();
        }
        final CharTermAttribute termAtt = buffer.getAttribute(CharTermAttribute.class);

        if (numTokens == 0) {
            return new NoTokenFoundQueryNode();
        }
        // phrase query
        else if (numTokens != 1) {
            String datatype = (String) DatatypeProcessor.getDatatype(this.getQueryConfigHandler(), node);
            final TokenizedPhraseQueryNode pq = new TokenizedPhraseQueryNode();
            // assign datatype
            pq.setTag(DatatypeQueryNode.DATATYPE_TAGID, datatype);

            for (int i = 0; i < numTokens; i++) {
                String term = null;

                try {
                    final boolean hasNext = buffer.incrementToken();
                    assert hasNext == true;
                    term = termAtt.toString();

                } catch (final IOException e) {
                    // safe to ignore, because we know the number of tokens
                }

                final FieldQueryNode newFieldNode = new FieldQueryNode(field, term, -1, -1);
                // set position increment
                newFieldNode.setPositionIncrement(i);
                // assign datatype
                newFieldNode.setTag(DatatypeQueryNode.DATATYPE_TAGID, datatype);
                pq.add(newFieldNode);
            }
            return pq;
        }
    }
    return node;
}

From source file:com.sindicetech.siren.qparser.keyword.processors.WildcardNodeQueryNodeProcessor.java

License:Open Source License

@Override
protected QueryNode preProcessNode(final QueryNode node) throws QueryNodeException {
    if ((node.getParent() instanceof TwigQueryNode || node.getParent() instanceof ArrayQueryNode)
            && this.isEmptyNode(node)) {
        return new WildcardNodeQueryNode();
    }//from ww w.  j a v  a  2  s  .c  o m
    return node;
}

From source file:org.sindice.siren.qparser.keyword.processors.DatatypeAnalyzerProcessor.java

License:Apache License

@Override
protected QueryNode postProcessNode(final QueryNode node) throws QueryNodeException {
    if (node instanceof TextableQueryNode && !(node instanceof WildcardQueryNode)
            && !(node instanceof FuzzyQueryNode) && !(node instanceof RegexpQueryNode)
            && !(node.getParent() instanceof RangeQueryNode)) {

        this.positionIncrementsEnabled = false;
        final Boolean positionIncrementsEnabled = this.getQueryConfigHandler()
                .get(ConfigurationKeys.ENABLE_POSITION_INCREMENTS);
        if (positionIncrementsEnabled != null) {
            this.positionIncrementsEnabled = positionIncrementsEnabled;
        }/*  w  ww. j a v a 2 s.co  m*/

        final FieldQueryNode fieldNode = ((FieldQueryNode) node);
        final String text = fieldNode.getTextAsString();
        final String field = fieldNode.getFieldAsString();
        final String datatype = (String) fieldNode.getTag(DatatypeQueryNode.DATATYPE_TAGID);

        if (datatype == null) {
            return node;
        }

        final Analyzer analyzer = this.getQueryConfigHandler().get(KeywordConfigurationKeys.DATATYPES_ANALYZERS)
                .get(datatype);
        if (analyzer == null) {
            throw new QueryNodeException(new MessageImpl(QueryParserMessages.INVALID_SYNTAX,
                    "No analyzer associated with " + datatype));
        }

        PositionIncrementAttribute posIncrAtt = null;
        int numTokens = 0;
        int positionCount = 0;
        boolean severalTokensAtSamePosition = false;

        final TokenStream source;
        try {
            source = analyzer.tokenStream(field, new StringReader(text));
            source.reset();
        } catch (final IOException e1) {
            throw new RuntimeException(e1);
        }
        final CachingTokenFilter buffer = new CachingTokenFilter(source);

        if (buffer.hasAttribute(PositionIncrementAttribute.class)) {
            posIncrAtt = buffer.getAttribute(PositionIncrementAttribute.class);
        }

        try {
            while (buffer.incrementToken()) {
                numTokens++;
                final int positionIncrement = (posIncrAtt != null) ? posIncrAtt.getPositionIncrement() : 1;
                if (positionIncrement != 0) {
                    positionCount += positionIncrement;
                } else {
                    severalTokensAtSamePosition = true;
                }
            }
        } catch (final IOException e) {
            // ignore
        }

        try {
            // rewind the buffer stream
            buffer.reset();
            // close original stream - all tokens buffered
            source.close();
        } catch (final IOException e) {
            // ignore
        }

        if (!buffer.hasAttribute(CharTermAttribute.class)) {
            return new NoTokenFoundQueryNode();
        }
        final CharTermAttribute termAtt = buffer.getAttribute(CharTermAttribute.class);

        if (numTokens == 0) {
            if (nbTwigs != 0) { // Twig special case
                return new WildcardNodeQueryNode();
            }
            return new NoTokenFoundQueryNode();
        } else if (numTokens == 1) {
            String term = null;
            try {
                boolean hasNext;
                hasNext = buffer.incrementToken();
                assert hasNext == true;
                term = termAtt.toString();
            } catch (final IOException e) {
                // safe to ignore, because we know the number of tokens
            }
            fieldNode.setText(term);
            return fieldNode;
        } else {
            // no phrase query:
            final LinkedList<QueryNode> children = new LinkedList<QueryNode>();

            int position = -1;

            for (int i = 0; i < numTokens; i++) {
                String term = null;
                final int positionIncrement = 1;

                try {
                    final boolean hasNext = buffer.incrementToken();
                    assert hasNext == true;
                    term = termAtt.toString();

                } catch (final IOException e) {
                    // safe to ignore, because we know the number of tokens
                }

                final FieldQueryNode newFieldNode = new FieldQueryNode(field, term, -1, -1);

                if (this.positionIncrementsEnabled) {
                    position += positionIncrement;
                    newFieldNode.setPositionIncrement(position);
                } else {
                    newFieldNode.setPositionIncrement(i);
                }

                children.add(new FieldQueryNode(field, term, -1, -1));
            }

            if (node.getParent() instanceof TokenizedPhraseQueryNode) {
                throw new QueryNodeException(new MessageImpl("Cannot build a MultiPhraseQuery"));
            }
            // If multiple terms at one single position, this must be a query
            // expansion. Perform a OR between the terms.
            if (severalTokensAtSamePosition && positionCount == 1) {
                return new GroupQueryNode(new OrQueryNode(children));
            }
            // if several tokens at same position && position count > 1, then
            // results can be unexpected
            else {
                final TokenizedPhraseQueryNode pq = new TokenizedPhraseQueryNode();
                for (int i = 0; i < children.size(); i++) {
                    pq.add(children.get(i));
                }
                return pq;
            }
        }
    } else if (node instanceof TwigQueryNode) {
        nbTwigs--;
        assert nbTwigs >= 0;
    }
    return node;
}

From source file:org.sindice.siren.qparser.keyword.processors.GroupQueryNodeProcessor.java

License:Apache License

private void processNode(final QueryNode node) throws QueryNodeException {
    if (node instanceof AndQueryNode || node instanceof OrQueryNode) {
        if (!this.latestNodeVerified && !this.queryNodeList.isEmpty()) {
            this.queryNodeList
                    .add(this.applyModifier(this.queryNodeList.remove(this.queryNodeList.size() - 1), node));
            this.latestNodeVerified = true;
        }//w  w w  .  jav  a 2  s .c o m
    } else if (node instanceof GroupQueryNode) {
        final ArrayList<QueryNode> actualQueryNodeList = this.queryNodeList;
        actualQueryNodeList.add(this.applyModifier(this.process(node), node.getParent()));
        this.queryNodeList = actualQueryNodeList;
        this.latestNodeVerified = false;
    } else if (node instanceof TwigQueryNode) {
        final ArrayList<QueryNode> actualQueryNodeList = this.queryNodeList;
        final TwigQueryNode twigNode = (TwigQueryNode) node;
        final QueryNode root = twigNode.getRoot();
        final QueryNode child = twigNode.getChild();
        if (!(root instanceof WildcardNodeQueryNode)) { // the root is not empty
            twigNode.setRoot(this.process(root));
        }
        if (!(child instanceof WildcardNodeQueryNode)) { // the child is not empty
            twigNode.setChild(this.process(child));
        }
        actualQueryNodeList.add(twigNode);
        this.queryNodeList = actualQueryNodeList;
        this.latestNodeVerified = false;
    } else if (node instanceof ArrayQueryNode) {
        final ArrayList<QueryNode> actualQueryNodeList = this.queryNodeList;
        final ArrayQueryNode arrayNode = (ArrayQueryNode) node;
        final List<QueryNode> children = arrayNode.getChildren();
        final List<QueryNode> newChildren = new ArrayList<QueryNode>();
        for (final QueryNode child : children) {
            // The unary modifier sets the occurrence of this value in the TwigQuery
            if (!(child instanceof ModifierQueryNode)) {
                newChildren.add(this.process(child));
            } else {
                newChildren.add(child);
            }
        }
        arrayNode.set(newChildren);
        actualQueryNodeList.add(arrayNode);
        this.queryNodeList = actualQueryNodeList;
        this.latestNodeVerified = false;
    } else if (node instanceof TopLevelQueryNode) {
        final ArrayList<QueryNode> actualQueryNodeList = this.queryNodeList;
        final TopLevelQueryNode topNode = (TopLevelQueryNode) node;
        final QueryNode child = topNode.getChildren().get(0);
        topNode.set(Arrays.asList(this.process(child)));
        actualQueryNodeList.add(topNode);
        this.queryNodeList = actualQueryNodeList;
        this.latestNodeVerified = false;
    } else if (node instanceof DatatypeQueryNode) {
        final ArrayList<QueryNode> actualQueryNodeList = this.queryNodeList;
        final DatatypeQueryNode dtNode = (DatatypeQueryNode) node;
        final QueryNode child = dtNode.getChild();
        dtNode.set(Arrays.asList(this.applyModifier(this.process(child), node.getParent())));
        actualQueryNodeList.add(dtNode);
        this.queryNodeList = actualQueryNodeList;
        this.latestNodeVerified = false;
    } else if (!(node instanceof BooleanQueryNode)) {
        this.queryNodeList.add(this.applyModifier(node, node.getParent()));
        this.latestNodeVerified = false;
    }
}

From source file:org.sindice.siren.qparser.keyword.processors.NodeNumericQueryNodeProcessor.java

License:Apache License

@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);
            }//from   w w w.  ja v a2  s  .  co  m

            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;

}