@Override
  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()
             */ QueryParserMessages.EMPTY_MESSAGE), ex);
          }
        }
      }
    }

    bQuery.setMinimumNumberShouldMatch(andNode.getMinimumMatchingElements());

    return bQuery;
  }
Example #2
0
 /**
  * Return the datatype label associated with the given node. If there is no datatype label
  * associated, returns the default datatype.
  */
 static String getDatatype(QueryConfigHandler config, QueryNode node) {
   // if a datatype has been defined, return it
   if (node.getTag(DatatypeQueryNode.DATATYPE_TAGID) != null) {
     return (String) node.getTag(DatatypeQueryNode.DATATYPE_TAGID);
   }
   // otherwise, return the default datatype
   else {
     return getDefaultDatatype(config);
   }
 }
  public MatchAllDocsQuery build(QueryNode queryNode) throws QueryNodeException {

    // validates node
    if (!(queryNode instanceof MatchAllDocsQueryNode)) {
      throw new QueryNodeException(
          new MessageImpl(
              QueryParserMessages.LUCENE_QUERY_CONVERSION_ERROR,
              queryNode.toQueryString(new EscapeQuerySyntaxImpl()),
              queryNode.getClass().getName()));
    }

    return new MatchAllDocsQuery();
  }
Example #4
0
  @Override
  protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
    final QueryConfigHandler conf = this.getQueryConfigHandler();

    // If the current node is a datatype query node, validate the datatype and assign it to its
    // child
    if (node instanceof DatatypeQueryNode) {
      final Map<String, Analyzer> dtAnalyzers =
          conf.get(KeywordConfigurationKeys.DATATYPES_ANALYZERS);
      final DatatypeQueryNode dt = (DatatypeQueryNode) node;
      String datatype = dt.getDatatype();

      // check if the datatype is correctly registered
      if (dtAnalyzers == null) {
        throw new IllegalArgumentException(
            "KeywordConfigurationKeys.DATAYPES_ANALYZERS "
                + "should be set on the ExtendedKeywordQueryConfigHandler");
      }
      if (!dtAnalyzers.containsKey(datatype)) {
        throw new IllegalArgumentException("Unknown datatype: [" + datatype + "]");
      }
      if (dtAnalyzers.get(datatype) == null) {
        throw new IllegalArgumentException(
            "Analyzer of datatype [" + datatype + "] cannot be null.");
      }

      // transfer the datatype to its child
      dt.getChild().setTag(DatatypeQueryNode.DATATYPE_TAGID, datatype);
    }
    // If the current node is a twig query node, assign the json:field datatype to the root, and
    // assign the default
    // or the tagged datatype to the child
    else if (node instanceof TwigQueryNode) {
      final TwigQueryNode twig = (TwigQueryNode) node;
      if (twig.getTag(DatatypeQueryNode.DATATYPE_TAGID) == null) {
        twig.getChild().setTag(DatatypeQueryNode.DATATYPE_TAGID, this.getDefaultDatatype(conf));
      } else {
        twig.getChild().setTag(DatatypeQueryNode.DATATYPE_TAGID, this.getDatatype(conf, node));
      }
      twig.getRoot().setTag(DatatypeQueryNode.DATATYPE_TAGID, JSONDatatype.JSON_FIELD);
    }
    // in any other cases, if the node is not a leaf node, transfer the datatype to its children
    else if (!node.isLeaf()) {
      for (final QueryNode child : node.getChildren()) {
        child.setTag(DatatypeQueryNode.DATATYPE_TAGID, this.getDatatype(conf, node));
      }
    }

    return node;
  }
  @Override
  protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {

    if (node instanceof BooleanQueryNode) {
      List<QueryNode> children = node.getChildren();

      if (children != null && children.size() == 1) {
        QueryNode child = children.get(0);

        if (child instanceof ModifierQueryNode) {
          ModifierQueryNode modNode = (ModifierQueryNode) child;

          if (modNode instanceof BooleanModifierNode
              || modNode.getModifier() == Modifier.MOD_NONE) {

            return child;
          }

        } else {
          return child;
        }
      }
    }

    return node;
  }
  @Override
  protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {

    if (node instanceof AqpWhiteSpacedQueryNode) {

      QueryConfigHandler config = getQueryConfigHandler();

      if (!config.has(
          AqpAdsabsQueryConfigHandler.ConfigurationKeys.FUNCTION_QUERY_BUILDER_CONFIG)) {
        throw new QueryNodeException(
            new MessageImpl("Invalid configuration", "Missing FunctionQueryBuilder provider"));
      }

      String funcName = getFuncName();
      String subQuery = ((FieldQueryNode) node).getTextAsString();
      String field = ((FieldQueryNode) node).getFieldAsString();
      if (field.equals(
          config.get(AqpAdsabsQueryConfigHandler.ConfigurationKeys.UNFIELDED_SEARCH_FIELD))) {
        field = null;
      }

      if (field != null) {
        subQuery = field + ":" + subQuery;
      }

      if (node.getParent() instanceof SlopQueryNode) {
        subQuery = "(" + subQuery + ")";
        subQuery = subQuery + "~" + ((SlopQueryNode) node.getParent()).getValue();
        if (node.getParent().getParent() instanceof BoostQueryNode) {
          subQuery = subQuery + "^" + ((BoostQueryNode) node.getParent().getParent()).getValue();
        }
      } else if (node.getParent() instanceof BoostQueryNode) {
        subQuery = "(" + subQuery + ")";
        subQuery = subQuery + "^" + ((BoostQueryNode) node.getParent()).getValue();
      }

      node.setTag("subQuery", subQuery);

      AqpFunctionQueryBuilder builder =
          config
              .get(AqpAdsabsQueryConfigHandler.ConfigurationKeys.FUNCTION_QUERY_BUILDER_CONFIG)
              .getBuilder(funcName, (QueryNode) node, config);

      if (builder == null) {
        throw new QueryNodeException(
            new MessageImpl(
                QueryParserMessages.INVALID_SYNTAX, "Unknown function: \"" + funcName + "\""));
      }

      List<OriginalInput> fValues = new ArrayList<OriginalInput>();
      fValues.add(
          new OriginalInput(
              subQuery,
              ((AqpWhiteSpacedQueryNode) node).getBegin(),
              ((AqpWhiteSpacedQueryNode) node).getEnd()));
      return new AqpFunctionQueryNode(funcName, builder, fValues);
    }
    return node;
  }
  @Override
  protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {

    if (node instanceof AqpANTLRNode && ((AqpANTLRNode) node).getTokenLabel().equals("TMODIFIER")) {

      List<QueryNode> children = node.getChildren();

      if (children.size() == 1) {
        return children.get(0);
      }

      QueryNode masterChild = null;
      QueryNode currentChild;
      List<QueryNode> currentChildren;

      for (int i = 0; i < children.size(); i++) {
        currentChild = children.get(i);
        if (currentChild.isLeaf()) {
          continue;
        }
        if (masterChild == null) {
          masterChild = currentChild;
          node = masterChild;
          continue;
        }
        currentChildren = masterChild.getChildren();
        currentChildren.add(currentChild);
        masterChild.set(currentChildren);
        masterChild = children.get(i);
      }

      return node;
    }
    return node;
  }
  @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;
      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
            }

            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;
  }