Example usage for org.apache.lucene.queryparser.flexible.messages MessageImpl MessageImpl

List of usage examples for org.apache.lucene.queryparser.flexible.messages MessageImpl MessageImpl

Introduction

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

Prototype

public MessageImpl(String key) 

Source Link

Usage

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

License:Open Source License

public BooleanQuery build(QueryNode queryNode) throws QueryNodeException {
    AnyQueryNode andNode = (AnyQueryNode) queryNode;

    BooleanQuery bQuery = new BooleanQuery();
    List<QueryNode> children = andNode.getChildren();

    if (children != null) {

        for (QueryNode child : children) {
            Object obj = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);

            if (obj != null) {
                Query query = (Query) obj;

                try {
                    bQuery.add(query, BooleanClause.Occur.SHOULD);
                } catch (TooManyClauses ex) {

                    throw new QueryNodeException(new MessageImpl(
                            /*
                             * IQQQ.Q0028E_TOO_MANY_BOOLEAN_CLAUSES,
                             * BooleanQuery.getMaxClauseCount()
                             *//*w ww. ja v  a 2  s.  co m*/
                            QueryParserMessages.EMPTY_MESSAGE), ex);

                }

            }

        }

    }

    bQuery.setMinimumNumberShouldMatch(andNode.getMinimumMatchingElements());

    return bQuery;

}

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
    }//from w w w . j a  v a 2s.  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.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));
    }//  w ww  .  ja  v a2 s . c  om
    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.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 .  j a  va2s.  c om
        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.WildcardNodeQueryNodeProcessor.java

License:Open Source License

@Override
protected QueryNode postProcessNode(final QueryNode node) throws QueryNodeException {
    if (node instanceof TwigQueryNode) {
        final TwigQueryNode twig = (TwigQueryNode) node;
        if (twig.getChild() instanceof WildcardNodeQueryNode
                && twig.getRoot() instanceof WildcardNodeQueryNode) {
            throw new QueryNodeException(
                    new MessageImpl("Twig with both root and child empty is not allowed."));
        }/*w  ww  .  ja va2s .c o m*/
    }
    return node;
}