~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/src/java/org/apache/lucene/document/AbstractField.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.document;
2
 
/**
3
 
 * Copyright 2006 The Apache Software Foundation
4
 
 *
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
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 */
17
 
 
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
25
 
 
26
 
 
27
 
/**
28
 
 *
29
 
 *
30
 
 **/
31
 
public abstract class AbstractField implements Fieldable {
32
 
 
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;
52
 
 
53
 
  protected AbstractField()
54
 
  {
55
 
  }
56
 
 
57
 
  protected AbstractField(String name, Field.Store store, Field.Index index, Field.TermVector termVector) {
58
 
    if (name == null)
59
 
      throw new NullPointerException("name cannot be null");
60
 
    this.name = StringHelper.intern(name);        // field names are interned
61
 
 
62
 
    this.isStored = store.isStored();
63
 
    this.isIndexed = index.isIndexed();
64
 
    this.isTokenized = index.isAnalyzed();
65
 
    this.omitNorms = index.omitNorms();
66
 
 
67
 
    this.isBinary = false;
68
 
 
69
 
    setStoreTermVector(termVector);
70
 
  }
71
 
 
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
74
 
   * document.
75
 
   *
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
83
 
   * by the {@link
84
 
   * org.apache.lucene.search.Similarity#lengthNorm(String,
85
 
   * int)} and then
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.
89
 
   *
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)
93
 
   */
94
 
  public void setBoost(float boost) {
95
 
    this.boost = boost;
96
 
  }
97
 
 
98
 
  /** Returns the boost factor for hits for this field.
99
 
   *
100
 
   * <p>The default value is 1.0.
101
 
   *
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.
106
 
   *
107
 
   * @see #setBoost(float)
108
 
   */
109
 
  public float getBoost() {
110
 
    return boost;
111
 
  }
112
 
 
113
 
  /** Returns the name of the field as an interned string.
114
 
   * For example "date", "title", "body", ...
115
 
   */
116
 
  public String name()    { return name; }
117
 
 
118
 
  protected void setStoreTermVector(Field.TermVector termVector) {
119
 
    this.storeTermVector = termVector.isStored();
120
 
    this.storePositionWithTermVector = termVector.withPositions();
121
 
    this.storeOffsetWithTermVector = termVector.withOffsets();
122
 
  }
123
 
 
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
126
 
    Reader-valued. */
127
 
  public final boolean  isStored()  { return isStored; }
128
 
 
129
 
  /** True iff the value of the field is to be indexed, so that it may be
130
 
    searched on. */
131
 
  public final boolean  isIndexed()   { return isIndexed; }
132
 
 
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
135
 
    Reader-valued. */
136
 
  public final boolean  isTokenized()   { return isTokenized; }
137
 
 
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.
143
 
   *
144
 
   * @see org.apache.lucene.index.IndexReader#getTermFreqVector(int, String)
145
 
   */
146
 
  public final boolean isTermVectorStored() { return storeTermVector; }
147
 
 
148
 
  /**
149
 
   * True iff terms are stored as term vector together with their offsets 
150
 
   * (start and end position in source text).
151
 
   */
152
 
  public boolean isStoreOffsetWithTermVector(){
153
 
    return storeOffsetWithTermVector;
154
 
  }
155
 
 
156
 
  /**
157
 
   * True iff terms are stored as term vector together with their token positions.
158
 
   */
159
 
  public boolean isStorePositionWithTermVector(){
160
 
    return storePositionWithTermVector;
161
 
  }
162
 
 
163
 
  /** True iff the value of the filed is stored as binary */
164
 
  public final boolean  isBinary() {
165
 
    return isBinary;
166
 
  }
167
 
 
168
 
 
169
 
  /**
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[].
175
 
   */
176
 
  public byte[] getBinaryValue() {
177
 
    return getBinaryValue(null);
178
 
  }
179
 
  
180
 
  public byte[] getBinaryValue(byte[] result){
181
 
    if (isBinary || fieldsData instanceof byte[])
182
 
      return (byte[]) fieldsData;
183
 
    else
184
 
      return null;
185
 
  }
186
 
 
187
 
  /**
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
191
 
   */
