1 package org.apache.lucene.document;
3 * Copyright 2006 The Apache Software Foundation
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 import org.apache.lucene.search.PhraseQuery; // for javadocs
19 import org.apache.lucene.search.spans.SpanQuery; // for javadocs
20 import org.apache.lucene.analysis.TokenStream;
21 import org.apache.lucene.index.FieldInvertState;
22 import org.apache.lucene.util.StringHelper; // for javadocs
23 import org.apache.lucene.index.FieldInfo.IndexOptions;
24 import org.apache.lucene.index.FieldInvertState; // for javadocs
31 public abstract class AbstractField implements Fieldable {
33 protected String name = "body";
34 protected boolean storeTermVector = false;
35 protected boolean storeOffsetWithTermVector = false;
36 protected boolean storePositionWithTermVector = false;
37 protected boolean omitNorms = false;
38 protected boolean isStored = false;
39 protected boolean isIndexed = true;
40 protected boolean isTokenized = true;
41 protected boolean isBinary = false;
42 protected boolean lazy = false;
43 protected IndexOptions indexOptions = IndexOptions.DOCS_AND_FREQS_AND_POSITIONS;
44 protected float boost = 1.0f;
45 // the data object for all different kind of field values
46 protected Object fieldsData = null;
47 // pre-analyzed tokenStream for indexed fields
48 protected TokenStream tokenStream;
49 // length/offset for all primitive types
50 protected int binaryLength;
51 protected int binaryOffset;
53 protected AbstractField()
57 protected AbstractField(String name, Field.Store store, Field.Index index, Field.TermVector termVector) {
59 throw new NullPointerException("name cannot be null");
60 this.name = StringHelper.intern(name); // field names are interned
62 this.isStored = store.isStored();
63 this.isIndexed = index.isIndexed();
64 this.isTokenized = index.isAnalyzed();
65 this.omitNorms = index.omitNorms();
67 this.isBinary = false;
69 setStoreTermVector(termVector);
72 /** Sets the boost factor hits on this field. This value will be
73 * multiplied into the score of all hits on this this field of this
76 * <p>The boost is multiplied by {@link org.apache.lucene.document.Document#getBoost()} of the document
77 * containing this field. If a document has multiple fields with the same
78 * name, all such values are multiplied together. This product is then
79 * used to compute the norm factor for the field. By
80 * default, in the {@link
81 * org.apache.lucene.search.Similarity#computeNorm(String,
82 * FieldInvertState)} method, the boost value is multiplied
84 * org.apache.lucene.search.Similarity#lengthNorm(String,
86 * rounded by {@link org.apache.lucene.search.Similarity#encodeNormValue(float)} before it is stored in the
87 * index. One should attempt to ensure that this product does not overflow
88 * the range of that encoding.
90 * @see org.apache.lucene.document.Document#setBoost(float)
91 * @see org.apache.lucene.search.Similarity#computeNorm(String, FieldInvertState)
92 * @see org.apache.lucene.search.Similarity#encodeNormValue(float)
94 public void setBoost(float boost) {
98 /** Returns the boost factor for hits for this field.
100 * <p>The default value is 1.0.
102 * <p>Note: this value is not stored directly with the document in the index.
103 * Documents returned from {@link org.apache.lucene.index.IndexReader#document(int)} and
104 * {@link org.apache.lucene.search.Searcher#doc(int)} may thus not have the same value present as when
105 * this field was indexed.
107 * @see #setBoost(float)
109 public float getBoost() {
113 /** Returns the name of the field as an interned string.
114 * For example "date", "title", "body", ...
116 public String name() { return name; }
118 protected void setStoreTermVector(Field.TermVector termVector) {
119 this.storeTermVector = termVector.isStored();
120 this.storePositionWithTermVector = termVector.withPositions();
121 this.storeOffsetWithTermVector = termVector.withOffsets();
124 /** True iff the value of the field is to be stored in the index for return
125 with search hits. It is an error for this to be true if a field is
127 public final boolean isStored() { return isStored; }
129 /** True iff the value of the field is to be indexed, so that it may be
131 public final boolean isIndexed() { return isIndexed; }
133 /** True iff the value of the field should be tokenized as text prior to
134 indexing. Un-tokenized fields are indexed as a single word and may not be
136 public final boolean isTokenized() { return isTokenized; }
138 /** True iff the term or terms used to index this field are stored as a term
139 * vector, available from {@link org.apache.lucene.index.IndexReader#getTermFreqVector(int,String)}.
140 * These methods do not provide access to the original content of the field,
141 * only to terms used to index it. If the original content must be
142 * preserved, use the <code>stored</code> attribute instead.
144 * @see org.apache.lucene.index.IndexReader#getTermFreqVector(int, String)
146 public final boolean isTermVectorStored() { return storeTermVector; }
149 * True iff terms are stored as term vector together with their offsets
150 * (start and end position in source text).
152 public boolean isStoreOffsetWithTermVector(){
153 return storeOffsetWithTermVector;
157 * True iff terms are stored as term vector together with their token positions.
159 public boolean isStorePositionWithTermVector(){
160 return storePositionWithTermVector;
163 /** True iff the value of the filed is stored as binary */
164 public final boolean isBinary() {
170 * Return the raw byte[] for the binary field. Note that
171 * you must also call {@link #getBinaryLength} and {@link
172 * #getBinaryOffset} to know which range of bytes in this
173 * returned array belong to the field.
174 * @return reference to the Field value as byte[].
176 public byte[] getBinaryValue() {
177 return getBinaryValue(null);
180 public byte[] getBinaryValue(byte[] result){
181 if (isBinary || fieldsData instanceof byte[])
182 return (byte[]) fieldsData;
188 * Returns length of byte[] segment that is used as value, if Field is not binary
189 * returned value is undefined
190 * @return length of byte[] segment that represents this Field value
192 public int getBinaryLength() {
195 } else if (fieldsData instanceof byte[])
196 return ((byte[]) fieldsData).length;
202 * Returns offset into byte[] segment that is used as value, if Field is not binary
203 * returned value is undefined
204 * @return index of the first character in byte[] segment that represents this Field value
206 public int getBinaryOffset() {
210 /** True if norms are omitted for this indexed field */
211 public boolean getOmitNorms() { return omitNorms; }
213 /** @deprecated use {@link #getIndexOptions()} instead. */
215 public boolean getOmitTermFreqAndPositions() { return indexOptions == IndexOptions.DOCS_ONLY; }
217 /** @see #setIndexOptions */
218 public IndexOptions getIndexOptions() { return indexOptions; }
222 * If set, omit normalization factors associated with this indexed field.
223 * This effectively disables indexing boosts and length normalization for this field.
225 public void setOmitNorms(boolean omitNorms) { this.omitNorms=omitNorms; }
227 /** @deprecated use {@link #setIndexOptions(FieldInfo.IndexOptions)} instead. */
229 public void setOmitTermFreqAndPositions(boolean omitTermFreqAndPositions) {
230 if (omitTermFreqAndPositions) {
231 indexOptions = IndexOptions.DOCS_ONLY;
233 indexOptions = IndexOptions.DOCS_AND_FREQS_AND_POSITIONS;
239 * If set, omit term freq, and optionally also positions and payloads from
240 * postings for this field.
242 * <p><b>NOTE</b>: While this option reduces storage space
243 * required in the index, it also means any query
244 * requiring positional information, such as {@link
245 * PhraseQuery} or {@link SpanQuery} subclasses will
246 * silently fail to find results.
248 public void setIndexOptions(IndexOptions indexOptions) { this.indexOptions=indexOptions; }
250 public boolean isLazy() {
254 /** Prints a Field for human consumption. */
256 public final String toString() {
257 StringBuilder result = new StringBuilder();
259 result.append("stored");
262 if (result.length() > 0)
264 result.append("indexed");
267 if (result.length() > 0)
269 result.append("tokenized");
271 if (storeTermVector) {
272 if (result.length() > 0)
274 result.append("termVector");
276 if (storeOffsetWithTermVector) {
277 if (result.length() > 0)
279 result.append("termVectorOffsets");
281 if (storePositionWithTermVector) {
282 if (result.length() > 0)
284 result.append("termVectorPosition");
287 if (result.length() > 0)
289 result.append("binary");
292 result.append(",omitNorms");
294 if (indexOptions != IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) {
295 result.append(",indexOptions=");
296 result.append(indexOptions);
299 result.append(",lazy");
305 if (fieldsData != null && lazy == false) {
306 result.append(fieldsData);
310 return result.toString();