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

Examples of org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode


   * Return <code>true</code> if the {@link QueryNode} is a {@link FieldQueryNode},
   * which text is equal to "*".
   */
  private boolean isEmptyNode(final QueryNode q) {
    if (q instanceof FieldQueryNode) {
      final FieldQueryNode fq = (FieldQueryNode) q;
      final CharSequence text = fq.getText();
      if (text != null && text.length() == 1 && text.charAt(0) == '*') {
        return true;
      }
    }
    return false;
View Full Code Here


    && !(node instanceof WildcardQueryNode)
    && !(node instanceof FuzzyQueryNode)
    && !(node instanceof RegexpQueryNode)
    && !(node.getParent() instanceof RangeQueryNode)) {

      final FieldQueryNode fieldNode = ((FieldQueryNode) node);
      final String text = fieldNode.getTextAsString();
      final String field = fieldNode.getFieldAsString();

      final TokenStream source;
      try {
        source = this.analyzer.tokenStream(field, new StringReader(text));
        source.reset();
      } catch (final IOException e1) {
        throw new RuntimeException(e1);
      }
      final CachingTokenFilter buffer = new CachingTokenFilter(source);

      int numTokens = 0;
      try {
        while (buffer.incrementToken()) {
          numTokens++;
        }
      } 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) {
        return new NoTokenFoundQueryNode();
      } else if (numTokens != 1) {
        // phrase query
        final TokenizedPhraseQueryNode pq = new TokenizedPhraseQueryNode();

        for (int i = 0; i < numTokens; i++) {
          String term = null;

          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);
          newFieldNode.setPositionIncrement(i);
          pq.add(newFieldNode);
        }
        return pq;
      }
    }
View Full Code Here

      final Analyzer analyzer = dts.get(node.getTag(DatatypeQueryNode.DATATYPE_TAGID));

      if (analyzer instanceof NumericAnalyzer) {
        final NumericAnalyzer na = (NumericAnalyzer) analyzer;

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

        final char[] lowerText = lower.getTextAsString().toCharArray();
        final char[] upperText = upper.getTextAsString().toCharArray();

        final NumericParser<?> parser = na.getNumericParser();
        final Number lowerNumber;
        try {
          if (lowerText.length == 0) { // open bound
View Full Code Here

      final Boolean positionIncrementsEnabled = this.getQueryConfigHandler().get(ConfigurationKeys.ENABLE_POSITION_INCREMENTS);
      if (positionIncrementsEnabled != null) {
          this.positionIncrementsEnabled = positionIncrementsEnabled;
      }

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

  protected QueryNode postProcessNode(final QueryNode node)
  throws QueryNodeException {

    if (node instanceof FieldQueryNode &&
        !(node.getParent() instanceof RangeQueryNode)) {
      final FieldQueryNode fieldNode = (FieldQueryNode) node;

      final Map<String, Analyzer> dts = this.getQueryConfigHandler().get(KeywordConfigurationKeys.DATATYPES_ANALYZERS);
      final Analyzer analyzer = dts.get(node.getTag(DatatypeQueryNode.DATATYPE_TAGID));

      if (analyzer instanceof NumericAnalyzer) {
        final NumericAnalyzer na = (NumericAnalyzer) analyzer;
        final char[] text = fieldNode.getTextAsString().toCharArray();
        final ReusableCharArrayReader textReader = new ReusableCharArrayReader(text);
        final Number number;
        try {
          number = na.getNumericParser().parse(textReader);
        } catch (final Exception e) {
          throw new QueryNodeParseException(new MessageImpl(QueryParserMessages.COULD_NOT_PARSE_NUMBER, text), e);
        }

        final CharSequence field = fieldNode.getField();
        final NodeNumericQueryNode lowerNode = new NodeNumericQueryNode(field, number);
        final NodeNumericQueryNode upperNode = new NodeNumericQueryNode(field, number);

        return new NodeNumericRangeQueryNode(lowerNode, upperNode, true, true, na);
      }
View Full Code Here

      }
      @Override
      protected QueryNode preProcessNode(final QueryNode node)
      throws QueryNodeException {
        if (node instanceof TwigQueryNode) {
          node.add(new FieldQueryNode("field", "text", 0, 4));
        }
        return node;
      }
      @Override
      protected QueryNode postProcessNode(final QueryNode node)
View Full Code Here

  @Test
  public void testTwigQueryNodeParent()
  throws Exception {
    final TwigQueryNode twig = new TwigQueryNode(new WildcardNodeQueryNode(),
                                                 new WildcardNodeQueryNode());
    final FieldQueryNode term = new FieldQueryNode("field", "term", 0, 4);
    assertTrue(term.getParent() == null);
    assertEquals(twig, twig.getRoot().getParent());
    assertEquals(twig, twig.getChild().getParent());
    twig.setRoot(term);
    twig.setChild(term);
    assertEquals(twig, twig.getRoot().getParent());
View Full Code Here

  public NodeTermRangeQueryNodeBuilder() {
  }

  public NodeTermRangeQuery build(final QueryNode queryNode) throws QueryNodeException {
    final TermRangeQueryNode rangeNode = (TermRangeQueryNode) queryNode;
    final FieldQueryNode upper = rangeNode.getUpperBound();
    final FieldQueryNode lower = rangeNode.getLowerBound();

    final String field = StringUtils.toString(rangeNode.getField());
    String lowerText = lower.getTextAsString();
    String upperText = upper.getTextAsString();

    if (lowerText.length() == 0) {
      lowerText = null;
    }
View Full Code Here

  public FieldQueryNodeBuilder() {
  }

  public NodeTermQuery build(QueryNode queryNode) throws QueryNodeException {
    final FieldQueryNode fieldNode = (FieldQueryNode) queryNode;
   
    return new NodeTermQuery(new Term(fieldNode.getFieldAsString(), fieldNode
        .getTextAsString()));
  }
View Full Code Here

    if (children != null) {
      for (final QueryNode child : children) {
        final NodeTermQuery termQuery = (NodeTermQuery) child
            .getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
        final FieldQueryNode termNode = (FieldQueryNode) child;
        phraseQuery.add(termQuery.getTerm(), termNode.getPositionIncrement());
      }
    }
    return phraseQuery;
  }
View Full Code Here

TOP

Related Classes of org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode

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.