public class Field extends Object implements IndexableField
TextField
: Reader
or String
indexed for full-text search
StringField
: String
indexed verbatim as a single token
IntPoint
: int
indexed for exact/range queries.
LongPoint
: long
indexed for exact/range queries.
FloatPoint
: float
indexed for exact/range queries.
DoublePoint
: double
indexed for exact/range queries.
SortedDocValuesField
: byte[]
indexed column-wise for sorting/faceting
SortedSetDocValuesField
: SortedSet<byte[]>
indexed column-wise for sorting/faceting
NumericDocValuesField
: long
indexed column-wise for sorting/faceting
SortedNumericDocValuesField
: SortedSet<long>
indexed column-wise for sorting/faceting
StoredField
: Stored-only value for retrieving in summary results
A field is a section of a Document. Each field has three parts: name, type and value. Values may be text (String, Reader or pre-analyzed TokenStream), binary (byte[]), or numeric (a Number). Fields are optionally stored in the index, so that they may be returned with hits on the document.
NOTE: the field type is an IndexableFieldType
. Making changes
to the state of the IndexableFieldType will impact any
Field it is used in. It is strongly recommended that no
changes be made after Field instantiation.
Modifier and Type | Class and Description |
---|---|
static class |
Field.Store
Specifies whether and how a field should be stored.
|
Modifier and Type | Field and Description |
---|---|
protected float |
boost
Field's boost
|
protected Object |
fieldsData
Field's value
|
protected String |
name
Field's name
|
protected TokenStream |
tokenStream
Pre-analyzed tokenStream for indexed fields; this is
separate from fieldsData because you are allowed to
have both; eg maybe field has a String value but you
customize how it's tokenized
|
protected FieldType |
type
Field's type
|
Modifier | Constructor and Description |
---|---|
|
Field(String name,
byte[] value,
FieldType type)
Create field with binary value.
|
|
Field(String name,
byte[] value,
int offset,
int length,
FieldType type)
Create field with binary value.
|
|
Field(String name,
BytesRef bytes,
FieldType type)
Create field with binary value.
|
protected |
Field(String name,
FieldType type)
Expert: creates a field with no initial value.
|
|
Field(String name,
Reader reader,
FieldType type)
Create field with Reader value.
|
|
Field(String name,
String value,
FieldType type)
Create field with String value.
|
|
Field(String name,
TokenStream tokenStream,
FieldType type)
Create field with TokenStream value.
|
Modifier and Type | Method and Description |
---|---|
BytesRef |
binaryValue()
Non-null if this field has a binary value
|
float |
boost()
Returns the field's index-time boost.
|
FieldType |
fieldType()
Returns the
FieldType for this field. |
String |
name()
Field name
|
Number |
numericValue()
Non-null if this field has a numeric value
|
Reader |
readerValue()
The value of the field as a Reader, or null.
|
void |
setBoost(float boost)
Sets the boost factor on this field.
|
void |
setBytesValue(byte[] value)
Expert: change the value of this field.
|
void |
setBytesValue(BytesRef value)
Expert: change the value of this field.
|
void |
setByteValue(byte value)
Expert: change the value of this field.
|
void |
setDoubleValue(double value)
Expert: change the value of this field.
|
void |
setFloatValue(float value)
Expert: change the value of this field.
|
void |
setIntValue(int value)
Expert: change the value of this field.
|
void |
setLongValue(long value)
Expert: change the value of this field.
|
void |
setReaderValue(Reader value)
Expert: change the value of this field.
|
void |
setShortValue(short value)
Expert: change the value of this field.
|
void |
setStringValue(String value)
Expert: change the value of this field.
|
void |
setTokenStream(TokenStream tokenStream)
Expert: sets the token stream to be used for indexing and causes
isIndexed() and isTokenized() to return true.
|
String |
stringValue()
The value of the field as a String, or null.
|
TokenStream |
tokenStream(Analyzer analyzer,
TokenStream reuse)
Creates the TokenStream used for indexing this field.
|
TokenStream |
tokenStreamValue()
The TokenStream for this field to be used when indexing, or null.
|
String |
toString()
Prints a Field for human consumption.
|
protected final FieldType type
protected final String name
protected Object fieldsData
protected TokenStream tokenStream
protected float boost
boost()
protected Field(String name, FieldType type)
name
- field nametype
- field typeIllegalArgumentException
- if either the name or type
is null.public Field(String name, Reader reader, FieldType type)
name
- field namereader
- reader valuetype
- field typeIllegalArgumentException
- if either the name or type
is null, or if the field's type is stored(), or
if tokenized() is false.NullPointerException
- if the reader is nullpublic Field(String name, TokenStream tokenStream, FieldType type)
name
- field nametokenStream
- TokenStream valuetype
- field typeIllegalArgumentException
- if either the name or type
is null, or if the field's type is stored(), or
if tokenized() is false, or if indexed() is false.NullPointerException
- if the tokenStream is nullpublic Field(String name, byte[] value, FieldType type)
NOTE: the provided byte[] is not copied so be sure not to change it until you're done with this field.
name
- field namevalue
- byte array pointing to binary content (not copied)type
- field typeIllegalArgumentException
- if the field name is null,
or the field's type is indexed()NullPointerException
- if the type is nullpublic Field(String name, byte[] value, int offset, int length, FieldType type)
NOTE: the provided byte[] is not copied so be sure not to change it until you're done with this field.
name
- field namevalue
- byte array pointing to binary content (not copied)offset
- starting position of the byte arraylength
- valid length of the byte arraytype
- field typeIllegalArgumentException
- if the field name is null,
or the field's type is indexed()NullPointerException
- if the type is nullpublic Field(String name, BytesRef bytes, FieldType type)
NOTE: the provided BytesRef is not copied so be sure not to change it until you're done with this field.
name
- field namebytes
- BytesRef pointing to binary content (not copied)type
- field typeIllegalArgumentException
- if the field name is null,
or the field's type is indexed()NullPointerException
- if the type is nullpublic Field(String name, String value, FieldType type)
name
- field namevalue
- string valuetype
- field typeIllegalArgumentException
- if either the name or value
is null, or if the field's type is neither indexed() nor stored(),
or if indexed() is false but storeTermVectors() is true.NullPointerException
- if the type is nullpublic String stringValue()
stringValue
in interface IndexableField
public Reader readerValue()
readerValue
in interface IndexableField
public TokenStream tokenStreamValue()
public void setStringValue(String value)
Expert: change the value of this field. This can be used during indexing to
re-use a single Field instance to improve indexing speed by avoiding GC
cost of new'ing and reclaiming Field instances. Typically a single
Document
instance is re-used as well. This helps most on small
documents.
Each Field instance should only be used once within a single
Document
instance. See ImproveIndexingSpeed for details.
public void setReaderValue(Reader value)
setStringValue(String)
.public void setBytesValue(byte[] value)
setStringValue(String)
.public void setBytesValue(BytesRef value)
setStringValue(String)
.
NOTE: the provided BytesRef is not copied so be sure not to change it until you're done with this field.
public void setByteValue(byte value)
setStringValue(String)
.public void setShortValue(short value)
setStringValue(String)
.public void setIntValue(int value)
setStringValue(String)
.public void setLongValue(long value)
setStringValue(String)
.public void setFloatValue(float value)
setStringValue(String)
.public void setDoubleValue(double value)
setStringValue(String)
.public void setTokenStream(TokenStream tokenStream)
public String name()
IndexableField
name
in interface IndexableField
public float boost()
Only fields can have an index-time boost, if you want to simulate a "document boost", then you must pre-multiply it across all the relevant fields yourself.
The boost is used to compute the norm factor for the field. By
default, in the Similarity.computeNorm(FieldInvertState)
method,
the boost value is multiplied by the length normalization factor and then
rounded by ClassicSimilarity.encodeNormValue(float)
before it is stored in the
index. One should attempt to ensure that this product does not overflow
the range of that encoding.
It is illegal to return a boost other than 1.0f for a field that is not
indexed (IndexableFieldType.indexOptions()
is IndexOptions.NONE) or
omits normalization values (IndexableFieldType.omitNorms()
returns true).
The default value is 1.0f
(no boost).
boost
in interface IndexableField
setBoost(float)
public void setBoost(float boost)
IllegalArgumentException
- if this field is not indexed,
or if it omits norms.boost()
public Number numericValue()
IndexableField
numericValue
in interface IndexableField
public BytesRef binaryValue()
IndexableField
binaryValue
in interface IndexableField
public String toString()
public FieldType fieldType()
FieldType
for this field.fieldType
in interface IndexableField
public TokenStream tokenStream(Analyzer analyzer, TokenStream reuse)
IndexableField
tokenStream
in interface IndexableField
analyzer
- Analyzer that should be used to create the TokenStreams fromreuse
- TokenStream for a previous instance of this field name. This allows
custom field types (like StringField and NumericField) that do not use
the analyzer to still have good performance. Note: the passed-in type
may be inappropriate, for example if you mix up different types of Fields
for the same field name. So it's the responsibility of the implementation to
check.Copyright © 2000-2016 Apache Software Foundation. All Rights Reserved.