001: package org.apache.lucene.document;
002:
003: /**
004: * Copyright 2006 The Apache Software Foundation
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: *
021: *
022: **/
023: public abstract class AbstractField implements Fieldable {
024:
025: protected String name = "body";
026: protected boolean storeTermVector = false;
027: protected boolean storeOffsetWithTermVector = false;
028: protected boolean storePositionWithTermVector = false;
029: protected boolean omitNorms = false;
030: protected boolean isStored = false;
031: protected boolean isIndexed = true;
032: protected boolean isTokenized = true;
033: protected boolean isBinary = false;
034: protected boolean isCompressed = false;
035: protected boolean lazy = false;
036: protected float boost = 1.0f;
037: // the one and only data object for all different kind of field values
038: protected Object fieldsData = null;
039:
040: protected AbstractField() {
041:
042: }
043:
044: protected AbstractField(String name, Field.Store store,
045: Field.Index index, Field.TermVector termVector) {
046: if (name == null)
047: throw new NullPointerException("name cannot be null");
048: this .name = name.intern(); // field names are interned
049:
050: if (store == Field.Store.YES) {
051: this .isStored = true;
052: this .isCompressed = false;
053: } else if (store == Field.Store.COMPRESS) {
054: this .isStored = true;
055: this .isCompressed = true;
056: } else if (store == Field.Store.NO) {
057: this .isStored = false;
058: this .isCompressed = false;
059: } else
060: throw new IllegalArgumentException(
061: "unknown store parameter " + store);
062:
063: if (index == Field.Index.NO) {
064: this .isIndexed = false;
065: this .isTokenized = false;
066: } else if (index == Field.Index.TOKENIZED) {
067: this .isIndexed = true;
068: this .isTokenized = true;
069: } else if (index == Field.Index.UN_TOKENIZED) {
070: this .isIndexed = true;
071: this .isTokenized = false;
072: } else if (index == Field.Index.NO_NORMS) {
073: this .isIndexed = true;
074: this .isTokenized = false;
075: this .omitNorms = true;
076: } else {
077: throw new IllegalArgumentException(
078: "unknown index parameter " + index);
079: }
080:
081: this .isBinary = false;
082:
083: setStoreTermVector(termVector);
084: }
085:
086: /** Sets the boost factor hits on this field. This value will be
087: * multiplied into the score of all hits on this this field of this
088: * document.
089: *
090: * <p>The boost is multiplied by {@link org.apache.lucene.document.Document#getBoost()} of the document
091: * containing this field. If a document has multiple fields with the same
092: * name, all such values are multiplied together. This product is then
093: * multipled by the value {@link org.apache.lucene.search.Similarity#lengthNorm(String,int)}, and
094: * rounded by {@link org.apache.lucene.search.Similarity#encodeNorm(float)} before it is stored in the
095: * index. One should attempt to ensure that this product does not overflow
096: * the range of that encoding.
097: *
098: * @see org.apache.lucene.document.Document#setBoost(float)
099: * @see org.apache.lucene.search.Similarity#lengthNorm(String, int)
100: * @see org.apache.lucene.search.Similarity#encodeNorm(float)
101: */
102: public void setBoost(float boost) {
103: this .boost = boost;
104: }
105:
106: /** Returns the boost factor for hits for this field.
107: *
108: * <p>The default value is 1.0.
109: *
110: * <p>Note: this value is not stored directly with the document in the index.
111: * Documents returned from {@link org.apache.lucene.index.IndexReader#document(int)} and
112: * {@link org.apache.lucene.search.Hits#doc(int)} may thus not have the same value present as when
113: * this field was indexed.
114: *
115: * @see #setBoost(float)
116: */
117: public float getBoost() {
118: return boost;
119: }
120:
121: /** Returns the name of the field as an interned string.
122: * For example "date", "title", "body", ...
123: */
124: public String name() {
125: return name;
126: }
127:
128: protected void setStoreTermVector(Field.TermVector termVector) {
129: if (termVector == Field.TermVector.NO) {
130: this .storeTermVector = false;
131: this .storePositionWithTermVector = false;
132: this .storeOffsetWithTermVector = false;
133: } else if (termVector == Field.TermVector.YES) {
134: this .storeTermVector = true;
135: this .storePositionWithTermVector = false;
136: this .storeOffsetWithTermVector = false;
137: } else if (termVector == Field.TermVector.WITH_POSITIONS) {
138: this .storeTermVector = true;
139: this .storePositionWithTermVector = true;
140: this .storeOffsetWithTermVector = false;
141: } else if (termVector == Field.TermVector.WITH_OFFSETS) {
142: this .storeTermVector = true;
143: this .storePositionWithTermVector = false;
144: this .storeOffsetWithTermVector = true;
145: } else if (termVector == Field.TermVector.WITH_POSITIONS_OFFSETS) {
146: this .storeTermVector = true;
147: this .storePositionWithTermVector = true;
148: this .storeOffsetWithTermVector = true;
149: } else {
150: throw new IllegalArgumentException(
151: "unknown termVector parameter " + termVector);
152: }
153: }
154:
155: /** True iff the value of the field is to be stored in the index for return
156: with search hits. It is an error for this to be true if a field is
157: Reader-valued. */
158: public final boolean isStored() {
159: return isStored;
160: }
161:
162: /** True iff the value of the field is to be indexed, so that it may be
163: searched on. */
164: public final boolean isIndexed() {
165: return isIndexed;
166: }
167:
168: /** True iff the value of the field should be tokenized as text prior to
169: indexing. Un-tokenized fields are indexed as a single word and may not be
170: Reader-valued. */
171: public final boolean isTokenized() {
172: return isTokenized;
173: }
174:
175: /** True if the value of the field is stored and compressed within the index */
176: public final boolean isCompressed() {
177: return isCompressed;
178: }
179:
180: /** True iff the term or terms used to index this field are stored as a term
181: * vector, available from {@link org.apache.lucene.index.IndexReader#getTermFreqVector(int,String)}.
182: * These methods do not provide access to the original content of the field,
183: * only to terms used to index it. If the original content must be
184: * preserved, use the <code>stored</code> attribute instead.
185: *
186: * @see org.apache.lucene.index.IndexReader#getTermFreqVector(int, String)
187: */
188: public final boolean isTermVectorStored() {
189: return storeTermVector;
190: }
191:
192: /**
193: * True iff terms are stored as term vector together with their offsets
194: * (start and end positon in source text).
195: */
196: public boolean isStoreOffsetWithTermVector() {
197: return storeOffsetWithTermVector;
198: }
199:
200: /**
201: * True iff terms are stored as term vector together with their token positions.
202: */
203: public boolean isStorePositionWithTermVector() {
204: return storePositionWithTermVector;
205: }
206:
207: /** True iff the value of the filed is stored as binary */
208: public final boolean isBinary() {
209: return isBinary;
210: }
211:
212: /** True if norms are omitted for this indexed field */
213: public boolean getOmitNorms() {
214: return omitNorms;
215: }
216:
217: /** Expert:
218: *
219: * If set, omit normalization factors associated with this indexed field.
220: * This effectively disables indexing boosts and length normalization for this field.
221: */
222: public void setOmitNorms(boolean omitNorms) {
223: this .omitNorms = omitNorms;
224: }
225:
226: public boolean isLazy() {
227: return lazy;
228: }
229:
230: /** Prints a Field for human consumption. */
231: public final String toString() {
232: StringBuffer result = new StringBuffer();
233: if (isStored) {
234: result.append("stored");
235: if (isCompressed)
236: result.append("/compressed");
237: else
238: result.append("/uncompressed");
239: }
240: if (isIndexed) {
241: if (result.length() > 0)
242: result.append(",");
243: result.append("indexed");
244: }
245: if (isTokenized) {
246: if (result.length() > 0)
247: result.append(",");
248: result.append("tokenized");
249: }
250: if (storeTermVector) {
251: if (result.length() > 0)
252: result.append(",");
253: result.append("termVector");
254: }
255: if (storeOffsetWithTermVector) {
256: if (result.length() > 0)
257: result.append(",");
258: result.append("termVectorOffsets");
259: }
260: if (storePositionWithTermVector) {
261: if (result.length() > 0)
262: result.append(",");
263: result.append("termVectorPosition");
264: }
265: if (isBinary) {
266: if (result.length() > 0)
267: result.append(",");
268: result.append("binary");
269: }
270: if (omitNorms) {
271: result.append(",omitNorms");
272: }
273: if (lazy) {
274: result.append(",lazy");
275: }
276: result.append('<');
277: result.append(name);
278: result.append(':');
279:
280: if (fieldsData != null && lazy == false) {
281: result.append(fieldsData);
282: }
283:
284: result.append('>');
285: return result.toString();
286: }
287: }
|