Example usage for org.apache.lucene.queryparser.flexible.core QueryNodeException QueryNodeException

List of usage examples for org.apache.lucene.queryparser.flexible.core QueryNodeException QueryNodeException

Introduction

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

Prototype

public QueryNodeException(Throwable throwable) 

Source Link

Usage

From source file:com.sindicetech.siren.qparser.keyword.builders.ArrayQueryNodeBuilder.java

License:Open Source License

@Override
public Query build(final QueryNode queryNode) throws QueryNodeException {
    final ArrayQueryNode arrayNode = (ArrayQueryNode) queryNode;
    final List<QueryNode> children = arrayNode.getChildren();
    final ArrayQuery arrayQuery = new ArrayQuery();

    for (final QueryNode child : children) {
        final Object v = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
        if (v == null) { // DummyNode such as the EmptyNodeQueryNode
            continue;
        }//from ww  w .j  av a  2 s .  c o  m
        if (v instanceof Query) {
            if (v instanceof ArrayQuery) {
                /*
                 * Nested array query. It is transformed as a TwigQuery with empty root
                 */
                final TwigQuery twigQuery = new TwigQuery();
                for (final Query qn : ((ArrayQuery) v).getElements()) {
                    final NodeQuery valQuery = (NodeQuery) qn;
                    twigQuery.addChild(valQuery,
                            NodeQueryBuilderUtil.getModifierValue(child, NodeBooleanClause.Occur.MUST));
                }
                arrayQuery.addElement(twigQuery);
            } else {
                arrayQuery.addElement((NodeQuery) v);
            }
        } else {
            throw new QueryNodeException(new MessageImpl(QueryParserMessages.INVALID_SYNTAX,
                    "Unexpected class of a Twig Query clause: " + v == null ? "null" : v.getClass().getName()));
        }
    }
    return arrayQuery;
}

From source file:com.sindicetech.siren.qparser.keyword.builders.concise.ConciseNodeNumericRangeQueryNodeBuilder.java

License:Open Source License

@Override
protected NodeNumericRangeQuery newNodeNumericRangeQuery(final NumericType numberType, final String field,
        final int precisionStep, final Number lowerNumber, final Number upperNumber, final boolean minInclusive,
        final boolean maxInclusive) throws QueryNodeException {
    if (conf.has(ConciseKeywordQueryConfigHandler.ConciseKeywordConfigurationKeys.ATTRIBUTE)) {
        final String attribute = conf
                .get(ConciseKeywordQueryConfigHandler.ConciseKeywordConfigurationKeys.ATTRIBUTE);

        switch (numberType) {
        case LONG:
            return ConciseNodeNumericRangeQuery.newLongRange(field, attribute, precisionStep,
                    (Long) lowerNumber, (Long) upperNumber, minInclusive, maxInclusive);

        case INT:
            return ConciseNodeNumericRangeQuery.newIntRange(field, attribute, precisionStep,
                    (Integer) lowerNumber, (Integer) upperNumber, minInclusive, maxInclusive);

        case FLOAT:
            return ConciseNodeNumericRangeQuery.newFloatRange(field, attribute, precisionStep,
                    (Float) lowerNumber, (Float) upperNumber, minInclusive, maxInclusive);

        case DOUBLE:
            return ConciseNodeNumericRangeQuery.newDoubleRange(field, attribute, precisionStep,
                    (Double) lowerNumber, (Double) upperNumber, minInclusive, maxInclusive);

        default:/*from   ww  w . j a va2  s .  co  m*/
            throw new QueryNodeException(
                    new MessageImpl(QueryParserMessages.UNSUPPORTED_NUMERIC_DATA_TYPE, numberType));
        }
    } else {
        return super.newNodeNumericRangeQuery(numberType, field, precisionStep, lowerNumber, upperNumber,
                minInclusive, maxInclusive);
    }
}

From source file:com.sindicetech.siren.qparser.keyword.builders.NodeBooleanQueryNodeBuilder.java

License:Open Source License

