Package org.apache.lucene.queryparser.flexible.core

Examples of org.apache.lucene.queryparser.flexible.core.QueryNodeException


  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."));
      }
    }
    return node;
  }
View Full Code Here


  @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())));
    }


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

    return node;
View Full Code Here

  @Override
  protected QueryNode preProcessNode(final QueryNode node)
  throws QueryNodeException {
    if (node instanceof SlopQueryNode && ((SlopQueryNode) node).getValue() != 0) {
      throw new QueryNodeException(new MessageImpl("Slop queries are not supported",
        node.toQueryString(new EscapeQuerySyntaxImpl())));
    } else if (node instanceof MultiPhraseQueryNode) {
      throw new QueryNodeException(new MessageImpl("Multi phrase queries are not supported",
        node.toQueryString(new EscapeQuerySyntaxImpl())));
    } else if (node instanceof MatchAllDocsQueryNodeBuilder) {
      throw new QueryNodeException(new MessageImpl("MatchAllDocsQueries are not supported",
        node.toQueryString(new EscapeQuerySyntaxImpl())));
    }
    return node;
  }
View Full Code Here

      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));
View Full Code Here

  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())));
        }
      } else {
        throw new IllegalArgumentException("KeywordConfigurationKeys.ALLOW_TWIG should be set on the KeywordQueryConfigHandler");
      }
View Full Code Here

    // process child and descendant queries
    try {
      processChildren(children, query);
    }
    catch (final TooManyClauses ex) {
      throw new QueryNodeException(new MessageImpl(
          QueryParserMessages.TOO_MANY_BOOLEAN_CLAUSES,
          BooleanQuery.getMaxClauseCount(),
          twigNode.toQueryString(new EscapeQuerySyntaxImpl())), ex);
    }
View Full Code Here

      final Query query = new LuceneProxyNodeQuery((NodeQuery) obj);
      try {
        bQuery.add(query, getModifierValue(child));
      }
      catch (final TooManyClauses ex) {
        throw new QueryNodeException(new MessageImpl(
            QueryParserMessages.TOO_MANY_BOOLEAN_CLAUSES, BooleanQuery
                .getMaxClauseCount(), queryNode
                .toQueryString(new EscapeQuerySyntaxImpl())), ex);
      }
    }
View Full Code Here

            }
            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
    }
View Full Code Here

    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));
    }
    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;
View Full Code Here

            }
            bQuery.add(query, getModifierValue(mod));

          } catch (final TooManyClauses ex) {

            throw new QueryNodeException(new MessageImpl(
                QueryParserMessages.TOO_MANY_BOOLEAN_CLAUSES, BooleanQuery
                    .getMaxClauseCount(), queryNode
                    .toQueryString(new EscapeQuerySyntaxImpl())), ex);

          }
View Full Code Here

TOP

Related Classes of org.apache.lucene.queryparser.flexible.core.QueryNodeException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.