public Query getQuery(Element e) throws ParserException {
    String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
    String text = DOMUtils.getNonBlankTextOrFail(e);

    BooleanQuery bq = new BooleanQuery(DOMUtils.getAttribute(e, "disableCoord", false));
    bq.setMinimumNumberShouldMatch(DOMUtils.getAttribute(e, "minimumNumberShouldMatch", 0));
    try {
      TokenStream ts = analyzer.tokenStream(fieldName, new StringReader(text));
      TermToBytesRefAttribute termAtt = ts.addAttribute(TermToBytesRefAttribute.class);
      Term term = null;
      BytesRef bytes = termAtt.getBytesRef();
      ts.reset();
      while (ts.incrementToken()) {
        termAtt.fillBytesRef();
        term = new Term(fieldName, BytesRef.deepCopyOf(bytes));
        bq.add(new BooleanClause(new TermQuery(term), BooleanClause.Occur.SHOULD));
      }
      ts.end();
      ts.close();
    } catch (IOException ioe) {
      throw new RuntimeException("Error constructing terms from index:" + ioe);
    }

    bq.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f));
    return bq;
  }
  @Override
  public Filter getFilter(Element e) throws ParserException {
    String field = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
    String lowerTerm = DOMUtils.getAttributeOrFail(e, "lowerTerm");
    String upperTerm = DOMUtils.getAttributeOrFail(e, "upperTerm");
    boolean lowerInclusive = DOMUtils.getAttribute(e, "includeLower", true);
    boolean upperInclusive = DOMUtils.getAttribute(e, "includeUpper", true);
    int precisionStep =
        DOMUtils.getAttribute(e, "precisionStep", NumericUtils.PRECISION_STEP_DEFAULT);

    String type = DOMUtils.getAttribute(e, "type", "int");
    try {
      Filter filter;
      if (type.equalsIgnoreCase("int")) {
        filter =
            NumericRangeFilter.newIntRange(
                field,
                precisionStep,
                Integer.valueOf(lowerTerm),
                Integer.valueOf(upperTerm),
                lowerInclusive,
                upperInclusive);
      } else if (type.equalsIgnoreCase("long")) {
        filter =
            NumericRangeFilter.newLongRange(
                field,
                precisionStep,
                Long.valueOf(lowerTerm),
                Long.valueOf(upperTerm),
                lowerInclusive,
                upperInclusive);
      } else if (type.equalsIgnoreCase("double")) {
        filter =
            NumericRangeFilter.newDoubleRange(
                field,
                precisionStep,
                Double.valueOf(lowerTerm),
                Double.valueOf(upperTerm),
                lowerInclusive,
                upperInclusive);
      } else if (type.equalsIgnoreCase("float")) {
        filter =
            NumericRangeFilter.newFloatRange(
                field,
                precisionStep,
                Float.valueOf(lowerTerm),
                Float.valueOf(upperTerm),
                lowerInclusive,
                upperInclusive);
      } else {
        throw new ParserException("type attribute must be one of: [long, int, double, float]");
      }
      return filter;
    } catch (NumberFormatException nfe) {
      if (strictMode) {
        throw new ParserException("Could not parse lowerTerm or upperTerm into a number", nfe);
      }
      return NO_MATCH_FILTER;
    }
  }
  public SpanQuery getSpanQuery(Element e) throws ParserException {
    String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
    String value = DOMUtils.getNonBlankTextOrFail(e);
    SpanTermQuery stq = new SpanTermQuery(new Term(fieldName, value));

    stq.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f));
    return stq;
  }
Esempio n. 4
0
  @Override
  public SpanQuery getSpanQuery(Element e) throws ParserException {
    String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
    String value = DOMUtils.getNonBlankTextOrFail(e);
    SpanTermQuery stq = new SpanTermQuery(new Term(fieldName, value));

    float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
    return new SpanBoostQuery(stq, boost);
  }
Esempio n. 5
0
 @Override
 public Query getQuery(Element e) throws ParserException {
   String field = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
   String value = DOMUtils.getNonBlankTextOrFail(e);
   Query tq = new TermQuery(new Term(field, value));
   float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
   if (boost != 1f) {
     tq = new BoostQuery(tq, boost);
   }
   return tq;
 }
  @Override
  public SpanQuery getSpanQuery(Element e) throws ParserException {
    String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
    String value = DOMUtils.getNonBlankTextOrFail(e);

    List<SpanQuery> clausesList = new ArrayList<>();

    try (TokenStream ts = analyzer.tokenStream(fieldName, value)) {
      TermToBytesRefAttribute termAtt = ts.addAttribute(TermToBytesRefAttribute.class);
      ts.reset();
      while (ts.incrementToken()) {
        SpanTermQuery stq =
            new SpanTermQuery(new Term(fieldName, BytesRef.deepCopyOf(termAtt.getBytesRef())));
        clausesList.add(stq);
      }
      ts.end();
      SpanOrQuery soq = new SpanOrQuery(clausesList.toArray(new SpanQuery[clausesList.size()]));
      float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
      return new SpanBoostQuery(soq, boost);
    } catch (IOException ioe) {
      throw new ParserException("IOException parsing value:" + value);
    }
  }