public NodeQuery build(final QueryNode queryNode) throws QueryNodeException {
    final NodeBooleanQueryNode booleanNode = (NodeBooleanQueryNode) queryNode;
    final List<QueryNode> children = booleanNode.getChildren();
    final NodeBooleanQuery bq = new NodeBooleanQuery();

    if (children == null) {
        return bq; // return empty boolean query
    }//from ww  w  .  jav  a  2s  .  c  o m

    // If more than one child, wrap them into a NodeBooleanQuery
    if (children.size() > 1) {
        for (final QueryNode child : children) {
            final Object obj = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
            if (obj != null) {
                if (obj instanceof NodeQuery) {
                    final QueryNode mod;
                    if (child instanceof DatatypeQueryNode) {
                        mod = ((DatatypeQueryNode) child).getChild();
                    } else {
                        mod = child;
                    }
                    bq.add((NodeQuery) obj,
                            NodeQueryBuilderUtil.getModifierValue(mod, NodeBooleanClause.Occur.SHOULD));
                } else {
                    throw new QueryNodeException(
                            new Error("Expected NodeQuery: got '" + obj.getClass().getCanonicalName() + "'"));
                }
            }
        }
        return bq;
    }
    // If only one child, return it directly
    else {
        final Object obj = children.get(0).getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
        if (obj != null) {
            if (obj instanceof NodeQuery) {
                return (NodeQuery) obj;
            } else {
                throw new QueryNodeException(
                        new Error("Non NodeQuery query '" + obj.getClass().getCanonicalName() + "' received"));
            }
        }
        return bq; // return empty boolean query
    }
}

From source file:com.sindicetech.siren.qparser.keyword.builders.NodeNumericRangeQueryNodeBuilder.java

License:Open Source License

protected NodeNumericRangeQuery newNodeNumericRangeQuery(final NumericType numberType, final String field,
        final int precisionStep, final Number lowerNumber, final Number upperNumber, final boolean minInclusive,
        final boolean maxInclusive) throws QueryNodeException {
    switch (numberType) {
    case LONG://  ww w. j a  v  a 2  s  . c om
        return NodeNumericRangeQuery.newLongRange(field, precisionStep, (Long) lowerNumber, (Long) upperNumber,
                minInclusive, maxInclusive);

    case INT:
        return NodeNumericRangeQuery.newIntRange(field, precisionStep, (Integer) lowerNumber,
                (Integer) upperNumber, minInclusive, maxInclusive);

    case FLOAT:
        return NodeNumericRangeQuery.newFloatRange(field, precisionStep, (Float) lowerNumber,
                (Float) upperNumber, minInclusive, maxInclusive);

    case DOUBLE:
        return NodeNumericRangeQuery.newDoubleRange(field, precisionStep, (Double) lowerNumber,
                (Double) upperNumber, minInclusive, maxInclusive);

    default:
        throw new QueryNodeException(
                new MessageImpl(QueryParserMessages.UNSUPPORTED_NUMERIC_DATA_TYPE, numberType));
    }
}

From source file:com.sindicetech.siren.qparser.keyword.builders.SpanBooleanQueryNodeBuilder.java

License:Open Source License

public NodeQuery build(final QueryNode queryNode) throws QueryNodeException {
    final SpanBooleanQueryNode booleanNode = (SpanBooleanQueryNode) queryNode;
    final List<QueryNode> children = booleanNode.getChildren();
    final BooleanSpanQuery bq = new BooleanSpanQuery(booleanNode.getSlop(), booleanNode.isInOrder());

    if (children == null) {
        return bq; // return empty boolean query
    }/* w w  w  .j av  a2 s . c o m*/

    // If more than one child, add them into the BooleanSpanQuery
    if (children.size() > 1) {
        for (final QueryNode child : children) {
            final Object obj = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
            if (obj != null) {
                if (obj instanceof SpanQuery) {
                    bq.add((SpanQuery) obj,
                            NodeQueryBuilderUtil.getModifierValue(child, NodeBooleanClause.Occur.SHOULD));
                } else {
                    throw new QueryNodeException(new MessageImpl(
                            "Expected SpanQuery: got '" + obj.getClass().getCanonicalName() + "'"));
                }
            }
        }
        return bq;
    }
    // If only one child, return it directly
    else {
        final Object obj = children.get(0).getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
        if (obj != null) {
            if (obj instanceof SpanQuery) {
                return (NodeQuery) obj;
            } else {
                throw new QueryNodeException(
                        new Error("Non SpanQuery query '" + obj.getClass().getCanonicalName() + "' received"));
            }
        }
        return bq; // return empty boolean query
    }
}

From source file:com.sindicetech.siren.qparser.keyword.builders.TopLevelQueryNodeBuilder.java

License:Open Source License

/**
 * Wraps a {@link NodeQuery} into a {@link LuceneProxyNodeQuery}.
 * This method is applied on each clause of a {@link BooleanQuery}.
 *///  www. j a v a2  s. c  o  m
