Java Code Examples for org.apache.lucene.util.NumericUtils#sortableLongToDouble()

The following examples show how to use org.apache.lucene.util.NumericUtils#sortableLongToDouble() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MultiFieldWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
static LongFunction<Object> bitsToValue(FieldType fieldType) {
  switch (fieldType.getNumberType()) {
    case LONG:
      return (bits)-> bits;
    case DATE:
      return (bits)-> new Date(bits);
    case INTEGER:
      return (bits)-> (int)bits;
    case FLOAT:
      return (bits)-> NumericUtils.sortableIntToFloat((int)bits);
    case DOUBLE:
      return (bits)-> NumericUtils.sortableLongToDouble(bits);
    default:
      throw new AssertionError("Unsupported NumberType: " + fieldType.getNumberType());
  }
}
 
Example 2
Source File: PercentileAgg.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * converts given long value to double based on field type
 */
protected double getDouble(long val) {
  switch (sf.getType().getNumberType()) {
    case INTEGER:
    case LONG:
    case DATE:
      return val;
    case FLOAT:
      return NumericUtils.sortableIntToFloat((int) val);
    case DOUBLE:
      return NumericUtils.sortableLongToDouble(val);
    default:
      // this would never happen
      return 0.0d;
  }
}
 
Example 3
Source File: SortedNumericStatsValues.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private Number toCorrectType(long value) {
  switch (numberType) {
    case INTEGER:
    case LONG:
      return value;
    case FLOAT:
      return NumericUtils.sortableIntToFloat((int)value);
    case DOUBLE:
      return NumericUtils.sortableLongToDouble(value);
    default:
      throw new AssertionError("Unsupported number type");
  }
}
 
Example 4
Source File: DoubleFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public FieldStats stats(Terms terms, int maxDoc) throws IOException {
    double minValue = NumericUtils.sortableLongToDouble(NumericUtils.getMinLong(terms));
    double maxValue = NumericUtils.sortableLongToDouble(NumericUtils.getMaxLong(terms));
    return new FieldStats.Double(
        maxDoc, terms.getDocCount(), terms.getSumDocFreq(), terms.getSumTotalTermFreq(), minValue, maxValue
    );
}
 
Example 5
Source File: SumAggregation.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(SumDoubleState state, int doc) throws IOException {
    if (values.advanceExact(doc) && values.docValueCount() == 1) {
        state.sum += NumericUtils.sortableLongToDouble(values.nextValue());
        state.hadValue = true;
    }
}
 
Example 6
Source File: DoubleMultiPointField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void collect(int doc) throws IOException {
  if (docValues.advanceExact(doc)) {
    count = docValues.docValueCount();
    resizeEmptyValues(count);
    for (int i = 0; i < count; ++i) {
      values[i] = NumericUtils.sortableLongToDouble(docValues.nextValue());
    }
  } else {
    count = 0;
  }
}
 
Example 7
Source File: AverageAggregation.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(AverageAggregation.AverageState state, int doc) throws IOException {
    if (values.advanceExact(doc) && values.docValueCount() == 1) {
        state.count++;
        state.sum += NumericUtils.sortableLongToDouble(values.nextValue());
    }
}
 
Example 8
Source File: TestGeoEncodingUtils.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * step through some integers, ensuring they decode to their expected double values.
 * double values start at -90 and increase by LATITUDE_DECODE for each integer.
 * check edge cases within the double range and random doubles within the range too.
 */
