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

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


  }
 
  @Override
  public TermRangeQuery build(QueryNode queryNode) throws QueryNodeException {
    TermRangeQueryNode rangeNode = (TermRangeQueryNode) queryNode;
    FieldQueryNode upper = rangeNode.getUpperBound();
    FieldQueryNode lower = rangeNode.getLowerBound();
   
    String field = StringUtils.toString(rangeNode.getField());
    String lowerText = lower.getTextAsString();
    String upperText = upper.getTextAsString();
   
    if (lowerText.length() == 0) {
      lowerText = null;
    }
View Full Code Here


    if (children != null) {
      TreeMap<Integer, List<Term>> positionTermMap = new TreeMap<>();

      for (QueryNode child : children) {
        FieldQueryNode termNode = (FieldQueryNode) child;
        TermQuery termQuery = (TermQuery) termNode
            .getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
        List<Term> termList = positionTermMap.get(termNode
            .getPositionIncrement());

        if (termList == null) {
          termList = new LinkedList<>();
          positionTermMap.put(termNode.getPositionIncrement(), termList);

        }

        termList.add(termQuery.getTerm());
View Full Code Here

    // empty constructor
  }

  @Override
  public TermQuery build(QueryNode queryNode) throws QueryNodeException {
    FieldQueryNode fieldNode = (FieldQueryNode) queryNode;

    return new TermQuery(new Term(fieldNode.getFieldAsString(), fieldNode
        .getTextAsString()));

  }
View Full Code Here

*/
public class SpanTermQueryNodeBuilder implements StandardQueryBuilder {

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

    return new SpanTermQuery(new Term(fieldQueryNode.getFieldAsString(),
        fieldQueryNode.getTextAsString()));

  }
View Full Code Here

  @Override
  protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
   
    if (node instanceof TermRangeQueryNode) {
      TermRangeQueryNode rangeNode = (TermRangeQueryNode) node;
      FieldQueryNode lowerNode = rangeNode.getLowerBound();
      FieldQueryNode upperNode = rangeNode.getUpperBound();
      CharSequence lowerText = lowerNode.getText();
      CharSequence upperText = upperNode.getText();
     
      if (OPEN_RANGE_TOKEN.equals(upperNode.getTextAsString())
          && (!(upperText instanceof UnescapedCharSequence) || !((UnescapedCharSequence) upperText)
              .wasEscaped(0))) {
        upperText = "";
      }
     
      if (OPEN_RANGE_TOKEN.equals(lowerNode.getTextAsString())
          && (!(lowerText instanceof UnescapedCharSequence) || !((UnescapedCharSequence) lowerText)
              .wasEscaped(0))) {
        lowerText = "";
      }
     
      lowerNode.setText(lowerText);
      upperNode.setText(upperText);
    }
   
    return node;
   
  }
View Full Code Here

        && !(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();

      CachingTokenFilter buffer = null;
      PositionIncrementAttribute posIncrAtt = null;
      int numTokens = 0;
      int positionCount = 0;
      boolean severalTokensAtSamePosition = false;
     
      TokenStream source = null;
      try {
        source = this.analyzer.tokenStream(field, text);
        source.reset();
        buffer = new CachingTokenFilter(source);

        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
        }
      } catch (IOException e) {
        throw new RuntimeException(e);
      } finally {
        IOUtils.closeWhileHandlingException(source);
      }
     
      // rewind the buffer stream
      buffer.reset();

      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:
         
          if (positionCount == 1) {
            // simple case: only one position, with synonyms
            LinkedList<QueryNode> children = new LinkedList<>();
           
            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 {
            // multiple positions
            QueryNode q = new StandardBooleanQueryNode(Collections.<QueryNode>emptyList(),false);
            QueryNode currentQuery = null;
            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
              }
              if (posIncrAtt != null && posIncrAtt.getPositionIncrement() == 0) {
                if (!(currentQuery instanceof BooleanQueryNode)) {
                  QueryNode t = currentQuery;
                  currentQuery = new StandardBooleanQueryNode(Collections.<QueryNode>emptyList(), true);
                  ((BooleanQueryNode)currentQuery).add(t);
                }
                ((BooleanQueryNode)currentQuery).add(new FieldQueryNode(field, term, -1, -1));
              } else {
                if (currentQuery != null) {
                  if (this.defaultOperator == Operator.OR) {
                    q.add(currentQuery);
                  } else {
                    q.add(new ModifierQueryNode(currentQuery, Modifier.MOD_REQ));
                  }
                }
                currentQuery = new FieldQueryNode(field, term, -1, -1);
              }
            }
            if (this.defaultOperator == Operator.OR) {
              q.add(currentQuery);
            } else {
              q.add(new ModifierQueryNode(currentQuery, Modifier.MOD_REQ));
            }
           
            if (q instanceof BooleanQueryNode) {
              q = new GroupQueryNode(q);
            }
            return q;
          }
        } else {
          // phrase query:
          MultiPhraseQueryNode mpq = new MultiPhraseQueryNode();

          List<FieldQueryNode> multiTerms = new ArrayList<>();
          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);

        }
