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

The following examples show how to use org.apache.lucene.util.NumericUtils#floatToSortableInt() . 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: IndexSorter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public ComparableProvider[] getComparableProviders(List<? extends LeafReader> readers) throws IOException {
  ComparableProvider[] providers = new ComparableProvider[readers.size()];
  final float missingValue;
  if (this.missingValue != null) {
    missingValue = this.missingValue;
  } else {
    missingValue = 0.0f;
  }

  for(int readerIndex=0;readerIndex<readers.size();readerIndex++) {
    final NumericDocValues values = valuesProvider.get(readers.get(readerIndex));

    providers[readerIndex] = docID -> {
      float value = missingValue;
      if (values.advanceExact(docID)) {
        value = Float.intBitsToFloat((int) values.longValue());
      }
      return NumericUtils.floatToSortableInt(value);
    };
  }
  return providers;
}
 
Example 2
Source File: NumericFieldType.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected Query getRangeQueryForMultiValuedFloatDocValues(SchemaField sf, String min, String max, boolean minInclusive, boolean maxInclusive) {
  float minVal,maxVal;
  if (min == null) {
    minVal = Float.NEGATIVE_INFINITY;
  } else {
    minVal = parseFloatFromUser(sf.getName(), min);
    if (!minInclusive) {
      if (minVal == Float.POSITIVE_INFINITY) return new MatchNoDocsQuery();
      minVal = FloatPoint.nextUp(minVal);
    }
  }
  if (max == null) {
    maxVal = Float.POSITIVE_INFINITY;
  } else {
    maxVal = parseFloatFromUser(sf.getName(), max);
    if (!maxInclusive) {
      if (maxVal == Float.NEGATIVE_INFINITY) return new MatchNoDocsQuery();
      maxVal = FloatPoint.nextDown(maxVal);
    }
  }
  Long minBits = (long)NumericUtils.floatToSortableInt(minVal);
  Long maxBits = (long)NumericUtils.floatToSortableInt(maxVal);
  return numericDocValuesRangeQuery(sf.getName(), minBits, maxBits, true, true, true);
}
 
Example 3
Source File: TestLegacyNumericUtils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testFloats() throws Exception {
  float[] vals=new float[]{
    Float.NEGATIVE_INFINITY, -2.3E25f, -1.0E15f, -1.0f, -1.0E-1f, -1.0E-2f, -0.0f, 
    +0.0f, 1.0E-2f, 1.0E-1f, 1.0f, 1.0E15f, 2.3E25f, Float.POSITIVE_INFINITY, Float.NaN
  };
  int[] intVals=new int[vals.length];
  
  // check forward and back conversion
  for (int i=0; i<vals.length; i++) {
    intVals[i]= NumericUtils.floatToSortableInt(vals[i]);
    assertTrue( "forward and back conversion should generate same double", Float.compare(vals[i], NumericUtils.sortableIntToFloat(intVals[i]))==0 );
  }
  
  // check sort order (prefixVals should be ascending)
  for (int i=1; i<intVals.length; i++) {
    assertTrue( "check sort order", intVals[i-1] < intVals[i] );
  }
}
 
Example 4
Source File: TermBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public BytesRef term(Float value) {
    int intValue = NumericUtils.floatToSortableInt(value);
    BytesRefBuilder bytesRef = new BytesRefBuilder();
    NumericUtils.intToPrefixCoded(intValue, 0, bytesRef);
    return bytesRef.get();
}
 
Example 5
Source File: FloatFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public BytesRef indexedValueForSearch(Object value) {
    int intValue = NumericUtils.floatToSortableInt(parseValue(value));
    BytesRefBuilder bytesRef = new BytesRefBuilder();
    NumericUtils.intToPrefixCoded(intValue, 0, bytesRef);   // 0 because of exact match
    return bytesRef.get();
}
 
Example 6
Source File: TestLegacyNumericUtils.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testSortableFloatNaN() {
  final int plusInf = NumericUtils.floatToSortableInt(Float.POSITIVE_INFINITY);
  for (float nan : FLOAT_NANs) {
    assertTrue(Float.isNaN(nan));
    final int sortable = NumericUtils.floatToSortableInt(nan);
    assertTrue("Float not sorted correctly: " + nan + ", int repr: " 
        + sortable + ", positive inf.: " + plusInf, sortable > plusInf);
  }
}
 
Example 7
Source File: IntervalFacets.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Set startLimit and endLimit for numeric values. The limits in this case
 * are going to be the <code>long</code> representation of the original
 * value. <code>startLimit</code> will be incremented by one in case of the
 * interval start being exclusive. <code>endLimit</code> will be decremented by
 * one in case of the interval end being exclusive.
 */
private void setNumericLimits(SchemaField schemaField) {
  if (start == null) {
    startLimit = Long.MIN_VALUE;
  } else {
    switch (schemaField.getType().getNumberType()) {
      case LONG:
        startLimit = (long) schemaField.getType().toObject(schemaField, start);
        break;
      case DATE:
        startLimit = ((Date) schemaField.getType().toObject(schemaField, start)).getTime();
        break;
      case INTEGER:
        startLimit = ((Integer) schemaField.getType().toObject(schemaField, start)).longValue();
        break;
      case FLOAT:
        startLimit = NumericUtils.floatToSortableInt((float) schemaField.getType().toObject(schemaField, start));
        break;
      case DOUBLE:
        startLimit = NumericUtils.doubleToSortableLong((double) schemaField.getType().toObject(schemaField, start));
        break;
      default:
        throw new AssertionError();
    }
    if (startOpen) {
      if (startLimit == Long.MAX_VALUE) {
        /*
         * This interval can match no docs
         */
        includeNoDocs = true;
      } else {
        startLimit++;
      }
    }
  }


  if (end == null) {
    endLimit = Long.MAX_VALUE;
  } else {
    switch (schemaField.getType().getNumberType()) {
      case LONG:
        endLimit = (long) schemaField.getType().toObject(schemaField, end);
        break;
      case DATE:
        endLimit = ((Date) schemaField.getType().toObject(schemaField, end)).getTime();
        break;
      case INTEGER:
        endLimit = ((Integer) schemaField.getType().toObject(schemaField, end)).longValue();
        break;
      case FLOAT:
        endLimit = NumericUtils.floatToSortableInt((float) schemaField.getType().toObject(schemaField, end));
        break;
      case DOUBLE:
        endLimit = NumericUtils.doubleToSortableLong((double) schemaField.getType().toObject(schemaField, end));
        break;
      default:
        throw new AssertionError();
    }
    if (endOpen) {
      if (endLimit == Long.MIN_VALUE) {
        /*
         * This interval can match no docs
         */
        includeNoDocs = true;
      } else {
        endLimit--;
      }
    }
  }
}
 
Example 8
Source File: FloatField.java    From HongsCORE with MIT License 4 votes vote down vote up
@Override
public Field ods(String k, Object v) {
    return new SortedNumericDocValuesField("%"+k, NumericUtils.floatToSortableInt(Synt.declare(v, 0.0F)));
}
 
Example 9
Source File: XYEncodingUtils.java    From lucene-solr with Apache License 2.0 2 votes vote down vote up
/**
 * Quantizes double (64 bit) values into 32 bits
 * @param x cartesian value
 * @return encoded value as a 32-bit {@code int}
 * @throws IllegalArgumentException if value is out of bounds
 */
public static int encode(float x) {
  return NumericUtils.floatToSortableInt(checkVal(x));
}