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

The following examples show how to use org.apache.lucene.util.NumericUtils#intToSortableBytes() . 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: TestBKDRadixSelector.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testBasic() throws IOException {
  int values = 4;
  Directory dir = getDirectory(values);
  int middle = 2;
  int dimensions =1;
  int bytesPerDimensions = Integer.BYTES;
  int packedLength = dimensions * bytesPerDimensions;
  PointWriter points = getRandomPointWriter(dir, values, packedLength);
  byte[] value = new byte[packedLength];
  NumericUtils.intToSortableBytes(1, value, 0);
  points.append(value, 0);
  NumericUtils.intToSortableBytes(2, value, 0);
  points.append(value, 1);
  NumericUtils.intToSortableBytes(3, value, 0);
  points.append(value, 2);
  NumericUtils.intToSortableBytes(4, value, 0);
  points.append(value, 3);
  points.close();
  PointWriter copy = copyPoints(dir,points, packedLength);
  verify(dir, copy, dimensions, dimensions, 0, values, middle, packedLength, bytesPerDimensions, 0);
  dir.close();
}
 
Example 2
Source File: LatLonBoundingBox.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** encodes a two-dimensional geopoint (lat, lon) into a byte array */
static void encode(double lat, double lon, byte[] result, int offset) {
  if (result == null) {
    result = new byte[BYTES*4];
  }
  NumericUtils.intToSortableBytes(encodeLatitude(lat), result, offset);
  NumericUtils.intToSortableBytes(encodeLongitude(lon), result, offset + BYTES);
}
 
Example 3
Source File: LatLonPoint.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** sugar encodes a single point as a byte array */
private static byte[] encode(double latitude, double longitude) {
  byte[] bytes = new byte[2 * Integer.BYTES];
  NumericUtils.intToSortableBytes(encodeLatitude(latitude), bytes, 0);
  NumericUtils.intToSortableBytes(encodeLongitude(longitude), bytes, Integer.BYTES);
  return bytes;
}
 
Example 4
Source File: LatLonPoint.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** sugar encodes a single point as a byte array, rounding values up */
private static byte[] encodeCeil(double latitude, double longitude) {
  byte[] bytes = new byte[2 * Integer.BYTES];
  NumericUtils.intToSortableBytes(encodeLatitudeCeil(latitude), bytes, 0);
  NumericUtils.intToSortableBytes(encodeLongitudeCeil(longitude), bytes, Integer.BYTES);
  return bytes;
}
 
Example 5
Source File: LatLonShapeBoundingBoxQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * encodes a bounding box into the provided byte array
 */
private static void encode(final int minX, final int maxX, final int minY, final int maxY, byte[] b) {
  if (b == null) {
    b = new byte[4 * BYTES];
  }
  NumericUtils.intToSortableBytes(minY, b, 0);
  NumericUtils.intToSortableBytes(minX, b, BYTES);
  NumericUtils.intToSortableBytes(maxY, b, 2 * BYTES);
  NumericUtils.intToSortableBytes(maxX, b, 3 * BYTES);
}
 
Example 6
Source File: EnumFieldType.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void readableToIndexed(CharSequence val, BytesRefBuilder result) {
  final String s = val.toString();
  if (s == null)
    return;

  result.grow(Integer.BYTES);
  result.setLength(Integer.BYTES);
  final Integer intValue = enumMapping.stringValueToIntValue(s);
  NumericUtils.intToSortableBytes(intValue, result.bytes(), 0);
}
 
Example 7
Source File: EnumFieldType.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public String storedToIndexed(IndexableField f) {
  final Number val = f.numericValue();
  if (val == null)
    return null;
  final BytesRefBuilder bytes = new BytesRefBuilder();
  bytes.grow(Integer.BYTES);
  bytes.setLength(Integer.BYTES);
  NumericUtils.intToSortableBytes(val.intValue(), bytes.bytes(), 0);
  return bytes.get().utf8ToString();
}
 
Example 8
Source File: EnumFieldType.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public IndexableField createField(SchemaField field, Object value) {
  final Integer intValue = enumMapping.stringValueToIntValue(value.toString());
  if (intValue == null || intValue.equals(EnumMapping.DEFAULT_VALUE)) {
    String exceptionMessage = String.format(Locale.ENGLISH, "Unknown value for enum field: %s, value: %s",
        field.getName(), value.toString());
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,  exceptionMessage);
  }

  org.apache.lucene.document.FieldType newType = new org.apache.lucene.document.FieldType();
  newType.setTokenized(false);
  newType.setStored(field.stored());
  newType.setOmitNorms(field.omitNorms());
  newType.setIndexOptions(field.indexOptions());
  newType.setStoreTermVectors(field.storeTermVector());
  newType.setStoreTermVectorOffsets(field.storeTermOffsets());
  newType.setStoreTermVectorPositions(field.storeTermPositions());
  newType.setStoreTermVectorPayloads(field.storeTermPayloads());
  
  byte[] bytes = new byte[Integer.BYTES];
  NumericUtils.intToSortableBytes(intValue, bytes, 0);         
  return new Field(field.getName(), bytes, newType) {
    @Override public Number numericValue() {
      return NumericUtils.sortableBytesToInt(((BytesRef)fieldsData).bytes, 0);
    }
  };
}
 
