Example usage for org.apache.lucene.util NumericUtils doubleToSortableLong

List of usage examples for org.apache.lucene.util NumericUtils doubleToSortableLong

Introduction

In this page you can find the example usage for org.apache.lucene.util NumericUtils doubleToSortableLong.

Prototype

public static long doubleToSortableLong(double value) 

Source Link

Document

Converts a double value to a sortable signed long.

Usage

From source file:IndexTaxis.java

License:Apache License

static void addOneField(Document doc, String fieldName, String rawValue) {
    // nocommit//w ww  .  j  av a  2 s.  co m
    /*
    if (fieldName.equals("pick_up_lat")) {
      double value = Double.parseDouble(rawValue);
      doc.add(new DoublePoint(fieldName, value));
      doc.add(new SortedNumericDocValuesField(fieldName, NumericUtils.doubleToSortableLong(value)));
    }
    */
    switch (fieldName) {
    case "vendor_id":
    case "cab_color":
    case "payment_type":
    case "trip_type":
    case "rate_code":
    case "store_and_fwd_flag":
        doc.add(new StringField(fieldName, rawValue, Field.Store.NO));
        doc.add(new SortedSetDocValuesField(fieldName, new BytesRef(rawValue)));
        break;
    case "vendor_name":
        doc.add(new TextField(fieldName, rawValue, Field.Store.NO));
        break;
    case "pick_up_date_time":
    case "drop_off_date_time": {
        long value = Long.parseLong(rawValue);
        doc.add(new LongPoint(fieldName, value));
        doc.add(new SortedNumericDocValuesField(fieldName, value));
    }
        break;
    case "passenger_count": {
        int value = Integer.parseInt(rawValue);
        doc.add(new IntPoint(fieldName, value));
        doc.add(new SortedNumericDocValuesField(fieldName, value));
    }
        break;
    case "trip_distance":
    case "pick_up_lat":
    case "pick_up_lon":
    case "drop_off_lat":
    case "drop_off_lon":
    case "fare_amount":
    case "surcharge":
    case "mta_tax":
    case "extra":
    case "ehail_fee":
    case "improvement_surcharge":
    case "tip_amount":
    case "tolls_amount":
    case "total_amount": {
        double value;
        try {
            value = Double.parseDouble(rawValue);
        } catch (NumberFormatException nfe) {
            System.out.println(
                    "WARNING: failed to parse \"" + rawValue + "\" as double for field \"" + fieldName + "\"");
            return;
        }
        doc.add(new DoublePoint(fieldName, value));
        doc.add(new SortedNumericDocValuesField(fieldName, NumericUtils.doubleToSortableLong(value)));
    }
        break;
    default:
        throw new AssertionError("failed to handle field \"" + fieldName + "\"");
    }
}

From source file:com.github.flaxsearch.util.BytesRefUtils.java

License:Apache License

private static Function<String, BytesRef> getDecoder(String type) {
    switch (type.toLowerCase(Locale.ROOT)) {
    case "base64":
        return s -> new BytesRef(Base64.getUrlDecoder().decode(s.getBytes(Charset.defaultCharset())));
    case "utf8":
        return BytesRef::new;
    case "int":
        return s -> {
            BytesRefBuilder builder = new BytesRefBuilder();
            LegacyNumericUtils.intToPrefixCoded(Integer.parseInt(s), 0, builder);
            return builder.get();
        };//from   w w  w .  j  av a  2  s.  c  o m
    case "long":
        return s -> {
            BytesRefBuilder builder = new BytesRefBuilder();
            LegacyNumericUtils.longToPrefixCoded(Long.parseLong(s), 0, builder);
            return builder.get();
        };
    case "float":
        return s -> {
            BytesRefBuilder builder = new BytesRefBuilder();
            LegacyNumericUtils.intToPrefixCoded(NumericUtils.floatToSortableInt(Float.parseFloat(s)), 0,
                    builder);
            return builder.get();
        };
    case "double":
        return s -> {
            BytesRefBuilder builder = new BytesRefBuilder();
            LegacyNumericUtils.longToPrefixCoded(NumericUtils.doubleToSortableLong(Double.parseDouble(s)), 0,
                    builder);
            return builder.get();
        };
    default:
        throw new IllegalArgumentException("Unknown decoder type: " + type);
    }
}

From source file:com.querydsl.lucene4.LuceneSerializer.java

License:Apache License

private BytesRef convertNumber(Number number) {
    if (Integer.class.isInstance(number) || Byte.class.isInstance(number) || Short.class.isInstance(number)) {
        BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT);
        NumericUtils.intToPrefixCoded(number.intValue(), 0, bytes);
        return bytes;
    } else if (Double.class.isInstance(number) || BigDecimal.class.isInstance(number)) {
        BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_LONG);
        long l = NumericUtils.doubleToSortableLong(number.doubleValue());
        NumericUtils.longToPrefixCoded(l, 0, bytes);
        return bytes;
    } else if (Long.class.isInstance(number) || BigInteger.class.isInstance(number)) {
        BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_LONG);
        NumericUtils.longToPrefixCoded(number.longValue(), 0, bytes);
        return bytes;
    } else if (Float.class.isInstance(number)) {
        BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT);
        int i = NumericUtils.floatToSortableInt(number.floatValue());
        NumericUtils.intToPrefixCoded(i, 0, bytes);
        return bytes;
    } else {//www. j  a  va2s.  c o  m
        throw new IllegalArgumentException("Unsupported numeric type " + number.getClass().getName());
    }
}

From source file:com.querydsl.lucene5.LuceneSerializer.java

License:Apache License

