Example usage for org.apache.lucene.queryparser.flexible.core.messages QueryParserMessages INVALID_SYNTAX

List of usage examples for org.apache.lucene.queryparser.flexible.core.messages QueryParserMessages INVALID_SYNTAX

Introduction

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

Prototype

String INVALID_SYNTAX

To view the source code for org.apache.lucene.queryparser.flexible.core.messages QueryParserMessages INVALID_SYNTAX.

Click 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  w  ww. j  a v  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.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.ja 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.DatatypeAnalyzerProcessor.java

License:Open Source License

private Analyzer getAnalyzer(String datatype) throws QueryNodeException {
    final Analyzer analyzer = this.getQueryConfigHandler().get(KeywordConfigurationKeys.DATATYPES_ANALYZERS)
            .get(datatype);//from  www  .  ja v a2s  .  co m
    if (analyzer == null) {
        throw new QueryNodeException(
                new MessageImpl(QueryParserMessages.INVALID_SYNTAX, "No analyzer associated with " + datatype));
    }

    return analyzer;
}

From source file:com.sindicetech.siren.qparser.tree.ParseException.java

License:Open Source License

public ParseException(final String message, final Throwable throwable) {
    super(new MessageImpl(QueryParserMessages.INVALID_SYNTAX, message), throwable);
}

From source file:com.sindicetech.siren.qparser.tree.ParseException.java

License:Open Source License

public ParseException(final String message) {
    super(new MessageImpl(QueryParserMessages.INVALID_SYNTAX, message));
}

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;
        }//from   w ww.jav 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;
}