192
 
  public int getBinaryLength() {
193
 
    if (isBinary) {
194
 
      return binaryLength;
195
 
    } else if (fieldsData instanceof byte[])
196
 
      return ((byte[]) fieldsData).length;
197
 
    else
198
 
      return 0;
199
 
  }
200
 
 
201
 
  /**
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
205
 
   */
206
 
  public int getBinaryOffset() {
207
 
    return binaryOffset;
208
 
  }
209
 
 
210
 
  /** True if norms are omitted for this indexed field */
211
 
  public boolean getOmitNorms() { return omitNorms; }
212
 
 
213
 
  /** @deprecated use {@link #getIndexOptions()} instead. */
214
 
  @Deprecated
215
 
  public boolean getOmitTermFreqAndPositions() { return indexOptions == IndexOptions.DOCS_ONLY; }
216
 
  
217
 
  /** @see #setIndexOptions */
218
 
  public IndexOptions getIndexOptions() { return indexOptions; }
219
 
  
220
 
  /** Expert:
221
 
   *
222
 
   * If set, omit normalization factors associated with this indexed field.
223
 
   * This effectively disables indexing boosts and length normalization for this field.
224
 
   */
225
 
  public void setOmitNorms(boolean omitNorms) { this.omitNorms=omitNorms; }
226
 
 
227
 
  /** @deprecated use {@link #setIndexOptions(FieldInfo.IndexOptions)} instead. */
228
 
  @Deprecated
229
 
  public void setOmitTermFreqAndPositions(boolean omitTermFreqAndPositions) { 
230
 
    if (omitTermFreqAndPositions) {
231
 
      indexOptions = IndexOptions.DOCS_ONLY;
232
 
    } else {
233
 
      indexOptions = IndexOptions.DOCS_AND_FREQS_AND_POSITIONS;
234
 
    }
235
 
  }
236
 
 
237
 
  /** Expert:
238
 
   *
239
 
   * If set, omit term freq, and optionally also positions and payloads from
240
 
   * postings for this field.
241
 
   *
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.
247
 
   */
248
 
  public void setIndexOptions(IndexOptions indexOptions) { this.indexOptions=indexOptions; }
249
 
 
250
 
  public boolean isLazy() {
251
 
    return lazy;
252
 
  }
253
 
 
254
 
  /** Prints a Field for human consumption. */
255
 
  @Override
256
 
  public final String toString() {
257
 
    StringBuilder result = new StringBuilder();
258
 
    if (isStored) {
259
 
      result.append("stored");
260
 
    }
261
 
    if (isIndexed) {
262
 
      if (result.length() > 0)
263
 
        result.append(",");
264
 
      result.append("indexed");
265
 
    }
266
 
    if (isTokenized) {
267
 
      if (result.length() > 0)
268
 
        result.append(",");
269
 
      result.append("tokenized");
270
 
    }
271
 
    if (storeTermVector) {
272
 
      if (result.length() > 0)
273
 
        result.append(",");
274
 
      result.append("termVector");
275
 
    }
276
 
    if (storeOffsetWithTermVector) {
277
 
      if (result.length() > 0)
278
 
        result.append(",");
279
 
      result.append("termVectorOffsets");
280
 
    }
281
 
    if (storePositionWithTermVector) {
282
 
      if (result.length() > 0)
283
 
        result.append(",");
284
 
      result.append("termVectorPosition");
285
 
    }
286
 
    if (isBinary) {
287
 
      if (result.length() > 0)
288
 
        result.append(",");
289
 
      result.append("binary");
290
 
    }
291
 
    if (omitNorms) {
292
 
      result.append(",omitNorms");
293
 
    }
294
 
    if (indexOptions != IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) {
295
 
      result.append(",indexOptions=");
296
 
      result.append(indexOptions);
297
 
    }
298
 
    if (lazy){
299
 
      result.append(",lazy");
300
 
    }
301
 
    result.append('<');
302
 
    result.append(name);
303
 
    result.append(':');
304
 
 
305
 
    if (fieldsData != null && lazy == false) {
306
 
      result.append(fieldsData);
307
 
    }
308
 
 
309
 
    result.append('>');
310
 
    return result.toString();
311
 
  }
312
 
}