private BytesRef convertNumber(Number number) {
    if (Integer.class.isInstance(number) || Byte.class.isInstance(number) || Short.class.isInstance(number)) {
        BytesRefBuilder ref = new BytesRefBuilder();
        NumericUtils.intToPrefixCoded(number.intValue(), 0, ref);
        return ref.get();
    } else if (Double.class.isInstance(number) || BigDecimal.class.isInstance(number)) {
        BytesRefBuilder ref = new BytesRefBuilder();
        long l = NumericUtils.doubleToSortableLong(number.doubleValue());
        NumericUtils.longToPrefixCoded(l, 0, ref);
        return ref.get();
    } else if (Long.class.isInstance(number) || BigInteger.class.isInstance(number)) {
        BytesRefBuilder ref = new BytesRefBuilder();
        NumericUtils.longToPrefixCoded(number.longValue(), 0, ref);
        return ref.get();
    } else if (Float.class.isInstance(number)) {
        BytesRefBuilder ref = new BytesRefBuilder();
        int i = NumericUtils.floatToSortableInt(number.floatValue());
        NumericUtils.intToPrefixCoded(i, 0, ref);
        return ref.get();
    } else {/* w  ww  .  j a v a2s  .  c o m*/
        throw new IllegalArgumentException("Unsupported numeric type " + number.getClass().getName());
    }
}

From source file:com.qwazr.search.field.SortedDoubleDocValuesType.java

License:Apache License

@Override
final public void fillValue(final Object value, final FieldConsumer consumer) {
    if (value instanceof Number)
        consumer.accept(new SortedNumericDocValuesField(fieldName,
                NumericUtils.doubleToSortableLong(((Number) value).doubleValue())));
    else//from w  w  w .  jav a 2 s.co  m
        consumer.accept(new SortedNumericDocValuesField(fieldName,
                NumericUtils.doubleToSortableLong(Double.parseDouble(value.toString()))));

}

From source file:com.qwazr.search.index.BytesRefUtils.java

License:Apache License

final static public BytesRef from(final Double value) {
    if (value == null)
        return new BytesRef(BytesRef.EMPTY_BYTES);
    final BytesRefBuilder builder = new BytesRefBuilder();
    NumericUtils.longToPrefixCoded(NumericUtils.doubleToSortableLong(value), 0, builder);
    return builder.toBytesRef();
}

From source file:com.vmware.xenon.services.common.LuceneDocumentIndexService.java

License:Open Source License

public static Document addNumericField(Document doc, String propertyName, double propertyValue,
        boolean stored) {
    long longPropertyValue = NumericUtils.doubleToSortableLong(propertyValue);

    // StoredField is used if the property needs to be stored in the lucene document
    if (stored) {
        doc.add(new StoredField(propertyName, propertyValue));
    }/*  w w w  . jav a 2s. c o  m*/

    // DoublePoint adds an index field to the document that allows for efficient search
    // and range queries
    doc.add(new DoublePoint(propertyName, propertyValue));

    // NumericDocValues allow for efficient group operations for a property.
    // TODO Investigate and revert code to use 'sort' to determine the type of DocValuesField
    doc.add(new NumericDocValuesField(propertyName, longPropertyValue));
    return doc;
}

From source file:com.vmware.xenon.services.common.LuceneIndexDocumentHelper.java

License:Open Source License

private void addNumericField(String propertyName, double propertyValue, boolean stored,
        boolean isCollectionItem, boolean sorted) {
    long longPropertyValue = NumericUtils.doubleToSortableLong(propertyValue);

    if (stored) {
        Field field = isCollectionItem ? new StoredField(propertyName, propertyValue)
                : getAndSetStoredField(propertyName, propertyValue);
        this.doc.add(field);
    }//from   w  w  w  .ja  v a 2s .  c o  m

    // DoublePoint adds an index field to the document that allows for efficient search
    // and range queries
    if (isCollectionItem) {
        this.doc.add(new DoublePoint(propertyName, propertyValue));
    } else {
        DoublePoint dpField = this.doublePointFields.computeIfAbsent(propertyName, (k) -> {
            return new DoublePoint(propertyName, propertyValue);
        });
        dpField.setDoubleValue(propertyValue);
        this.doc.add(dpField);
    }

    NumericDocValuesField ndField = getAndSetNumericField(propertyName, longPropertyValue, isCollectionItem);
    this.doc.add(ndField);

    if (sorted) {
        // special handling for groupBy queries
        Field sdField = getAndSetSortedStoredField(propertyName + GROUP_BY_PROPERTY_NAME_SUFFIX,
                Double.toString(propertyValue));
        this.doc.add(sdField);
    }

}

From source file:io.crate.expression.reference.doc.DoubleColumnReferenceTest.java

License:Apache License

@Override
protected void insertValues(IndexWriter writer) throws Exception {
    for (double d = 0.5; d < 10.0d; d++) {
        Document doc = new Document();
        doc.add(new SortedNumericDocValuesField(column, NumericUtils.doubleToSortableLong(d)));
        writer.addDocument(doc);// w w  w. j  a v a 2  s .  co m
    }
}

From source file:org.apache.solr.legacy.BBoxStrategy.java

License:Apache License

private Query makeNumberTermQuery(String field, double number) {
    if (hasPointVals) {
        return DoublePoint.newExactQuery(field, number);
    } else if (legacyNumericFieldType != null) {
        BytesRefBuilder bytes = new BytesRefBuilder();
        LegacyNumericUtils.longToPrefixCoded(NumericUtils.doubleToSortableLong(number), 0, bytes);
        return new TermQuery(new Term(field, bytes.get()));
    }// ww  w.j a  v a2  s .  c o m
    throw new UnsupportedOperationException("An index is required for this operation.");
}