public final class NumericUtils extends Object
To quickly execute range queries in Apache Lucene, a range is divided recursively into multiple intervals for searching: The center of the range is searched only with the lowest possible precision in the trie, while the boundaries are matched more exactly. This reduces the number of terms dramatically.
This class generates terms to achieve this: First the numerical integer values need to
be converted to strings. For that integer values (32 bit or 64 bit) are made unsigned
and the bits are converted to ASCII chars with each 7 bit. The resulting string is
sortable like the original integer value. Each value is also prefixed
(in the first char) by the shift
value (number of bits removed) used
during encoding.
To also index floating point numbers, this class supplies two methods to convert them
to integer values by changing their bit layout: doubleToSortableLong(double)
,
floatToSortableInt(float)
. You will have no precision loss by
converting floating point numbers to integers and back (only that the integer form
is not usable). Other data types like dates can easily converted to longs or ints (e.g.
date to long: Date.getTime()
).
For easy usage, the trie algorithm is implemented for indexing inside
NumericTokenStream
that can index int
, long
,
float
, and double
. For querying,
NumericRangeQuery
and NumericRangeFilter
implement the query part
for the same data types.
This class can also be used, to generate lexicographically sortable (according
String.compareTo(String)
) representations of numeric data types for other
usages (e.g. sorting).
Modifier and Type | Class and Description |
---|---|
static class |
NumericUtils.IntRangeBuilder
Expert: Callback for
splitIntRange(org.apache.lucene.util.NumericUtils.IntRangeBuilder, int, int, int) . |
static class |
NumericUtils.LongRangeBuilder
Expert: Callback for
splitLongRange(org.apache.lucene.util.NumericUtils.LongRangeBuilder, int, long, long) . |
Modifier and Type | Field and Description |
---|---|
static int |
BUF_SIZE_INT
Expert: The maximum term length (used for
char[] buffer size)
for encoding int values. |
static int |
BUF_SIZE_LONG
Expert: The maximum term length (used for
char[] buffer size)
for encoding long values. |
static int |
PRECISION_STEP_DEFAULT
The default precision step used by
NumericField , NumericTokenStream ,
NumericRangeQuery , and NumericRangeFilter as default |
static char |
SHIFT_START_INT
Expert: Integers are stored at lower precision by shifting off lower bits.
|
static char |
SHIFT_START_LONG
Expert: Longs are stored at lower precision by shifting off lower bits.
|
Modifier and Type | Method and Description |
---|---|
static String |
doubleToPrefixCoded(double val) |
static long |
doubleToSortableLong(double val)
Converts a
double value to a sortable signed long . |
static String |
floatToPrefixCoded(float val) |
static int |
floatToSortableInt(float val)
Converts a
float value to a sortable signed int . |
static String |
intToPrefixCoded(int val) |
static String |
intToPrefixCoded(int val,
int shift) |
static int |
intToPrefixCoded(int val,
int shift,
char[] buffer)
Expert: Returns prefix coded bits after reducing the precision by
shift bits. |
static String |
longToPrefixCoded(long val) |
static String |
longToPrefixCoded(long val,
int shift) |
static int |
longToPrefixCoded(long val,
int shift,
char[] buffer)
Expert: Returns prefix coded bits after reducing the precision by
shift bits. |
static double |
prefixCodedToDouble(String val) |
static float |
prefixCodedToFloat(String val) |
static int |
prefixCodedToInt(String prefixCoded) |
static long |
prefixCodedToLong(String prefixCoded) |
static float |
sortableIntToFloat(int val)
Converts a sortable
int back to a float . |
static double |
sortableLongToDouble(long val)
Converts a sortable
long back to a double . |
static void |
splitIntRange(NumericUtils.IntRangeBuilder builder,
int precisionStep,
int minBound,
int maxBound)
Expert: Splits an int range recursively.
|
static void |
splitLongRange(NumericUtils.LongRangeBuilder builder,
int precisionStep,
long minBound,
long maxBound)
Expert: Splits a long range recursively.
|
public static final int PRECISION_STEP_DEFAULT
NumericField
, NumericTokenStream
,
NumericRangeQuery
, and NumericRangeFilter
as defaultpublic static final char SHIFT_START_LONG
SHIFT_START_LONG+shift
in the first characterpublic static final int BUF_SIZE_LONG
char[]
buffer size)
for encoding long
values.public static final char SHIFT_START_INT
SHIFT_START_INT+shift
in the first characterpublic static final int BUF_SIZE_INT
char[]
buffer size)
for encoding int
values.public static int longToPrefixCoded(long val, int shift, char[] buffer)
shift
bits.
This is method is used by NumericTokenStream
.val
- the numeric valueshift
- how many bits to strip from the rightbuffer
- that will contain the encoded chars, must be at least of BUF_SIZE_LONG
lengthpublic static String longToPrefixCoded(long val, int shift)
public static String longToPrefixCoded(long val)
public static int intToPrefixCoded(int val, int shift, char[] buffer)
shift
bits.
This is method is used by NumericTokenStream
.val
- the numeric valueshift
- how many bits to strip from the rightbuffer
- that will contain the encoded chars, must be at least of BUF_SIZE_INT
lengthpublic static String intToPrefixCoded(int val, int shift)
public static String intToPrefixCoded(int val)
public static long prefixCodedToLong(String prefixCoded)
public static int prefixCodedToInt(String prefixCoded)
public static long doubleToSortableLong(double val)
double
value to a sortable signed long
.
The value is converted by getting their IEEE 754 floating-point "double format"
bit layout and then some bits are swapped, to be able to compare the result as long.
By this the precision is not reduced, but the value can easily used as a long.
The sort order (including Double.NaN
) is defined by
Double.compareTo(java.lang.Double)
; NaN
is greater than positive infinity.sortableLongToDouble(long)
public static String doubleToPrefixCoded(double val)
public static double sortableLongToDouble(long val)
long
back to a double
.doubleToSortableLong(double)
public static double prefixCodedToDouble(String val)
public static int floatToSortableInt(float val)
float
value to a sortable signed int
.
The value is converted by getting their IEEE 754 floating-point "float format"
bit layout and then some bits are swapped, to be able to compare the result as int.
By this the precision is not reduced, but the value can easily used as an int.
The sort order (including Float.NaN
) is defined by
Float.compareTo(java.lang.Float)
; NaN
is greater than positive infinity.sortableIntToFloat(int)
public static String floatToPrefixCoded(float val)
public static float sortableIntToFloat(int val)
int
back to a float
.floatToSortableInt(float)
public static float prefixCodedToFloat(String val)
public static void splitLongRange(NumericUtils.LongRangeBuilder builder, int precisionStep, long minBound, long maxBound)
BooleanQuery
for each call to its
NumericUtils.LongRangeBuilder.addRange(String,String)
method.
This method is used by NumericRangeQuery
.
public static void splitIntRange(NumericUtils.IntRangeBuilder builder, int precisionStep, int minBound, int maxBound)
BooleanQuery
for each call to its
NumericUtils.IntRangeBuilder.addRange(String,String)
method.
This method is used by NumericRangeQuery
.