StringField (Lucene 10.5.1 core API)
Description: declaration: package: org.apache.lucene.document, class: StringField

Package org.apache.lucene.document

# Class StringField

java.lang.Object
org.apache.lucene.document.Field
org.apache.lucene.document.StringField

All Implemented Interfaces:
  `IndexableField`

public final class StringField extends Field
A field that is indexed but not tokenized: the entire String value is indexed as a single token. For example this might be used for a 'country' field or an 'id' field. If you also need to sort on this field, separately add a `SortedDocValuesField` to your document.

- ## Nested Class Summary

  ## Nested classes/interfaces inherited from class org.apache.lucene.document.Field

  `Field.Store`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final FieldType`
  `TYPE_NOT_STORED`
  Indexed, not tokenized, omits norms, indexes DOCS_ONLY, not stored.
  `static final FieldType`
  `TYPE_STORED`
  Indexed, not tokenized, omits norms, indexes DOCS_ONLY, stored

  ### Fields inherited from class org.apache.lucene.document.Field

  `fieldsData, name, type`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `StringField(String name, String value, Field.Store stored)`
  Creates a new textual StringField, indexing the provided String value as a single token.
  `StringField(String name, BytesRef value, Field.Store stored)`
  Creates a new binary StringField, indexing the provided binary (BytesRef) value as a single token.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `BytesRef`
  `binaryValue()`
  Non-null if this field has a binary value
  `InvertableType`
  `invertableType()`
  Describes how this field should be inverted.
  `void`
  `setBytesValue(BytesRef value)`
  Expert: change the value of this field.
  `void`
  `setStringValue(String value)`
  Expert: change the value of this field.
  `StoredValue`
  `storedValue()`
  Stored value.

  ### Methods inherited from class org.apache.lucene.document.Field

  `fieldType, getCharSequenceValue, name, numericValue, readerValue, setBytesValue, setByteValue, setDoubleValue, setFloatValue, setIntValue, setLongValue, setReaderValue, setShortValue, setTokenStream, stringValue, tokenStream, tokenStreamValue, toString`

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

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

- ## Field Details

  - ### TYPE_NOT_STORED

    public static final FieldType TYPE_NOT_STORED
    Indexed, not tokenized, omits norms, indexes DOCS_ONLY, not stored.

  - ### TYPE_STORED

    public static final FieldType TYPE_STORED
    Indexed, not tokenized, omits norms, indexes DOCS_ONLY, stored

- ## Constructor Details

  - ### StringField

    public StringField(String name, String value, Field.Store stored)
    Creates a new textual StringField, indexing the provided String value as a single token.
    Parameters:
      `name` - field name
      `value` - String value
      `stored` - Store.YES if the content should also be stored
    Throws:
      `IllegalArgumentException` - if the field name or value is null.

  - ### StringField

    public StringField(String name, BytesRef value, Field.Store stored)
    Creates a new binary StringField, indexing the provided binary (BytesRef) value as a single token.
    Parameters:
      `name` - field name
      `value` - BytesRef value. The provided value is not cloned so you must not change it until the document(s) holding it have been indexed.
      `stored` - Store.YES if the content should also be stored
    Throws:
      `IllegalArgumentException` - if the field name or value is null.

- ## Method Details

  - ### invertableType

    public InvertableType invertableType()
    Description copied from interface: `IndexableField`
    Describes how this field should be inverted. This must return a non-null value if the field indexes terms and postings.
    Specified by:
      `invertableType` in interface `IndexableField`
    Overrides:
      `invertableType` in class `Field`

  - ### binaryValue

    public BytesRef binaryValue()
    Description copied from interface: `IndexableField`
    Non-null if this field has a binary value
    Specified by:
      `binaryValue` in interface `IndexableField`
    Overrides:
      `binaryValue` in class `Field`

  - ### setStringValue

    public void setStringValue(String value)
    Description copied from class: `Field`
    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.

    Overrides:
      `setStringValue` in class `Field`

  - ### setBytesValue

    public void setBytesValue(BytesRef value)
    Description copied from class: `Field`
    Expert: change the value of this field. See `Field.setStringValue(String)`.

    NOTE: the provided BytesRef is not copied so be sure not to change it until you're done with this field.

    Overrides:
      `setBytesValue` in class `Field`

  - ### storedValue

    public StoredValue storedValue()
    Description copied from interface: `IndexableField`
    Stored value. This method is called to populate stored fields and must return a non-null value if the field stored.
    Specified by:
      `storedValue` in interface `IndexableField`
    Overrides:
      `storedValue` in class `Field`