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

The following examples show how to use org.apache.lucene.util.NumericUtils#doubleToSortableLong() . 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 double 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 -> {
      double value = missingValue;
      if (values.advanceExact(docID)) {
        value = Double.longBitsToDouble(values.longValue());
      }
      return NumericUtils.doubleToSortableLong(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 getRangeQueryForMultiValuedDoubleDocValues(SchemaField sf, String min, String max, boolean minInclusive, boolean maxInclusive) {
  double minVal,maxVal;
  if (min == null) {
    minVal = Double.NEGATIVE_INFINITY;
  } else {
    minVal = parseDoubleFromUser(sf.getName(), min);
    if (!minInclusive) {
      if (minVal == Double.POSITIVE_INFINITY) return new MatchNoDocsQuery();
      minVal = DoublePoint.nextUp(minVal);
    }
  }
  if (max == null) {
    maxVal = Double.POSITIVE_INFINITY;
  } else {
    maxVal = parseDoubleFromUser(sf.getName(), max);
    if (!maxInclusive) {
      if (maxVal == Double.NEGATIVE_INFINITY) return new MatchNoDocsQuery();
      maxVal = DoublePoint.nextDown(maxVal);
    }
  }
  Long minBits = NumericUtils.doubleToSortableLong(minVal);
  Long maxBits = NumericUtils.doubleToSortableLong(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 testDoubles() throws Exception {
  double[] vals=new double[]{
    Double.NEGATIVE_INFINITY, -2.3E25, -1.0E15, -1.0, -1.0E-1, -1.0E-2, -0.0, 
    +0.0, 1.0E-2, 1.0E-1, 1.0, 1.0E15, 2.3E25, Double.POSITIVE_INFINITY, Double.NaN
  };
  long[] longVals=new long[vals.length];
  
  // check forward and back conversion
  for (int i=0; i<vals.length; i++) {
    longVals[i]= NumericUtils.doubleToSortableLong(vals[i]);
    assertTrue( "forward and back conversion should generate same double", Double.compare(vals[i], NumericUtils.sortableLongToDouble(longVals[i]))==0 );
  }
  
  // check sort order (prefixVals should be ascending)
  for (int i=1; i<longVals.length; i++) {
    assertTrue( "check sort order", longVals[i-1] < longVals[i] );
  }
}
 
Example 4
Source File: TermBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public BytesRef term(Double value) {
    long longValue = NumericUtils.doubleToSortableLong(value);
    BytesRefBuilder bytesRef = new BytesRefBuilder();
    NumericUtils.longToPrefixCoded(longValue, 0, bytesRef);
    return bytesRef.get();
}
 
Example 5
Source File: DoubleFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public BytesRef indexedValueForSearch(Object value) {
    long longValue = NumericUtils.doubleToSortableLong(parseDoubleValue(value));
    BytesRefBuilder bytesRef = new BytesRefBuilder();
    NumericUtils.longToPrefixCoded(longValue, 0, bytesRef);   // 0 because of exact match
    return bytesRef.get();
}
 
Example 6
Source File: TestDocValuesQueries.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testSortedNumericNPE() throws IOException {
  Directory dir = newDirectory();
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
  double[] nums = {-1.7147449030215377E-208, -1.6887024655302576E-11, 1.534911516604164E113, 0.0,
      2.6947996404505155E-166, -2.649722021970773E306, 6.138239235731689E-198, 2.3967090122610808E111};
  for (int i = 0; i < nums.length; ++i) {
    Document doc = new Document();
    doc.add(new SortedNumericDocValuesField("dv", NumericUtils.doubleToSortableLong(nums[i])));
    iw.addDocument(doc);
  }
  iw.commit();
  final IndexReader reader = iw.getReader();
  final IndexSearcher searcher = newSearcher(reader);
  iw.close();

  final long lo = NumericUtils.doubleToSortableLong(8.701032080293731E-226);
  final long hi = NumericUtils.doubleToSortableLong(2.0801416404385346E-41);
  
  Query query = SortedNumericDocValuesField.newSlowRangeQuery("dv", lo, hi);
  // TODO: assert expected matches
  searcher.search(query, searcher.reader.maxDoc(), Sort.INDEXORDER);

  // swap order, should still work
  query = SortedNumericDocValuesField.newSlowRangeQuery("dv", hi, lo);
  // TODO: assert expected matches
  searcher.search(query, searcher.reader.maxDoc(), Sort.INDEXORDER);
  
  reader.close();
  dir.close();
}
 
Example 7
Source File: TestLegacyNumericUtils.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testSortableDoubleNaN() {
  final long plusInf = NumericUtils.doubleToSortableLong(Double.POSITIVE_INFINITY);
  for (double nan : DOUBLE_NANs) {
    assertTrue(Double.isNaN(nan));
    final long sortable = NumericUtils.doubleToSortableLong(nan);
    assertTrue("Double not sorted correctly: " + nan + ", long repr: " 
        + sortable + ", positive inf.: " + plusInf, sortable > plusInf);
  }
}
 
Example 8
Source File: SortableLongBitsNumericDocValues.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public long get(int docID) {
    return NumericUtils.doubleToSortableLong(values.get(docID));
}
 
Example 9
Source File: SortableLongBitsSortedNumericDocValues.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public long valueAt(int index) {
    return NumericUtils.doubleToSortableLong(values.valueAt(index));
}
 
Example 10
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 11
Source File: DoubleRangeFacetCounts.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void count(DoubleValuesSource valueSource, List<MatchingDocs> matchingDocs) throws IOException {

    DoubleRange[] ranges = (DoubleRange[]) this.ranges;

    LongRange[] longRanges = new LongRange[ranges.length];
    for(int i=0;i<ranges.length;i++) {
      DoubleRange range = ranges[i];
      longRanges[i] =  new LongRange(range.label,
                                     NumericUtils.doubleToSortableLong(range.min), true,
                                     NumericUtils.doubleToSortableLong(range.max), true);
    }

    LongRangeCounter counter = new LongRangeCounter(longRanges);

    int missingCount = 0;
    for (MatchingDocs hits : matchingDocs) {
      DoubleValues fv = valueSource.getValues(hits.context, null);
      
      totCount += hits.totalHits;
      final DocIdSetIterator fastMatchDocs;
      if (fastMatchQuery != null) {
        final IndexReaderContext topLevelContext = ReaderUtil.getTopLevelContext(hits.context);
        final IndexSearcher searcher = new IndexSearcher(topLevelContext);
        searcher.setQueryCache(null);
        final Weight fastMatchWeight = searcher.createWeight(searcher.rewrite(fastMatchQuery), ScoreMode.COMPLETE_NO_SCORES, 1);
        Scorer s = fastMatchWeight.scorer(hits.context);
        if (s == null) {
          continue;
        }
        fastMatchDocs = s.iterator();
      } else {
        fastMatchDocs = null;
      }

      DocIdSetIterator docs = hits.bits.iterator();

      for (int doc = docs.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; ) {
        if (fastMatchDocs != null) {
          int fastMatchDoc = fastMatchDocs.docID();
          if (fastMatchDoc < doc) {
            fastMatchDoc = fastMatchDocs.advance(doc);
          }

          if (doc != fastMatchDoc) {
            doc = docs.advance(fastMatchDoc);
            continue;
          }
        }
        // Skip missing docs:
        if (fv.advanceExact(doc)) {
          counter.add(NumericUtils.doubleToSortableLong(fv.doubleValue()));
        } else {
          missingCount++;
        }

        doc = docs.nextDoc();
      }
    }

    missingCount += counter.fillCounts(counts);
    totCount -= missingCount;
  }
 
Example 12
Source File: DoubleRange.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
LongRange toLongRange() {
  return new LongRange(label,
                       NumericUtils.doubleToSortableLong(min), true,
                       NumericUtils.doubleToSortableLong(max), true);
}
 
Example 13
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 14
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 -180 and increase by LONGITUDE_DECODE for each integer.
 * check edge cases within the double range and a random doubles within the range too.
 */
public void testLongitudeQuantization() throws Exception {
  final double LONGITUDE_DECODE = 360.0D/(0x1L<<32);
  Random random = random();
  for (int i = 0; i < 10000; i++) {
    int encoded = random.nextInt();
    double min = MIN_LON_INCL + (encoded - (long)Integer.MIN_VALUE) * LONGITUDE_DECODE;
    double decoded = decodeLongitude(encoded);
    // should exactly equal expected value
    assertEquals(min, decoded, 0.0D);
    // should round-trip
    assertEquals(encoded, encodeLongitude(decoded));
    assertEquals(encoded, encodeLongitudeCeil(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 + LONGITUDE_DECODE;
      assertEquals(max, decodeLongitude(encoded+1), 0.0D);
      assertEquals(encoded+1, encodeLongitude(max));
      assertEquals(encoded+1, encodeLongitudeCeil(max));

      // first and last doubles in range that will be quantized
      double minEdge = Math.nextUp(min);
      double maxEdge = Math.nextDown(max);
      assertEquals(encoded,   encodeLongitude(minEdge));
      assertEquals(encoded+1, encodeLongitudeCeil(minEdge));
      assertEquals(encoded,   encodeLongitude(maxEdge));
      assertEquals(encoded+1, encodeLongitudeCeil(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, encodeLongitude(value));
        // round up
        assertEquals(encoded+1, encodeLongitudeCeil(value));
      }
    }
  }
}
 
Example 15
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 16
Source File: DoubleField.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.doubleToSortableLong(Synt.declare(v, 0.0D)));
}