public SpanTermQuery build(QueryNode node) throws QueryNodeException {
    FieldQueryNode fieldQueryNode = (FieldQueryNode) node;

    return new SpanTermQuery(
        new Term(fieldQueryNode.getFieldAsString(), fieldQueryNode.getTextAsString()));
  }
  @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;
  }
  @Override
  protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {

    if (node instanceof TermRangeQueryNode) {
      QueryConfigHandler config = getQueryConfigHandler();

      if (config != null) {
        TermRangeQueryNode termRangeNode = (TermRangeQueryNode) node;
        FieldConfig fieldConfig =
            config.getFieldConfig(StringUtils.toString(termRangeNode.getField()));

        if (fieldConfig != null) {

          NumericConfig numericConfig = fieldConfig.get(ConfigurationKeys.NUMERIC_CONFIG);

          if (numericConfig != null) {

            FieldQueryNode lower = termRangeNode.getLowerBound();
            FieldQueryNode upper = termRangeNode.getUpperBound();

            String lowerText = lower.getTextAsString();
            String upperText = upper.getTextAsString();
            NumberFormat numberFormat = numericConfig.getNumberFormat();
            Number lowerNumber = null, upperNumber = null;

            if (lowerText.length() > 0) {

              try {
                lowerNumber = numberFormat.parse(lowerText);

              } catch (ParseException e) {
                throw new QueryNodeParseException(
                    new MessageImpl(
                        QueryParserMessages.COULD_NOT_PARSE_NUMBER,
                        lower.getTextAsString(),
                        numberFormat.getClass().getCanonicalName()),
                    e);
              }
            }

            if (upperText.length() > 0) {

              try {
                upperNumber = numberFormat.parse(upperText);

              } catch (ParseException e) {
                throw new QueryNodeParseException(
                    new MessageImpl(
                        QueryParserMessages.COULD_NOT_PARSE_NUMBER,
                        upper.getTextAsString(),
                        numberFormat.getClass().getCanonicalName()),
                    e);
              }
            }

            switch (numericConfig.getType()) {
              case LONG:
                if (upperNumber != null) upperNumber = upperNumber.longValue();
                if (lowerNumber != null) lowerNumber = lowerNumber.longValue();
                break;
              case INT:
                if (upperNumber != null) upperNumber = upperNumber.intValue();
                if (lowerNumber != null) lowerNumber = lowerNumber.intValue();
                break;
              case DOUBLE:
                if (upperNumber != null) upperNumber = upperNumber.doubleValue();
                if (lowerNumber != null) lowerNumber = lowerNumber.doubleValue();
                break;
              case FLOAT:
                if (upperNumber != null) upperNumber = upperNumber.floatValue();
                if (lowerNumber != null) lowerNumber = lowerNumber.floatValue();
            }

            NumericQueryNode lowerNode =
                new NumericQueryNode(termRangeNode.getField(), lowerNumber, numberFormat);
            NumericQueryNode upperNode =
                new NumericQueryNode(termRangeNode.getField(), upperNumber, numberFormat);

            boolean lowerInclusive = termRangeNode.isLowerInclusive();
            boolean upperInclusive = termRangeNode.isUpperInclusive();

            return new NumericRangeQueryNode(
                lowerNode, upperNode, lowerInclusive, upperInclusive, numericConfig);
          }
        }
      }
    }

    return node;
  }