public final class NumericTokenStream extends TokenStream
TokenStream
for indexing numeric values that can be used by NumericRangeQuery
or NumericRangeFilter
.
Note that for simple usage, IntField
, LongField
, FloatField
or DoubleField
is
recommended. These fields disable norms and
term freqs, as they are not usually needed during
searching. If you need to change these settings, you
should use this class.
Here's an example usage, for an int
field:
FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED); fieldType.setOmitNorms(true); fieldType.setIndexOptions(IndexOptions.DOCS_ONLY); Field field = new Field(name, new NumericTokenStream(precisionStep).setIntValue(value), fieldType); document.add(field);
For optimal performance, re-use the TokenStream and Field instance for more than one document:
NumericTokenStream stream = new NumericTokenStream(precisionStep); FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED); fieldType.setOmitNorms(true); fieldType.setIndexOptions(IndexOptions.DOCS_ONLY); Field field = new Field(name, stream, fieldType); Document document = new Document(); document.add(field); for(all documents) { stream.setIntValue(value) writer.addDocument(document); }
This stream is not intended to be used in analyzers; it's more for iterating the different precisions during indexing a specific numeric value.
NOTE: as token streams are only consumed once
the document is added to the index, if you index more
than one numeric field, use a separate NumericTokenStream
instance for each.
See NumericRangeQuery
for more details on the
precisionStep
parameter as well as how numeric fields work under the hood.
Modifier and Type | Class and Description |
---|---|
static interface |
NumericTokenStream.NumericTermAttribute
Expert: Use this attribute to get the details of the currently generated token.
|
static class |
NumericTokenStream.NumericTermAttributeImpl
Implementation of
NumericTokenStream.NumericTermAttribute . |
AttributeSource.AttributeFactory, AttributeSource.State
Modifier and Type | Field and Description |
---|---|
static String |
TOKEN_TYPE_FULL_PREC
The full precision token gets this token type assigned.
|
static String |
TOKEN_TYPE_LOWER_PREC
The lower precision tokens gets this token type assigned.
|
Constructor and Description |
---|
NumericTokenStream()
Creates a token stream for numeric values using the default
precisionStep
NumericUtils.PRECISION_STEP_DEFAULT (4). |
NumericTokenStream(AttributeSource.AttributeFactory factory,
int precisionStep)
Expert: Creates a token stream for numeric values with the specified
precisionStep using the given
AttributeSource.AttributeFactory . |
NumericTokenStream(int precisionStep)
Creates a token stream for numeric values with the specified
precisionStep . |
Modifier and Type | Method and Description |
---|---|
int |
getPrecisionStep()
Returns the precision step.
|
boolean |
incrementToken()
Consumers (i.e.,
IndexWriter ) use this method to advance the stream to
the next token. |
void |
reset()
This method is called by a consumer before it begins consumption using
TokenStream.incrementToken() . |
NumericTokenStream |
setDoubleValue(double value)
Initializes the token stream with the supplied
double value. |
NumericTokenStream |
setFloatValue(float value)
Initializes the token stream with the supplied
float value. |
NumericTokenStream |
setIntValue(int value)
Initializes the token stream with the supplied
int value. |
NumericTokenStream |
setLongValue(long value)
Initializes the token stream with the supplied
long value. |
close, end
addAttribute, addAttributeImpl, captureState, clearAttributes, cloneAttributes, copyTo, equals, getAttribute, getAttributeClassesIterator, getAttributeFactory, getAttributeImplsIterator, hasAttribute, hasAttributes, hashCode, reflectAsString, reflectWith, restoreState
public static final String TOKEN_TYPE_FULL_PREC
public static final String TOKEN_TYPE_LOWER_PREC
public NumericTokenStream()
precisionStep
NumericUtils.PRECISION_STEP_DEFAULT
(4). The stream is not yet initialized,
before using set a value using the various set???Value() methods.public NumericTokenStream(int precisionStep)
precisionStep
. The stream is not yet initialized,
before using set a value using the various set???Value() methods.public NumericTokenStream(AttributeSource.AttributeFactory factory, int precisionStep)
precisionStep
using the given
AttributeSource.AttributeFactory
.
The stream is not yet initialized,
before using set a value using the various set???Value() methods.public NumericTokenStream setLongValue(long value)
long
value.value
- the value, for which this TokenStream should enumerate tokens.new Field(name, new NumericTokenStream(precisionStep).setLongValue(value))
public NumericTokenStream setIntValue(int value)
int
value.value
- the value, for which this TokenStream should enumerate tokens.new Field(name, new NumericTokenStream(precisionStep).setIntValue(value))
public NumericTokenStream setDoubleValue(double value)
double
value.value
- the value, for which this TokenStream should enumerate tokens.new Field(name, new NumericTokenStream(precisionStep).setDoubleValue(value))
public NumericTokenStream setFloatValue(float value)
float
value.value
- the value, for which this TokenStream should enumerate tokens.new Field(name, new NumericTokenStream(precisionStep).setFloatValue(value))
public void reset()
TokenStream
TokenStream.incrementToken()
.
Resets this stream to a clean state. Stateful implementations must implement
this method so that they can be reused, just as if they had been created fresh.reset
in class TokenStream
public boolean incrementToken()
TokenStream
IndexWriter
) use this method to advance the stream to
the next token. Implementing classes must implement this method and update
the appropriate AttributeImpl
s with the attributes of the next
token.
The producer must make no assumptions about the attributes after the method
has been returned: the caller may arbitrarily change it. If the producer
needs to preserve the state for subsequent calls, it can use
AttributeSource.captureState()
to create a copy of the current attribute state.
This method is called for every token of a document, so an efficient
implementation is crucial for good performance. To avoid calls to
AttributeSource.addAttribute(Class)
and AttributeSource.getAttribute(Class)
,
references to all AttributeImpl
s that this stream uses should be
retrieved during instantiation.
To ensure that filters and consumers know which attributes are available,
the attributes must be added during instantiation. Filters and consumers
are not required to check for availability of attributes in
TokenStream.incrementToken()
.
incrementToken
in class TokenStream
public int getPrecisionStep()
Copyright © 2000-2012 Apache Software Foundation. All Rights Reserved.