protected Query wrap(final Query q) throws QueryNodeException {
    if (q instanceof BooleanQuery) {
        for (final BooleanClause clause : ((BooleanQuery) q).clauses()) {
            final Query cq = clause.getQuery();
            clause.setQuery(this.wrap(cq));
        }
        return q;
    } else if (q instanceof NodeQuery) {
        return new LuceneProxyNodeQuery((NodeQuery) q);
    } else {
        throw new QueryNodeException(new Error(
                "Expected a BooleanQuery or a NodeQuery: got '" + q.getClass().getCanonicalName() + "'"));
    }
}

From source file:com.sindicetech.siren.qparser.keyword.builders.TwigQueryNodeBuilder.java

License:Open Source License

@Override
public Query build(final QueryNode queryNode) throws QueryNodeException {
    final TwigQueryNode tqn = (TwigQueryNode) queryNode;
    final QueryNode root = tqn.getRoot();
    final QueryNode child = tqn.getChild();
    final TwigQuery twigQuery;
    final int rootLevel = tqn.getRootLevel();

    if (root == null && child == null) {
        throw new QueryNodeException(new MessageImpl(QueryParserMessages.EMPTY_MESSAGE));
    }//from  ww w .  j  a  v  a 2 s. c o m
    if (tqn.getChildren().size() != 2) {
        throw new IllegalArgumentException(
                "A TwigQueryNode cannot have more " + "than 2 children:\n" + tqn.getChildren().toString());
    }
    if (child instanceof WildcardNodeQueryNode && root instanceof WildcardNodeQueryNode) {
        throw new QueryNodeException(
                new MessageImpl("Twig with both root and " + "child empty is not allowed."));
    }
    // Build the root operand
    if (root instanceof WildcardNodeQueryNode) { // Empty root query
        twigQuery = new TwigQuery(rootLevel);
    } else {
        final Object attQuery = root.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
        if (attQuery != null) {
            twigQuery = new TwigQuery(rootLevel);
            twigQuery.addRoot((NodeQuery) attQuery);
        } else {
            throw new QueryNodeException(new MessageImpl(QueryParserMessages.INVALID_SYNTAX,
                    "Unable to get the root of the Twig query"));
        }
    }
    if (!(child instanceof WildcardNodeQueryNode)) {
        // Build the child operand
        final Object v = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
        if (v instanceof ArrayQuery) { // array of children nodes
            final ArrayQueryNode aqn = (ArrayQueryNode) child;
            final List<Query> children = ((ArrayQuery) v).getElements();
            for (int i = 0; i < children.size(); i++) {
                twigQuery.addChild((NodeQuery) children.get(i), NodeQueryBuilderUtil
                        .getModifierValue(aqn.getChildren().get(i), NodeBooleanClause.Occur.MUST));
            }
        } else if (v instanceof Query) {
            final NodeQuery valQuery = (NodeQuery) v;
            twigQuery.addChild(valQuery, Occur.MUST);
        } else {
            throw new QueryNodeException(new MessageImpl(QueryParserMessages.INVALID_SYNTAX,
                    "Unexpected class of a Twig Query clause: " + v == null ? "null" : v.getClass().getName()));
        }
    }

    return twigQuery;
}

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

License:Open Source License

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

    if (node instanceof WildcardQueryNode) {
        throw new QueryNodeException(
                new MessageImpl("Wildcard not allowed", node.toQueryString(new EscapeQuerySyntaxImpl())));
    }//w w  w .  j  a  v a2  s .co m

    if (node instanceof FuzzyQueryNode) {
        throw new QueryNodeException(
                new MessageImpl("Fuzzy not allowed", node.toQueryString(new EscapeQuerySyntaxImpl())));
    }

    return node;

}

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

License:Open Source License

@Override
protected QueryNode preProcessNode(final QueryNode node) throws QueryNodeException {
    if (node instanceof TwigQueryNode) {
        if (this.getQueryConfigHandler().has(KeywordConfigurationKeys.ALLOW_TWIG)) {
            if (!this.getQueryConfigHandler().get(KeywordConfigurationKeys.ALLOW_TWIG)) {
                throw new QueryNodeException(new MessageImpl("TwigQuery not allowed",
                        node.toQueryString(new EscapeQuerySyntaxImpl())));
            }/*from  w w  w. jav a2  s  . c om*/
        } else {
            throw new IllegalArgumentException(
                    "KeywordConfigurationKeys.ALLOW_TWIG should be set on the ExtendedKeywordQueryConfigHandler");
        }
    }
    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 w w  .  ja  va2 s  .  co m
        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;
}