public void testLatitudeQuantization() throws Exception {
  final double LATITUDE_DECODE = 180.0D/(0x1L<<32);
  Random random = random();
  for (int i = 0; i < 10000; i++) {
    int encoded = random.nextInt();
    double min = MIN_LAT_INCL + (encoded - (long)Integer.MIN_VALUE) * LATITUDE_DECODE;
    double decoded = decodeLatitude(encoded);
    // should exactly equal expected value
    assertEquals(min, decoded, 0.0D);
    // should round-trip
    assertEquals(encoded, encodeLatitude(decoded));
    assertEquals(encoded, encodeLatitudeCeil(decoded));
    // test within the range
    if (encoded != Integer.MAX_VALUE) {
      // this is the next representable value
      // all double values between [min .. max) should encode to the current integer
      // all double values between (min .. max] should encodeCeil to the next integer.
      double max = min + LATITUDE_DECODE;
      assertEquals(max, decodeLatitude(encoded+1), 0.0D);
      assertEquals(encoded+1, encodeLatitude(max));
      assertEquals(encoded+1, encodeLatitudeCeil(max));

      // first and last doubles in range that will be quantized
      double minEdge = Math.nextUp(min);
      double maxEdge = Math.nextDown(max);
      assertEquals(encoded,   encodeLatitude(minEdge));
      assertEquals(encoded+1, encodeLatitudeCeil(minEdge));
      assertEquals(encoded,   encodeLatitude(maxEdge));
      assertEquals(encoded+1, encodeLatitudeCeil(maxEdge));

      // check random values within the double range
      long minBits = NumericUtils.doubleToSortableLong(minEdge);
      long maxBits = NumericUtils.doubleToSortableLong(maxEdge);
      for (int j = 0; j < 100; j++) {
        double value = NumericUtils.sortableLongToDouble(TestUtil.nextLong(random, minBits, maxBits));
        // round down
        assertEquals(encoded,   encodeLatitude(value));
        // round up
        assertEquals(encoded+1, encodeLatitudeCeil(value));
      }
    }
  }
}
 
Example 9
Source File: DoubleFieldTypeDefinition.java    From incubator-retired-blur with Apache License 2.0 4 votes vote down vote up
@Override
public String readTerm(BytesRef byteRef) {
 if(NumericUtils.getPrefixCodedLongShift(byteRef) == 0)
  return NumericUtils.sortableLongToDouble(NumericUtils.prefixCodedToLong(byteRef))+"";
 return null;
}
 
Example 10
Source File: SolrDocumentFetcher.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private Object decodeNumberFromDV(SchemaField schemaField, long value, boolean sortableNumeric) {
  // note: This special-case is unfortunate; if we have to add any more than perhaps the fieldType should
  //  have this method so that specific field types can customize it.
  if (schemaField.getType() instanceof LatLonPointSpatialField) {
    return LatLonPointSpatialField.decodeDocValueToString(value);
  }

  if (schemaField.getType().getNumberType() == null) {
    log.warn("Couldn't decode docValues for field: [{}], schemaField: [{}], numberType is unknown",
        schemaField.getName(), schemaField);
    return null;
  }

  switch (schemaField.getType().getNumberType()) {
    case INTEGER:
      final int raw = (int)value;
      if (schemaField.getType() instanceof AbstractEnumField) {
        return ((AbstractEnumField)schemaField.getType()).getEnumMapping().intValueToStringValue(raw);
      } else {
        return raw;
      }
    case LONG:
      return value;
    case FLOAT:
      if (sortableNumeric) {
        return NumericUtils.sortableIntToFloat((int)value);
      } else {
        return Float.intBitsToFloat((int)value);
      }
    case DOUBLE:
      if (sortableNumeric) {
        return NumericUtils.sortableLongToDouble(value);
      } else {
        return Double.longBitsToDouble(value);
      }
    case DATE:
      return new Date(value);
    default:
      // catched all possible values, this line will never be reached
      throw new AssertionError();
  }
}
 
Example 11
Source File: TrieField.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public Object toObject(IndexableField f) {
  final Number val = f.numericValue();
  if (val != null) {

    if (f.fieldType().stored() == false && f.fieldType().docValuesType() == DocValuesType.NUMERIC ) {
      long bits = val.longValue();
      switch (type) {
        case INTEGER:
          return (int)bits;
        case FLOAT:
          return Float.intBitsToFloat((int)bits);
        case LONG:
          return bits;
        case DOUBLE:
          return Double.longBitsToDouble(bits);
        case DATE:
          return new Date(bits);
        default:
          throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + f.name());
      }
    }

    // normal stored case
    return (type == NumberType.DATE) ? new Date(val.longValue()) : val;
  } else {
    // multi-valued numeric docValues currently use SortedSet on the indexed terms.
    BytesRef term = f.binaryValue();
    switch (type) {
      case INTEGER:
        return LegacyNumericUtils.prefixCodedToInt(term);
      case FLOAT:
        return NumericUtils.sortableIntToFloat(LegacyNumericUtils.prefixCodedToInt(term));
      case LONG:
        return LegacyNumericUtils.prefixCodedToLong(term);
      case DOUBLE:
        return NumericUtils.sortableLongToDouble(LegacyNumericUtils.prefixCodedToLong(term));
      case DATE:
        return new Date(LegacyNumericUtils.prefixCodedToLong(term));
      default:
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + f.name());
    }
  }

}
 