View Full Code Here

 
  @Test
  public void testSetFieldBuilder() throws QueryNodeException {
    QueryTreeBuilder qtb = new QueryTreeBuilder();
    qtb.setBuilder("field", new DummyBuilder());
    Object result = qtb.build(new FieldQueryNode(new UnescapedCharSequence("field"), "foo", 0, 0));
    Assert.assertEquals("OK", result);
   
    qtb = new QueryTreeBuilder();
    qtb.setBuilder(DummyQueryNodeInterface.class, new DummyBuilder());
    result = qtb.build(new DummyQueryNode());
View Full Code Here

        && !(node.getParent() instanceof RangeQueryNode)) {
     
      QueryConfigHandler config = getQueryConfigHandler();
     
      if (config != null) {
        FieldQueryNode fieldNode = (FieldQueryNode) node;
        FieldConfig fieldConfig = config.getFieldConfig(fieldNode
            .getFieldAsString());
       
        if (fieldConfig != null) {
          NumericConfig numericConfig = fieldConfig
              .get(ConfigurationKeys.NUMERIC_CONFIG);
         
          if (numericConfig != null) {
           
            NumberFormat numberFormat = numericConfig.getNumberFormat();
            String text = fieldNode.getTextAsString();
            Number number = null;
           
            if (text.length() > 0) {
             
              try {
                number = numberFormat.parse(text);
               
              } catch (ParseException e) {
                throw new QueryNodeParseException(new MessageImpl(
                    QueryParserMessages.COULD_NOT_PARSE_NUMBER, fieldNode
                        .getTextAsString(), numberFormat.getClass()
                        .getCanonicalName()), e);
              }
             
              switch (numericConfig.getType()) {
                case LONG:
                  number = number.longValue();
                  break;
                case INT:
                  number = number.intValue();
                  break;
                case DOUBLE:
                  number = number.doubleValue();
                  break;
                case FLOAT:
                  number = number.floatValue();
              }
             
            } else {
              throw new QueryNodeParseException(new MessageImpl(
                  QueryParserMessages.NUMERIC_CANNOT_BE_EMPTY, fieldNode.getFieldAsString()));
            }
           
            NumericQueryNode lowerNode = new NumericQueryNode(fieldNode
                .getField(), number, numberFormat);
            NumericQueryNode upperNode = new NumericQueryNode(fieldNode
                .getField(), number, numberFormat);
           
            return new NumericRangeQueryNode(lowerNode, upperNode, true, true,
                numericConfig);
           
View Full Code Here

  @Override
  protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
   
    if (node instanceof TermRangeQueryNode) {
      TermRangeQueryNode termRangeNode = (TermRangeQueryNode) node;
      FieldQueryNode upper = termRangeNode.getUpperBound();
      FieldQueryNode lower = termRangeNode.getLowerBound();
     
      DateTools.Resolution dateRes = null;
      boolean inclusive = false;
      Locale locale = getQueryConfigHandler().get(ConfigurationKeys.LOCALE);
     
      if (locale == null) {
        locale = Locale.getDefault();
      }
     
      TimeZone timeZone = getQueryConfigHandler().get(ConfigurationKeys.TIMEZONE);
     
      if (timeZone == null) {
        timeZone = TimeZone.getDefault();
      }
     
      CharSequence field = termRangeNode.getField();
      String fieldStr = null;
     
      if (field != null) {
        fieldStr = field.toString();
      }
     
      FieldConfig fieldConfig = getQueryConfigHandler()
          .getFieldConfig(fieldStr);
     
      if (fieldConfig != null) {
        dateRes = fieldConfig.get(ConfigurationKeys.DATE_RESOLUTION);
      }
     
      if (termRangeNode.isUpperInclusive()) {
        inclusive = true;
      }
     
      String part1 = lower.getTextAsString();
      String part2 = upper.getTextAsString();
     
      try {
        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
        df.setLenient(true);
       
        if (part1.length() > 0) {
          Date d1 = df.parse(part1);
          part1 = DateTools.dateToString(d1, dateRes);
          lower.setText(part1);
        }
       
        if (part2.length() > 0) {
          Date d2 = df.parse(part2);
          if (inclusive) {
View Full Code Here

  @Override
  protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {

    if (node instanceof FieldQueryNode) {
      FieldQueryNode fqn = (FieldQueryNode) node;

      if (fqn.getField().toString().equals("*")
          && fqn.getText().toString().equals("*")) {

        return new MatchAllDocsQueryNode();

      }

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.