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.
| Constructor: |
public Field(String name,
Reader reader) {
this(name, reader, TermVector.NO);
}
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:
NullPointerException - if name or reader is null
|
public Field(String name,
TokenStream tokenStream) {
this(name, tokenStream, TermVector.NO);
}
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:
NullPointerException - if name or tokenStream is null
|
public Field(String name,
Reader reader,
Field.TermVector termVector) {
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);
}
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:
NullPointerException - if name or reader is null
|
public Field(String name,
TokenStream tokenStream,
Field.TermVector termVector) {
if (name == null)
throw new NullPointerException("name cannot be null");
if (tokenStream == null)
throw new NullPointerException("tokenStream cannot be null");
this.name = name.intern(); // field names are interned
this.fieldsData = tokenStream;
this.isStored = false;
this.isCompressed = false;
this.isIndexed = true;
this.isTokenized = true;
this.isBinary = false;
setStoreTermVector(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:
NullPointerException - if name or tokenStream is null
|
public Field(String name,
byte[] value,
Field.Store store) {
this(name, value, 0, value.length, 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:
IllegalArgumentException - if store is Store.NO
|
public Field(String name,
String value,
Field.Store store,
Field.Index index) {
this(name, value, store, index, TermVector.NO);
}
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:
NullPointerException - if name or value is null
IllegalArgumentException - if the field is neither stored nor indexed
|
public Field(String name,
String value,
Field.Store store,
Field.Index index,
Field.TermVector termVector) {
if (name == null)
throw new NullPointerException("name cannot be null");
if (value == null)
throw new NullPointerException("value cannot be null");
if (name.length() == 0 && value.length() == 0)
throw new IllegalArgumentException("name and value cannot both be empty");
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.ANALYZED) {
this.isIndexed = true;
this.isTokenized = true;
} else if (index == Index.NOT_ANALYZED) {
this.isIndexed = true;
this.isTokenized = false;
} else if (index == Index.NOT_ANALYZED_NO_NORMS) {
this.isIndexed = true;
this.isTokenized = false;
this.omitNorms = true;
} else if (index == Index.ANALYZED_NO_NORMS) {
this.isIndexed = true;
this.isTokenized = true;
this.omitNorms = true;
} else {
throw new IllegalArgumentException("unknown index parameter " + index);
}
this.isBinary = false;
setStoreTermVector(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:
NullPointerException - if name or value is null
IllegalArgumentException - in any of the following situations:
- the field is neither stored nor indexed
- the field is not indexed but termVector is
TermVector.YES
|
public Field(String name,
byte[] value,
int offset,
int length,
Field.Store store) {
if (name == null)
throw new IllegalArgumentException("name cannot be null");
if (value == null)
throw new IllegalArgumentException("value cannot be null");
this.name = name.intern();
fieldsData = value;
if (store == Store.YES) {
isStored = true;
isCompressed = false;
}
else if (store == Store.COMPRESS) {
isStored = true;
isCompressed = true;
}
else if (store == Store.NO)
throw new IllegalArgumentException("binary values can't be unstored");
else
throw new IllegalArgumentException("unknown store parameter " + store);
isIndexed = false;
isTokenized = false;
isBinary = true;
binaryLength = length;
binaryOffset = offset;
setStoreTermVector(TermVector.NO);
}
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:
IllegalArgumentException - if store is Store.NO
|
| Method from org.apache.lucene.document.Field Detail: |
public byte[] binaryValue() {
if (!isBinary)
return null;
final byte[] data = (byte[]) fieldsData;
if (binaryOffset == 0 && data.length == binaryLength)
return data; //Optimization
final byte[] ret = new byte[binaryLength];
System.arraycopy(data, binaryOffset, ret, 0, binaryLength);
return ret;
} 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,
String value, or TokenStream value is used. Exactly one of stringValue(),
readerValue(), getBinaryValue(), and tokenStreamValue() must be set. |
public Reader readerValue() {
return fieldsData instanceof Reader ? (Reader)fieldsData : null;
}
The value of the field as a Reader, or null. If null, the String value,
binary value, or TokenStream value is used. Exactly one of stringValue(),
readerValue(), getBinaryValue(), and tokenStreamValue() must be set. |
public void setValue(String value) {
fieldsData = 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.
Note that you should only use this method after the
Field has been consumed (ie, the Document
containing this Field has been added to the index).
Also, each Field instance should only be used once
within a single Document instance. See ImproveIndexingSpeed
for details.
|
public void setValue(Reader value) {
fieldsData = value;
}
|
public void setValue(byte[] value) {
fieldsData = value;
binaryLength = value.length;
binaryOffset = 0;
}
|
public void setValue(TokenStream value) {
fieldsData = value;
}
|
public void setValue(byte[] value,
int offset,
int length) {
fieldsData = value;
binaryLength = length;
binaryOffset = offset;
}
|
public String stringValue() {
return fieldsData instanceof String ? (String)fieldsData : null;
}
The value of the field as a String, or null. If null, the Reader value,
binary value, or TokenStream value is used. Exactly one of stringValue(),
readerValue(), getBinaryValue(), and tokenStreamValue() must be set. |
public TokenStream tokenStreamValue() {
return fieldsData instanceof TokenStream ? (TokenStream)fieldsData : null;
}
The value of the field as a TokesStream, or null. If null, the Reader value,
String value, or binary value is used. Exactly one of stringValue(),
readerValue(), getBinaryValue(), and tokenStreamValue() must be set. |