Example 12
Source File: DoubleTermsAggregator.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private static DoubleTerms.Bucket convertToDouble(InternalTerms.Bucket bucket) {
    final long term = ((Number) bucket.getKey()).longValue();
    final double value = NumericUtils.sortableLongToDouble(term);
    return new DoubleTerms.Bucket(value, bucket.docCount, bucket.aggregations, bucket.showDocCountError, bucket.docCountError, bucket.formatter);
}
 
Example 13
Source File: DoubleRange.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** decodes the max value (for the defined dimension) from the encoded input byte array */
static double decodeMax(byte[] b, int dimension) {
  int offset = b.length/2 + dimension*BYTES;
  return NumericUtils.sortableLongToDouble(NumericUtils.sortableBytesToLong(b, offset));
}
 
Example 14
Source File: SortableLongBitsToSortedNumericDoubleValues.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public double nextValue() throws IOException {
    return NumericUtils.sortableLongToDouble(values.nextValue());
}
 
Example 15
Source File: DoublePoint.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** Decode single double dimension */
public static double decodeDimension(byte value[], int offset) {
  return NumericUtils.sortableLongToDouble(NumericUtils.sortableBytesToLong(value, offset));
}
 
Example 16
Source File: TestGeo3DPoint.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * step through some integers, ensuring they decode to their expected double values.
 * double values start at -planetMax and increase by Geo3DUtil.DECODE for each integer.
 * check edge cases within the double range and random doubles within the range too.
 */
public void testQuantization() throws Exception {
  Random random = random();
  PlanetModel planetModel = randomPlanetModel();
  for (int i = 0; i < 10000; i++) {
    int encoded = random.nextInt();
    if (encoded <= planetModel.MIN_ENCODED_VALUE) {
      continue;
    }
    if (encoded >= planetModel.MAX_ENCODED_VALUE) {
      continue;
    }
    double min = encoded * planetModel.DECODE;
    double decoded = Geo3DUtil.decodeValueFloor(encoded, planetModel);
    // should exactly equal expected value
    assertEquals(min, decoded, 0.0D);
    // should round-trip
    assertEquals(encoded, planetModel.encodeValue(decoded));
    // test within the range
    if (encoded != Integer.MAX_VALUE) {
      // this is the next representable value
      // all double values between [min .. max) should encode to the current integer
      double max = min + planetModel.DECODE;
      assertEquals(max, Geo3DUtil.decodeValueFloor(encoded+1, planetModel), 0.0D);
      assertEquals(encoded+1, planetModel.encodeValue(max));

      // first and last doubles in range that will be quantized
      double minEdge = Math.nextUp(min);
      double maxEdge = Math.nextDown(max);
      assertEquals(encoded, planetModel.encodeValue(minEdge));
      assertEquals(encoded, planetModel.encodeValue(maxEdge));

      // check random values within the double range
      long minBits = NumericUtils.doubleToSortableLong(minEdge);
      long maxBits = NumericUtils.doubleToSortableLong(maxEdge);
      for (int j = 0; j < 100; j++) {
        double value = NumericUtils.sortableLongToDouble(TestUtil.nextLong(random, minBits, maxBits));
        // round down
        assertEquals(encoded,   planetModel.encodeValue(value));
      }
    }
  }
}
 
Example 17
Source File: SortableLongBitsToNumericDoubleValues.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public double get(int docID) {
    return NumericUtils.sortableLongToDouble(values.get(docID));
}
 
Example 18
Source File: SortableLongBitsToSortedNumericDoubleValues.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public double valueAt(int index) {
    return NumericUtils.sortableLongToDouble(values.valueAt(index));
}
 
Example 19
Source File: IndexNumericFieldData.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Number toNumber(BytesRef indexForm) {
    return NumericUtils.sortableLongToDouble(NumericUtils.prefixCodedToLong(indexForm));
}
 
Example 20
Source File: SortableLongBitsToNumericDoubleValues.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public double doubleValue() throws IOException {
    return NumericUtils.sortableLongToDouble(values.longValue());
}