protected <N extends Number> NumericRangeQuery<?> numericRange(
     Class<N> clazz,
     String field,
     @Nullable N min,
     @Nullable N max,
     boolean minInc,
     boolean maxInc) {
   if (clazz.equals(Integer.class)) {
     return NumericRangeQuery.newIntRange(field, (Integer) min, (Integer) max, minInc, maxInc);
   } else if (clazz.equals(Double.class)) {
     return NumericRangeQuery.newDoubleRange(field, (Double) min, (Double) max, minInc, minInc);
   } else if (clazz.equals(Float.class)) {
     return NumericRangeQuery.newFloatRange(field, (Float) min, (Float) max, minInc, minInc);
   } else if (clazz.equals(Long.class)) {
     return NumericRangeQuery.newLongRange(field, (Long) min, (Long) max, minInc, minInc);
   } else if (clazz.equals(Byte.class) || clazz.equals(Short.class)) {
     return NumericRangeQuery.newIntRange(
         field,
         min != null ? min.intValue() : null,
         max != null ? max.intValue() : null,
         minInc,
         maxInc);
   } else {
     throw new IllegalArgumentException("Unsupported numeric type " + clazz.getName());
   }
 }
 @Override
 public Query fuzzyQuery(
     String value,
     Fuzziness fuzziness,
     int prefixLength,
     int maxExpansions,
     boolean transpositions) {
   double iValue = Double.parseDouble(value);
   double iSim = fuzziness.asDouble();
   return NumericRangeQuery.newDoubleRange(
       names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true);
 }
 @Override
 public Query rangeQuery(
     Object lowerTerm,
     Object upperTerm,
     boolean includeLower,
     boolean includeUpper,
     @Nullable QueryParseContext context) {
   return NumericRangeQuery.newDoubleRange(
       names.indexName(),
       precisionStep,
       lowerTerm == null ? null : parseDoubleValue(lowerTerm),
       upperTerm == null ? null : parseDoubleValue(upperTerm),
       includeLower,
       includeUpper);
 }
Example #4
0
  // loading promotional products
  public List<Products> extractPromotionalProducts() {

    FullTextEntityManager fullTextEntityManager =
        org.hibernate.search.jpa.Search.getFullTextEntityManager(em);

    org.apache.lucene.search.Query query =
        NumericRangeQuery.newDoubleRange("old_price", 0.0d, 1000d, false, true);
    FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query, Products.class);
    Sort sort = new Sort(new SortField("price", SortField.DOUBLE));
    fullTextQuery.setSort(sort);

    fullTextQuery.initializeObjectsWith(
        ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID);

    List results = fullTextQuery.getResultList();

    return results;
  }
 @Override
 public Query getRangeQuery(String field, String part1, String part2, boolean inclusive)
     throws ParseException {
   TermRangeQuery query =
       (TermRangeQuery) // A
           super.getRangeQuery(
               field, part1, part2, // A
               inclusive); // A
   if ("price".equals(field)) {
     return NumericRangeQuery.newDoubleRange( // B
         "price", // B
         Double.parseDouble( // B
             query.getLowerTerm()), // B
         Double.parseDouble( // B
             query.getUpperTerm()), // B
         query.includesLower(), // B
         query.includesUpper()); // B
   } else {
     return query; // C
   }
 }
 @Override
 public Query termQuery(Object value, @Nullable QueryParseContext context) {
   double dValue = parseDoubleValue(value);
   return NumericRangeQuery.newDoubleRange(
       names.indexName(), precisionStep, dValue, dValue, true, true);
 }
Example #7
0
  @Override
  public Query getRangeQuery(
      QParser parser,
      SchemaField field,
      String min,
      String max,
      boolean minInclusive,
      boolean maxInclusive) {
    int ps = precisionStep;
    Query query = null;
    switch (type) {
      case INTEGER:
        query =
            NumericRangeQuery.newIntRange(
                field.getName(),
                ps,
                min == null ? null : Integer.parseInt(min),
                max == null ? null : Integer.parseInt(max),
                minInclusive,
                maxInclusive);
        break;
      case FLOAT:
        query =
            NumericRangeQuery.newFloatRange(
                field.getName(),
                ps,
                min == null ? null : Float.parseFloat(min),
                max == null ? null : Float.parseFloat(max),
                minInclusive,
                maxInclusive);
        break;
      case LONG:
        query =
            NumericRangeQuery.newLongRange(
                field.getName(),
                ps,
                min == null ? null : Long.parseLong(min),
                max == null ? null : Long.parseLong(max),
                minInclusive,
                maxInclusive);
        break;
      case DOUBLE:
        query =
            NumericRangeQuery.newDoubleRange(
                field.getName(),
                ps,
                min == null ? null : Double.parseDouble(min),
                max == null ? null : Double.parseDouble(max),
                minInclusive,
                maxInclusive);
        break;
      case DATE:
        query =
            NumericRangeQuery.newLongRange(
                field.getName(),
                ps,
                min == null ? null : dateField.parseMath(null, min).getTime(),
                max == null ? null : dateField.parseMath(null, max).getTime(),
                minInclusive,
                maxInclusive);
        break;
      default:
        throw new SolrException(
            SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field");
    }

    return query;
  }