Example 9
Source File: Geo3DPoint.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** Encode single dimension */
public static void encodeDimension(double value, byte bytes[], int offset, PlanetModel planetModel) {
  NumericUtils.intToSortableBytes(planetModel.encodeValue(value), bytes, offset);
}
 
Example 10
Source File: DocumentsTestBase.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private BytesRef packInt(int value) {
  byte[] dest = new byte[Integer.BYTES];
  NumericUtils.intToSortableBytes(value, dest, 0);
  return new BytesRef(dest);
}
 
Example 11
Source File: IntRange.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** encode the given value into the byte array at the defined offset */
private static void encode(int val, byte[] bytes, int offset) {
  NumericUtils.intToSortableBytes(val, bytes, offset);
}
 
Example 12
Source File: LatLonPoint.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Create a query for matching a bounding box.
 * <p>
 * The box may cross over the dateline.
 * @param field field name. must not be null.
 * @param minLatitude latitude lower bound: must be within standard +/-90 coordinate bounds.
 * @param maxLatitude latitude upper bound: must be within standard +/-90 coordinate bounds.
 * @param minLongitude longitude lower bound: must be within standard +/-180 coordinate bounds.
 * @param maxLongitude longitude upper bound: must be within standard +/-180 coordinate bounds.
 * @return query matching points within this box
 * @throws IllegalArgumentException if {@code field} is null, or the box has invalid coordinates.
 */
public static Query newBoxQuery(String field, double minLatitude, double maxLatitude, double minLongitude, double maxLongitude) {
  // exact double values of lat=90.0D and lon=180.0D must be treated special as they are not represented in the encoding
  // and should not drag in extra bogus junk! TODO: should encodeCeil just throw ArithmeticException to be less trappy here?
  if (minLatitude == 90.0) {
    // range cannot match as 90.0 can never exist
    return new MatchNoDocsQuery("LatLonPoint.newBoxQuery with minLatitude=90.0");
  }
  if (minLongitude == 180.0) {
    if (maxLongitude == 180.0) {
      // range cannot match as 180.0 can never exist
      return new MatchNoDocsQuery("LatLonPoint.newBoxQuery with minLongitude=maxLongitude=180.0");
    } else if (maxLongitude < minLongitude) {
      // encodeCeil() with dateline wrapping!
      minLongitude = -180.0;
    }
  }
  byte[] lower = encodeCeil(minLatitude, minLongitude);
  byte[] upper = encode(maxLatitude, maxLongitude);
  // Crosses date line: we just rewrite into OR of two bboxes, with longitude as an open range:
  if (maxLongitude < minLongitude) {
    // Disable coord here because a multi-valued doc could match both rects and get unfairly boosted:
    BooleanQuery.Builder q = new BooleanQuery.Builder();

    // E.g.: maxLon = -179, minLon = 179
    byte[] leftOpen = lower.clone();
    // leave longitude open
    NumericUtils.intToSortableBytes(Integer.MIN_VALUE, leftOpen, Integer.BYTES);
    Query left = newBoxInternal(field, leftOpen, upper);
    q.add(new BooleanClause(left, BooleanClause.Occur.SHOULD));

    byte[] rightOpen = upper.clone();
    // leave longitude open
    NumericUtils.intToSortableBytes(Integer.MAX_VALUE, rightOpen, Integer.BYTES);
    Query right = newBoxInternal(field, lower, rightOpen);
    q.add(new BooleanClause(right, BooleanClause.Occur.SHOULD));
    return new ConstantScoreQuery(q.build());
  } else {
    return newBoxInternal(field, lower, upper);
  }
}
 
Example 13
Source File: IntPoint.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** Encode single integer dimension */
public static void encodeDimension(int value, byte dest[], int offset) {
  NumericUtils.intToSortableBytes(value, dest, offset);
}
 
Example 14
Source File: FloatRange.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** encode the given value into the byte array at the defined offset */
private static void encode(float val, byte[] bytes, int offset) {
  NumericUtils.intToSortableBytes(NumericUtils.floatToSortableInt(val), bytes, offset);
}
 
Example 15
Source File: FloatPoint.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** Encode single float dimension */
public static void encodeDimension(float value, byte dest[], int offset) {
  NumericUtils.intToSortableBytes(NumericUtils.floatToSortableInt(value), dest, offset);
}