Class Field
- All Implemented Interfaces:
IndexableField
- Direct Known Subclasses:
BinaryDocValuesField
,DoubleField
,FloatField
,IntField
,LongField
,NumericDocValuesField
,SortedDocValuesField
,SortedSetDocValuesField
,StoredField
,StringField
,TextField
IntField
, LongField
, FloatField
, DoubleField
, BinaryDocValuesField
, NumericDocValuesField
, SortedDocValuesField
, StringField
, TextField
, StoredField
.
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.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Deprecated.This is here only to ease transition from the pre-4.0 APIs.static enum
Specifies whether and how a field should be stored.static enum
Deprecated.This is here only to ease transition from the pre-4.0 APIs. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected float
Field's boostprotected Object
Field's valueprotected final String
Field's nameprotected 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 tokenizedprotected final FieldType
Field's type -
Constructor Summary
ConstructorsModifierConstructorDescriptionDeprecated.UseStoredField
instead.Deprecated.UseStoredField
instead.Create field with binary value.Create field with binary value.Deprecated.UseTextField
instead.Field
(String name, Reader reader, Field.TermVector termVector) Deprecated.UseTextField
instead.Create field with Reader value.Field
(String name, String value, Field.Store store, Field.Index index) Deprecated.UseStringField
,TextField
instead.Field
(String name, String value, Field.Store store, Field.Index index, Field.TermVector termVector) Deprecated.UseStringField
,TextField
instead.Create field with String value.Field
(String name, TokenStream tokenStream) Deprecated.UseTextField
insteadField
(String name, TokenStream tokenStream, Field.TermVector termVector) Deprecated.UseTextField
insteadField
(String name, TokenStream tokenStream, FieldType type) Create field with TokenStream value.protected
Expert: creates a field with no initial value.Create field with binary value. -
Method Summary
Modifier and TypeMethodDescriptionNon-null if this field has a binary valuefloat
boost()
Returns the field's index-time boost.Returns theFieldType
for this field.name()
Field nameNon-null if this field has a numeric valueThe 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.The value of the field as a String, or null.tokenStream
(Analyzer analyzer) Creates the TokenStream used for indexing this field.The TokenStream for this field to be used when indexing, or null.toString()
Prints a Field for human consumption.static final FieldType
translateFieldType
(Field.Store store, Field.Index index, Field.TermVector termVector) Deprecated.This is here only to ease transition from the pre-4.0 APIs.
-
Field Details
-
type
Field's type -
name
Field's name -
fieldsData
Field's value -
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 -
boost
protected float boostField's boost- See Also:
-
-
Constructor Details
-
Field
Expert: creates a field with no initial value. Intended only for custom Field subclasses.- Parameters:
name
- field nametype
- field type- Throws:
IllegalArgumentException
- if either the name or type is null.
-
Field
Create field with Reader value.- Parameters:
name
- field namereader
- reader valuetype
- field type- Throws:
IllegalArgumentException
- 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 null
-
Field
Create field with TokenStream value.- Parameters:
name
- field nametokenStream
- TokenStream valuetype
- field type- Throws:
IllegalArgumentException
- 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 null
-
Field
Create field with binary value.NOTE: the provided byte[] is not copied so be sure not to change it until you're done with this field.
- Parameters:
name
- field namevalue
- byte array pointing to binary content (not copied)type
- field type- Throws:
IllegalArgumentException
- if the field name is null, or the field's type is indexed()NullPointerException
- if the type is null
-
Field
Create field with binary value.NOTE: the provided byte[] is not copied so be sure not to change it until you're done with this field.
- Parameters:
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 type- Throws:
IllegalArgumentException
- if the field name is null, or the field's type is indexed()NullPointerException
- if the type is null
-
Field
Create field with binary value.NOTE: the provided BytesRef is not copied so be sure not to change it until you're done with this field.
- Parameters:
name
- field namebytes
- BytesRef pointing to binary content (not copied)type
- field type- Throws:
IllegalArgumentException
- if the field name is null, or the field's type is indexed()NullPointerException
- if the type is null
-
Field
Create field with String value.- Parameters:
name
- field namevalue
- string valuetype
- field type- Throws:
IllegalArgumentException
- 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 null
-
Field
Deprecated.UseStringField
,TextField
instead.Create a field by specifying its name, value and how it will be saved in the index. Term vectors will not be stored in the index.- Parameters:
name
- The name of the fieldvalue
- The string to processstore
- Whethervalue
should be stored in the indexindex
- Whether the field should be indexed, and if so, if it should be tokenized before indexing- Throws:
NullPointerException
- if name or value isnull
IllegalArgumentException
- if the field is neither stored nor indexed
-
Field
@Deprecated public Field(String name, String value, Field.Store store, Field.Index index, Field.TermVector termVector) Deprecated.UseStringField
,TextField
instead.Create a field by specifying its name, value and how it will be saved in the index.- Parameters:
name
- The name of the fieldvalue
- The string to processstore
- Whethervalue
should be stored in the indexindex
- Whether the field should be indexed, and if so, if it should be tokenized before indexingtermVector
- Whether term vector should be stored- Throws:
NullPointerException
- if name or value isnull
IllegalArgumentException
- in any of the following situations:- the field is neither stored nor indexed
- the field is not indexed but termVector is
TermVector.YES
-
Field
Deprecated.UseTextField
instead.Create a tokenized and indexed field that is not stored. Term vectors will not be stored. The Reader is read only when the Document is added to the index, i.e. you may not close the Reader untilIndexWriter.addDocument(java.lang.Iterable<? extends org.apache.lucene.index.IndexableField>)
has been called.- Parameters:
name
- The name of the fieldreader
- The reader with the content- Throws:
NullPointerException
- if name or reader isnull
-
Field
Deprecated.UseTextField
instead.Create a tokenized and indexed field that is not stored, optionally with storing term vectors. The Reader is read only when the Document is added to the index, i.e. you may not close the Reader untilIndexWriter.addDocument(java.lang.Iterable<? extends org.apache.lucene.index.IndexableField>)
has been called.- Parameters:
name
- The name of the fieldreader
- The reader with the contenttermVector
- Whether term vector should be stored- Throws:
NullPointerException
- if name or reader isnull
-
Field
Deprecated.UseTextField
insteadCreate a tokenized and indexed field that is not stored. Term vectors will not be stored. This is useful for pre-analyzed fields. The TokenStream is read only when the Document is added to the index, i.e. you may not close the TokenStream untilIndexWriter.addDocument(java.lang.Iterable<? extends org.apache.lucene.index.IndexableField>)
has been called.- Parameters:
name
- The name of the fieldtokenStream
- The TokenStream with the content- Throws:
NullPointerException
- if name or tokenStream isnull
-
Field
Deprecated.UseTextField
insteadCreate a tokenized and indexed field that is not stored, optionally with storing term vectors. This is useful for pre-analyzed fields. The TokenStream is read only when the Document is added to the index, i.e. you may not close the TokenStream untilIndexWriter.addDocument(java.lang.Iterable<? extends org.apache.lucene.index.IndexableField>)
has been called.- Parameters:
name
- The name of the fieldtokenStream
- The TokenStream with the contenttermVector
- Whether term vector should be stored- Throws:
NullPointerException
- if name or tokenStream isnull
-
Field
Deprecated.UseStoredField
instead.Create a stored field with binary value. Optionally the value may be compressed.- Parameters:
name
- The name of the fieldvalue
- The binary value
-
Field
Deprecated.UseStoredField
instead.Create a stored field with binary value. Optionally the value may be compressed.- Parameters:
name
- The name of the fieldvalue
- The binary valueoffset
- Starting offset in value where this Field's bytes arelength
- Number of bytes to use for this Field, starting at offset
-
-
Method Details
-
stringValue
The value of the field as a String, or null. If null, the Reader value or binary value is used. Exactly one of stringValue(), readerValue(), and getBinaryValue() must be set.- Specified by:
stringValue
in interfaceIndexableField
-
readerValue
The value of the field as a Reader, or null. If null, the String value or binary value is used. Exactly one of stringValue(), readerValue(), and getBinaryValue() must be set.- Specified by:
readerValue
in interfaceIndexableField
-
tokenStreamValue
The TokenStream for this field to be used when indexing, or null. If null, the Reader value or String value is analyzed to produce the indexed tokens. -
setStringValue
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. -
setReaderValue
Expert: change the value of this field. SeesetStringValue(String)
. -
setBytesValue
public void setBytesValue(byte[] value) Expert: change the value of this field. SeesetStringValue(String)
. -
setBytesValue
Expert: change the value of this field. SeesetStringValue(String)
.NOTE: the provided BytesRef is not copied so be sure not to change it until you're done with this field.
-
setByteValue
public void setByteValue(byte value) Expert: change the value of this field. SeesetStringValue(String)
. -
setShortValue
public void setShortValue(short value) Expert: change the value of this field. SeesetStringValue(String)
. -
setIntValue
public void setIntValue(int value) Expert: change the value of this field. SeesetStringValue(String)
. -
setLongValue
public void setLongValue(long value) Expert: change the value of this field. SeesetStringValue(String)
. -
setFloatValue
public void setFloatValue(float value) Expert: change the value of this field. SeesetStringValue(String)
. -
setDoubleValue
public void setDoubleValue(double value) Expert: change the value of this field. SeesetStringValue(String)
. -
setTokenStream
Expert: sets the token stream to be used for indexing and causes isIndexed() and isTokenized() to return true. May be combined with stored values from stringValue() or getBinaryValue() -
name
Description copied from interface:IndexableField
Field name- Specified by:
name
in interfaceIndexableField
-
boost
public float boost()Returns the field's index-time 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 byDefaultSimilarity.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.indexed()
is false) or omits normalization values (IndexableFieldType.omitNorms()
returns true).The default value is
1.0f
(no boost).- Specified by:
boost
in interfaceIndexableField
- See Also:
-
setBoost
public void setBoost(float boost) Sets the boost factor on this field.- Throws:
IllegalArgumentException
- if this field is not indexed, or if it omits norms.- See Also:
-
numericValue
Description copied from interface:IndexableField
Non-null if this field has a numeric value- Specified by:
numericValue
in interfaceIndexableField
-
binaryValue
Description copied from interface:IndexableField
Non-null if this field has a binary value- Specified by:
binaryValue
in interfaceIndexableField
-
toString
Prints a Field for human consumption. -
fieldType
Returns theFieldType
for this field.- Specified by:
fieldType
in interfaceIndexableField
-
tokenStream
Description copied from interface:IndexableField
Creates the TokenStream used for indexing this field. If appropriate, implementations should use the given Analyzer to create the TokenStreams.- Specified by:
tokenStream
in interfaceIndexableField
- Parameters:
analyzer
- Analyzer that should be used to create the TokenStreams from- Returns:
- TokenStream value for indexing the document. Should always return a non-null value if the field is to be indexed
- Throws:
IOException
- Can be thrown while creating the TokenStream
-
translateFieldType
@Deprecated public static final FieldType translateFieldType(Field.Store store, Field.Index index, Field.TermVector termVector) Deprecated.This is here only to ease transition from the pre-4.0 APIs.Translates the pre-4.0 enums for specifying how a field should be indexed into the 4.0FieldType
approach.
-