FileDocCategorySizeDatePackage
Field.javaAPI DocApache Lucene 1.923587Mon Feb 20 09:20:18 GMT 2006org.apache.lucene.document

Field

public final class Field extends Object implements 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.

Fields Summary
private String
name
private Object
fieldsData
private boolean
storeTermVector
private boolean
storeOffsetWithTermVector
private boolean
storePositionWithTermVector
private boolean
omitNorms
private boolean
isStored
private boolean
isIndexed
private boolean
isTokenized
private boolean
isBinary
private boolean
isCompressed
private float
boost
Constructors Summary
public Field(String name, String value, Store store, 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.

param
name The name of the field
param
value The string to process
param
store Whether value should be stored in the index
param
index Whether the field should be indexed, and if so, if it should be tokenized before indexing
throws
NullPointerException if name or value is null
throws
IllegalArgumentException if the field is neither stored nor indexed

    this(name, value, store, index, TermVector.NO);
  
public Field(String name, String value, Store store, Index index, TermVector termVector)
Create a field by specifying its name, value and how it will be saved in the index.

param
name The name of the field
param
value The string to process
param
store Whether value should be stored in the index
param
index Whether the field should be indexed, and if so, if it should be tokenized before indexing
param
termVector Whether term vector should be stored
throws
NullPointerException if name or value is null
throws
IllegalArgumentException in any of the following situations:
  • the field is neither stored nor indexed
  • the field is not indexed but termVector is TermVector.YES

    if (name == null)
      throw new NullPointerException("name cannot be null");
    if (value == null)
      throw new NullPointerException("value cannot be null");
    if (index == Index.NO && store == Store.NO)
      throw new IllegalArgumentException("it doesn't make sense to have a field that "
         + "is neither indexed nor stored");
    if (index == Index.NO && termVector != TermVector.NO)
      throw new IllegalArgumentException("cannot store term vector information "
         + "for a field that is not indexed");
          
    this.name = name.intern();        // field names are interned
    this.fieldsData = value;

    if (store == Store.YES){
      this.isStored = true;
      this.isCompressed = false;
    }
    else if (store == Store.COMPRESS) {
      this.isStored = true;
      this.isCompressed = true;
    }
    else if (store == Store.NO){
      this.isStored = false;
      this.isCompressed = false;
    }
    else
      throw new IllegalArgumentException("unknown store parameter " + store);
   
    if (index == Index.NO) {
      this.isIndexed = false;
      this.isTokenized = false;
    } else if (index == Index.TOKENIZED) {
      this.isIndexed = true;
      this.isTokenized = true;
    } else if (index == Index.UN_TOKENIZED) {
      this.isIndexed = true;
      this.isTokenized = false;
    } else if (index == Index.NO_NORMS) {
      this.isIndexed = true;
      this.isTokenized = false;
      this.omitNorms = true;
    } else {
      throw new IllegalArgumentException("unknown index parameter " + index);
    }
    
    this.isBinary = false;

    setStoreTermVector(termVector);
  
public Field(String name, Reader reader)
Create a tokenized and indexed field that is not stored. Term vectors will not be stored.

param
name The name of the field
param
reader The reader with the content
throws
NullPointerException if name or reader is null

    this(name, reader, TermVector.NO);
  
public Field(String name, Reader reader, TermVector termVector)
Create a tokenized and indexed field that is not stored, optionally with storing term vectors.

param
name The name of the field
param
reader The reader with the content
param
termVector Whether term vector should be stored
throws
NullPointerException if name or reader is null

    if (name == null)
      throw new NullPointerException("name cannot be null");
    if (reader == null)
      throw new NullPointerException("reader cannot be null");
    
    this.name = name.intern();        // field names are interned
    this.fieldsData = reader;
    
    this.isStored = false;
    this.isCompressed = false;
    
    this.isIndexed = true;
    this.isTokenized = true;
    
    this.isBinary = false;
    
    setStoreTermVector(termVector);
  
public Field(String name, String string, boolean store, boolean index, boolean token)
Create a field by specifying all parameters except for storeTermVector, which is set to false.

deprecated
use {@link #Field(String, String, Field.Store, Field.Index)} instead

    this(name, string, store, index, token, false);
  
public Field(String name, byte[] value, Store store)
Create a stored field with binary value. Optionally the value may be compressed.

param
name The name of the field
param
value The binary value
param
store How value should be stored (compressed or not)
throws
IllegalArgumentException if store is Store.NO

    if (name == null)
      throw new IllegalArgumentException("name cannot be null");
    if (value == null)
      throw new IllegalArgumentException("value cannot be null");
    
    this.name = name.intern();
    this.fieldsData = value;
    
    if (store == Store.YES){
      this.isStored = true;
      this.isCompressed = false;
    }
    else if (store == Store.COMPRESS) {
      this.isStored = true;
      this.isCompressed = true;
    }
    else if (store == Store.NO)
      throw new IllegalArgumentException("binary values can't be unstored");
    else
      throw new IllegalArgumentException("unknown store parameter " + store);
    
    this.isIndexed   = false;
    this.isTokenized = false;
    
    this.isBinary    = true;
    
    setStoreTermVector(TermVector.NO);
  
public Field(String name, String string, boolean store, boolean index, boolean token, boolean storeTermVector)

param
name The name of the field
param
string The string to process
param
store true if the field should store the string
param
index true if the field should be indexed
param
token true if the field should be tokenized
param
storeTermVector true if we should store the Term Vector info
deprecated
use {@link #Field(String, String, Field.Store, Field.Index, Field.TermVector)} instead

    if (name == null)
      throw new NullPointerException("name cannot be null");
    if (string == null)
      throw new NullPointerException("value cannot be null");
    if (!index && storeTermVector)
      throw new IllegalArgumentException("cannot store a term vector for fields that are not indexed");

    this.name = name.intern();        // field names are interned
    this.fieldsData = string;
    this.isStored = store;
    this.isIndexed = index;
    this.isTokenized = token;
    this.storeTermVector = storeTermVector;
  
Methods Summary
public static final org.apache.lucene.document.FieldKeyword(java.lang.String name, java.lang.String value)
Constructs a String-valued Field that is not tokenized, but is indexed and stored. Useful for non-text fields, e.g. date or url.

deprecated
use {@link #Field(String, String, Field.Store, Field.Index) Field(name, value, Field.Store.YES, Field.Index.UN_TOKENIZED)} instead

    return new Field(name, value, true, true, false);
  
public static final org.apache.lucene.document.FieldKeyword(java.lang.String name, java.util.Date value)
Constructs a Date-valued Field that is not tokenized and is indexed, and stored in the index, for return with hits.

deprecated
use {@link #Field(String, String, Field.Store, Field.Index) Field(name, value, Field.Store.YES, Field.Index.UN_TOKENIZED)} instead

    return new Field(name, DateField.dateToString(value), true, true, false);
  
public static final org.apache.lucene.document.FieldText(java.lang.String name, java.io.Reader value)
Constructs a Reader-valued Field that is tokenized and indexed, but is not stored in the index verbatim. Useful for longer text fields, like "body". Term vector will not be stored for this field.

deprecated
use {@link #Field(String, Reader) Field(name, value)} instead

    return Text(name, value, false);
  
public static final org.apache.lucene.document.FieldText(java.lang.String name, java.io.Reader value, boolean storeTermVector)
Constructs a Reader-valued Field that is tokenized and indexed, but is not stored in the index verbatim. Useful for longer text fields, like "body".

deprecated
use {@link #Field(String, Reader, Field.TermVector) Field(name, value, storeTermVector)} instead

    Field f = new Field(name, value);
    f.storeTermVector = storeTermVector;
    return f;
  
public static final org.apache.lucene.document.FieldText(java.lang.String name, java.lang.String value)
Constructs a String-valued Field that is tokenized and indexed, and is stored in the index, for return with hits. Useful for short text fields, like "title" or "subject". Term vector will not be stored for this field.

deprecated
use {@link #Field(String, String, Field.Store, Field.Index) Field(name, value, Field.Store.YES, Field.Index.TOKENIZED)} instead

    return Text(name, value, false);
  
public static final org.apache.lucene.document.FieldText(java.lang.String name, java.lang.String value, boolean storeTermVector)
Constructs a String-valued Field that is tokenized and indexed, and is stored in the index, for return with hits. Useful for short text fields, like "title" or "subject".

deprecated
use {@link #Field(String, String, Field.Store, Field.Index, Field.TermVector) Field(name, value, Field.Store.YES, Field.Index.TOKENIZED, storeTermVector)} instead

    return new Field(name, value, true, true, true, storeTermVector);
  
public static final org.apache.lucene.document.FieldUnIndexed(java.lang.String name, java.lang.String value)
Constructs a String-valued Field that is not tokenized nor indexed, but is stored in the index, for return with hits.

deprecated
use {@link #Field(String, String, Field.Store, Field.Index) Field(name, value, Field.Store.YES, Field.Index.NO)} instead

    return new Field(name, value, true, false, false);
  
public static final org.apache.lucene.document.FieldUnStored(java.lang.String name, java.lang.String value)
Constructs a String-valued Field that is tokenized and indexed, but that is not stored in the index. Term vector will not be stored for this field.

deprecated
use {@link #Field(String, String, Field.Store, Field.Index) Field(name, value, Field.Store.NO, Field.Index.TOKENIZED)} instead

    return UnStored(name, value, false);
  
public static final org.apache.lucene.document.FieldUnStored(java.lang.String name, java.lang.String value, boolean storeTermVector)
Constructs a String-valued Field that is tokenized and indexed, but that is not stored in the index.

deprecated
use {@link #Field(String, String, Field.Store, Field.Index, Field.TermVector) Field(name, value, Field.Store.NO, Field.Index.TOKENIZED, storeTermVector)} instead

    return new Field(name, value, false, true, true, storeTermVector);
  
public byte[]binaryValue()
The value of the field in Binary, or null. If null, the Reader or String value is used. Exactly one of stringValue(), readerValue() and binaryValue() must be set.

 return fieldsData instanceof byte[] ? (byte[])fieldsData : null; 
public floatgetBoost()
Returns the boost factor for hits for this field.

The default value is 1.0.

Note: this value is not stored directly with the document in the index. Documents returned from {@link IndexReader#document(int)} and {@link Hits#doc(int)} may thus not have the same value present as when this field was indexed.

see
#setBoost(float)

    return boost;
  
public booleangetOmitNorms()
True if norms are omitted for this indexed field

 return omitNorms; 
public final booleanisBinary()
True iff the value of the filed is stored as binary

 return isBinary; 
public final booleanisCompressed()
True if the value of the field is stored and compressed within the index

 return isCompressed; 
public final booleanisIndexed()
True iff the value of the field is to be indexed, so that it may be searched on.

 return isIndexed; 
public booleanisStoreOffsetWithTermVector()
True iff terms are stored as term vector together with their offsets (start and end positon in source text).

 
    return storeOffsetWithTermVector; 
  
public booleanisStorePositionWithTermVector()
True iff terms are stored as term vector together with their token positions.

 
    return storePositionWithTermVector; 
  
public final booleanisStored()
True iff the value of the field is to be stored in the index for return with search hits. It is an error for this to be true if a field is Reader-valued.

 return isStored; 
public final booleanisTermVectorStored()
True iff the term or terms used to index this field are stored as a term vector, available from {@link IndexReader#getTermFreqVector(int,String)}. These methods do not provide access to the original content of the field, only to terms used to index it. If the original content must be preserved, use the stored attribute instead.

see
IndexReader#getTermFreqVector(int, String)

 return storeTermVector; 
public final booleanisTokenized()
True iff the value of the field should be tokenized as text prior to indexing. Un-tokenized fields are indexed as a single word and may not be Reader-valued.

 return isTokenized; 
public java.lang.Stringname()
Returns the name of the field as an interned string. For example "date", "title", "body", ...

 return name; 
public java.io.ReaderreaderValue()
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 binaryValue() must be set.

 return fieldsData instanceof Reader ? (Reader)fieldsData : null; 
public voidsetBoost(float boost)
Sets the boost factor hits on this field. This value will be multiplied into the score of all hits on this this field of this document.

The boost is multiplied by {@link Document#getBoost()} of the document containing this field. If a document has multiple fields with the same name, all such values are multiplied together. This product is then multipled by the value {@link Similarity#lengthNorm(String,int)}, and rounded by {@link Similarity#encodeNorm(float)} before it is stored in the index. One should attempt to ensure that this product does not overflow the range of that encoding.

see
Document#setBoost(float)
see
Similarity#lengthNorm(String, int)
see
Similarity#encodeNorm(float)

  
  
                                                                                                             
      
    this.boost = boost;
  
public voidsetOmitNorms(boolean omitNorms)
Expert: If set, omit normalization factors associated with this indexed field. This effectively disables indexing boosts and length normalization for this field.

 this.omitNorms=omitNorms; 
private voidsetStoreTermVector(org.apache.lucene.document.Field$TermVector termVector)

    if (termVector == TermVector.NO) {
      this.storeTermVector = false;
      this.storePositionWithTermVector = false;
      this.storeOffsetWithTermVector = false;
    } 
    else if (termVector == TermVector.YES) {
      this.storeTermVector = true;
      this.storePositionWithTermVector = false;
      this.storeOffsetWithTermVector = false;
    }
    else if (termVector == TermVector.WITH_POSITIONS) {
      this.storeTermVector = true;
      this.storePositionWithTermVector = true;
      this.storeOffsetWithTermVector = false;
    } 
    else if (termVector == TermVector.WITH_OFFSETS) {
      this.storeTermVector = true;
      this.storePositionWithTermVector = false;
      this.storeOffsetWithTermVector = true;
    } 
    else if (termVector == TermVector.WITH_POSITIONS_OFFSETS) {
      this.storeTermVector = true;
      this.storePositionWithTermVector = true;
      this.storeOffsetWithTermVector = true;
    } 
    else {
      throw new IllegalArgumentException("unknown termVector parameter " + termVector);
    }
  
public java.lang.StringstringValue()
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 binaryValue() must be set.

 return fieldsData instanceof String ? (String)fieldsData : null; 
public final java.lang.StringtoString()
Prints a Field for human consumption.

    StringBuffer result = new StringBuffer();
    if (isStored) {
      result.append("stored");
      if (isCompressed)
        result.append("/compressed");
      else
        result.append("/uncompressed");
    }
    if (isIndexed) {
      if (result.length() > 0)
        result.append(",");
      result.append("indexed");
    }
    if (isTokenized) {
      if (result.length() > 0)
        result.append(",");
      result.append("tokenized");
    }
    if (storeTermVector) {
      if (result.length() > 0)
        result.append(",");
      result.append("termVector");
    }
    if (storeOffsetWithTermVector) { 
      if (result.length() > 0) 
        result.append(","); 
      result.append("termVectorOffsets"); 
    } 
    if (storePositionWithTermVector) { 
      if (result.length() > 0) 
        result.append(","); 
      result.append("termVectorPosition"); 
    } 
    if (isBinary) {
      if (result.length() > 0)
        result.append(",");
      result.append("binary");
    }
    if (omitNorms) {
      result.append(",omitNorms");
    }
    result.append('<");
    result.append(name);
    result.append(':");
    
    if (fieldsData != null) {
      result.append(fieldsData);
    }
    
    result.append('>");
    return result.toString();