org.apache.lucene.document

## Class Field

- java.lang.Object
- - org.apache.lucene.document.AbstractField
  - - org.apache.lucene.document.Field

- All Implemented Interfaces:
    java.io.Serializable, Fieldable

      public final class Field
      extends AbstractField
      implements Fieldable, java.io.Serializable

  A field is a section of a Document. Each field has two parts, a name and a value. Values may be free text, provided as a String or as a Reader, or they may be atomic keywords, which are not further processed. Such keywords may be used to represent dates, urls, etc. Fields are optionally stored in the index, so that they may be returned with hits on the document.
  See Also:
    Serialized Form

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `Field.Index` Specifies whether and how a field should be indexed.
    `static class` | `Field.Store` Specifies whether and how a field should be stored.
    `static class` | `Field.TermVector` Specifies whether and how a field should have term vectors.

  - ### Field Summary

    - ### Fields inherited from class org.apache.lucene.document.AbstractField

      `binaryLength, binaryOffset, boost, fieldsData, isBinary, isCompressed, isIndexed, isStored, isTokenized, lazy, name, omitNorms, omitTermFreqAndPositions, storeOffsetWithTermVector, storePositionWithTermVector, storeTermVector, tokenStream`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Field(java.lang.String name, boolean internName, java.lang.String value, Field.Store store, Field.Index index, Field.TermVector termVector)` Create a field by specifying its name, value and how it will be saved in the index.
    `Field(java.lang.String name, byte[] value, Field.Store store)` Create a stored field with binary value.
    `Field(java.lang.String name, byte[] value, int offset, int length, Field.Store store)` Create a stored field with binary value.
    `Field(java.lang.String name, java.io.Reader reader)` Create a tokenized and indexed field that is not stored.
    `Field(java.lang.String name, java.io.Reader reader, Field.TermVector termVector)` Create a tokenized and indexed field that is not stored, optionally with storing term vectors.
    `Field(java.lang.String name, java.lang.String value, Field.Store store, Field.Index index)` Create a field by specifying its name, value and how it will be saved in the index.
    `Field(java.lang.String name, java.lang.String value, Field.Store store, Field.Index index, Field.TermVector termVector)` Create a field by specifying its name, value and how it will be saved in the index.
    `Field(java.lang.String name, TokenStream tokenStream)` Create a tokenized and indexed field that is not stored.
    `Field(java.lang.String name, TokenStream tokenStream, Field.TermVector termVector)` Create a tokenized and indexed field that is not stored, optionally with storing term vectors.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `byte[]` | `binaryValue()` Deprecated. This method must allocate a new byte[] if the `AbstractField.getBinaryOffset()` is non-zero or `AbstractField.getBinaryLength()` is not the full length of the byte[]. Please use `AbstractField.getBinaryValue()` instead, which simply returns the byte[].
    `java.io.Reader` | `readerValue()` The value of the field as a Reader, or null.
    `void` | `setTokenStream(TokenStream tokenStream)` Expert: sets the token stream to be used for indexing and causes isIndexed() and isTokenized() to return true.
    `void` | `setValue(byte[] value)` Expert: change the value of this field.
    `void` | `setValue(byte[] value, int offset, int length)` Expert: change the value of this field.
    `void` | `setValue(java.io.Reader value)` Expert: change the value of this field.
    `void` | `setValue(java.lang.String value)` Expert: change the value of this field.
    `void` | `setValue(TokenStream value)` Deprecated. use `setTokenStream(org.apache.lucene.analysis.TokenStream)`
    `java.lang.String` | `stringValue()` The value of the field as a String, or null.
    `TokenStream` | `tokenStreamValue()` The TokesStream for this field to be used when indexing, or null.

    - ### Methods inherited from class org.apache.lucene.document.AbstractField

      `getBinaryLength, getBinaryOffset, getBinaryValue, getBinaryValue, getBoost, getOmitNorms, getOmitTermFreqAndPositions, getOmitTf, isBinary, isCompressed, isIndexed, isLazy, isStored, isStoreOffsetWithTermVector, isStorePositionWithTermVector, isTermVectorStored, isTokenized, name, setBoost, setOmitNorms, setOmitTermFreqAndPositions, setOmitTf, setStoreTermVector, toString`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface org.apache.lucene.document.Fieldable

      `getBinaryLength, getBinaryOffset, getBinaryValue, getBinaryValue, getBoost, getOmitNorms, getOmitTf, isBinary, isCompressed, isIndexed, isLazy, isStored, isStoreOffsetWithTermVector, isStorePositionWithTermVector, isTermVectorStored, isTokenized, name, setBoost, setOmitNorms, setOmitTf`

- - ### Constructor Detail

    - #### Field

          public Field(java.lang.String name,
                       java.lang.String value,
                       Field.Store store,
                       Field.Index index)

      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 field
        `value` - The string to process
        `store` - Whether `value` should be stored in the index
        `index` - Whether the field should be indexed, and if so, if it should be tokenized before indexing
      Throws:
        `java.lang.NullPointerException` - if name or value is `null`
        `java.lang.IllegalArgumentException` - if the field is neither stored nor indexed

    - #### Field

          public Field(java.lang.String name,
                       java.lang.String value,
                       Field.Store store,
                       Field.Index index,
                       Field.TermVector termVector)

      Create a field by specifying its name, value and how it will be saved in the index.
      Parameters:
        `name` - The name of the field
        `value` - The string to process
        `store` - Whether `value` should be stored in the index
        `index` - Whether the field should be indexed, and if so, if it should be tokenized before indexing
        `termVector` - Whether term vector should be stored
      Throws:
        `java.lang.NullPointerException` - if name or value is `null`
        `java.lang.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

          public Field(java.lang.String name,
                       boolean internName,
                       java.lang.String value,
                       Field.Store store,
                       Field.Index index,
                       Field.TermVector termVector)

      Create a field by specifying its name, value and how it will be saved in the index.
      Parameters:
        `name` - The name of the field
        `internName` - Whether to .intern() name or not
        `value` - The string to process
        `store` - Whether `value` should be stored in the index
        `index` - Whether the field should be indexed, and if so, if it should be tokenized before indexing
        `termVector` - Whether term vector should be stored
      Throws:
        `java.lang.NullPointerException` - if name or value is `null`
        `java.lang.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

          public Field(java.lang.String name,
                       java.io.Reader reader)

      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 until `IndexWriter.addDocument(Document)` has been called.
      Parameters:
        `name` - The name of the field
        `reader` - The reader with the content
      Throws:
        `java.lang.NullPointerException` - if name or reader is `null`

    - #### Field

          public Field(java.lang.String name,
                       java.io.Reader reader,
                       Field.TermVector termVector)

      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 until `IndexWriter.addDocument(Document)` has been called.
      Parameters:
        `name` - The name of the field
        `reader` - The reader with the content
        `termVector` - Whether term vector should be stored
      Throws:
        `java.lang.NullPointerException` - if name or reader is `null`

    - #### Field

          public Field(java.lang.String name,
                       TokenStream tokenStream)

      Create 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 until `IndexWriter.addDocument(Document)` has been called.
      Parameters:
        `name` - The name of the field
        `tokenStream` - The TokenStream with the content
      Throws:
        `java.lang.NullPointerException` - if name or tokenStream is `null`

    - #### Field

          public Field(java.lang.String name,
                       TokenStream tokenStream,
                       Field.TermVector termVector)

      Create 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 until `IndexWriter.addDocument(Document)` has been called.
      Parameters:
        `name` - The name of the field
        `tokenStream` - The TokenStream with the content
        `termVector` - Whether term vector should be stored
      Throws:
        `java.lang.NullPointerException` - if name or tokenStream is `null`

    - #### Field

          public Field(java.lang.String name,
                       byte[] value,
                       Field.Store store)

      Create a stored field with binary value. Optionally the value may be compressed.
      Parameters:
        `name` - The name of the field
        `value` - The binary value
        `store` - How `value` should be stored (compressed or not)
      Throws:
        `java.lang.IllegalArgumentException` - if store is `Store.NO`

    - #### Field

          public Field(java.lang.String name,
                       byte[] value,
                       int offset,
                       int length,
                       Field.Store store)

      Create a stored field with binary value. Optionally the value may be compressed.
      Parameters:
        `name` - The name of the field
        `value` - The binary value
        `offset` - Starting offset in value where this Field's bytes are
        `length` - Number of bytes to use for this Field, starting at offset
        `store` - How `value` should be stored (compressed or not)
      Throws:
        `java.lang.IllegalArgumentException` - if store is `Store.NO`

  - ### Method Detail

    - #### stringValue

          public java.lang.String 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 interface `Fieldable`

    - #### readerValue

          public java.io.Reader 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 interface `Fieldable`
      See Also:
        `Fieldable.stringValue()`

    - #### binaryValue

          public byte[] binaryValue()

      Deprecated. This method must allocate a new byte[] if the `AbstractField.getBinaryOffset()` is non-zero or `AbstractField.getBinaryLength()` is not the full length of the byte[]. Please use `AbstractField.getBinaryValue()` instead, which simply returns the byte[].
      The value of the field in Binary, or null. If null, the Reader value, or String value is used. Exactly one of stringValue(), readerValue(), and getBinaryValue() must be set.
      Specified by:
        `binaryValue` in interface `Fieldable`
      See Also:
        `Fieldable.stringValue()`

    - #### tokenStreamValue

          public TokenStream tokenStreamValue()

      The TokesStream 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.
      Specified by:
        `tokenStreamValue` in interface `Fieldable`
      See Also:
        `Fieldable.stringValue()`

    - #### setValue

          public void setValue(java.lang.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.

    - #### setValue

          public void setValue(java.io.Reader value)

      Expert: change the value of this field. See setValue(String).

    - #### setValue

          public void setValue(byte[] value)

      Expert: change the value of this field. See setValue(String).

    - #### setValue

          public void setValue(byte[] value,
                               int offset,
                               int length)

      Expert: change the value of this field. See setValue(String).

    - #### setValue

          public void setValue(TokenStream value)

      Deprecated. use `setTokenStream(org.apache.lucene.analysis.TokenStream)`
      Expert: change the value of this field. See setValue(String).

    - #### setTokenStream

          public void setTokenStream(TokenStream tokenStream)

      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 binaryValue()

Copyright © 2000-2016 Apache Software Foundation. All Rights Reserved.