Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
AbstractField.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 using System;
19 using System.IO;
20 using TokenStream = Lucene.Net.Analysis.TokenStream;
21 using StringHelper = Lucene.Net.Util.StringHelper;
22 using PhraseQuery = Lucene.Net.Search.PhraseQuery;
23 using SpanQuery = Lucene.Net.Search.Spans.SpanQuery;
24 
25 namespace Lucene.Net.Documents
26 {
27  /// <summary>
28  ///
29  ///
30  /// </summary>
31  [Serializable]
32  public abstract class AbstractField : IFieldable
33  {
34 
35  protected internal System.String internalName = "body";
36  protected internal bool storeTermVector = false;
37  protected internal bool storeOffsetWithTermVector = false;
38  protected internal bool storePositionWithTermVector = false;
39  protected internal bool internalOmitNorms = false;
40  protected internal bool internalIsStored = false;
41  protected internal bool internalIsIndexed = true;
42  protected internal bool internalIsTokenized = true;
43  protected internal bool internalIsBinary = false;
44  protected internal bool lazy = false;
45  protected internal bool internalOmitTermFreqAndPositions = false;
46  protected internal float internalBoost = 1.0f;
47  // the data object for all different kind of field values
48  protected internal System.Object fieldsData = null;
49  // pre-analyzed tokenStream for indexed fields
50  protected internal TokenStream tokenStream;
51  // length/offset for all primitive types
52  protected internal int internalBinaryLength;
53  protected internal int internalbinaryOffset;
54 
55  protected internal AbstractField()
56  {
57  }
58 
59  protected internal AbstractField(System.String name, Field.Store store, Field.Index index, Field.TermVector termVector)
60  {
61  if (name == null)
62  throw new System.NullReferenceException("name cannot be null");
63  this.internalName = StringHelper.Intern(name); // field names are interned
64 
65  this.internalIsStored = store.IsStored();
66  this.internalIsIndexed = index.IsIndexed();
67  this.internalIsTokenized = index.IsAnalyzed();
68  this.internalOmitNorms = index.OmitNorms();
69 
70  this.internalIsBinary = false;
71 
72  SetStoreTermVector(termVector);
73  }
74 
75  /// <summary>Gets or sets the boost factor for hits for this field.
76  ///
77  /// <p/>The default value is 1.0.
78  ///
79  /// <p/>Note: this value is not stored directly with the document in the index.
80  /// Documents returned from <see cref="Lucene.Net.Index.IndexReader.Document(int)" /> and
81  /// <see cref="Lucene.Net.Search.Searcher.Doc(int)" /> may thus not have the same value present as when
82  /// this field was indexed.
83  /// </summary>
84  public virtual float Boost
85  {
86  get { return internalBoost; }
87  set { this.internalBoost = value; }
88  }
89 
90  /// <summary>Returns the name of the field as an interned string.
91  /// For example "date", "title", "body", ...
92  /// </summary>
93  public virtual string Name
94  {
95  get { return internalName; }
96  }
97 
98  protected internal virtual void SetStoreTermVector(Field.TermVector termVector)
99  {
100  this.storeTermVector = termVector.IsStored();
101  this.storePositionWithTermVector = termVector.WithPositions();
102  this.storeOffsetWithTermVector = termVector.WithOffsets();
103  }
104 
105  /// <summary>True iff the value of the field is to be stored in the index for return
106  /// with search hits. It is an error for this to be true if a field is
107  /// Reader-valued.
108  /// </summary>
109  public bool IsStored
110  {
111  get { return internalIsStored; }
112  }
113 
114  /// <summary>True iff the value of the field is to be indexed, so that it may be
115  /// searched on.
116  /// </summary>
117  public bool IsIndexed
118  {
119  get { return internalIsIndexed; }
120  }
121 
122  /// <summary>True iff the value of the field should be tokenized as text prior to
123  /// indexing. Un-tokenized fields are indexed as a single word and may not be
124  /// Reader-valued.
125  /// </summary>
126  public bool IsTokenized
127  {
128  get { return internalIsTokenized; }
129  }
130 
131  /// <summary>True iff the term or terms used to index this field are stored as a term
132  /// vector, available from <see cref="Lucene.Net.Index.IndexReader.GetTermFreqVector(int,String)" />.
133  /// These methods do not provide access to the original content of the field,
134  /// only to terms used to index it. If the original content must be
135  /// preserved, use the <c>stored</c> attribute instead.
136  ///
137  /// </summary>
138  /// <seealso cref="Lucene.Net.Index.IndexReader.GetTermFreqVector(int, String)">
139  /// </seealso>
140  public bool IsTermVectorStored
141  {
142  get { return storeTermVector; }
143  }
144 
145  /// <summary> True iff terms are stored as term vector together with their offsets
146  /// (start and end position in source text).
147  /// </summary>
148  public virtual bool IsStoreOffsetWithTermVector
149  {
150  get { return storeOffsetWithTermVector; }
151  }
152 
153  /// <summary> True iff terms are stored as term vector together with their token positions.</summary>
154  public virtual bool IsStorePositionWithTermVector
155  {
156  get { return storePositionWithTermVector; }
157  }
158 
159  /// <summary>True iff the value of the filed is stored as binary </summary>
160  public bool IsBinary
161  {
162  get { return internalIsBinary; }
163  }
164 
165 
166  /// <summary> Return the raw byte[] for the binary field. Note that
167  /// you must also call <see cref="BinaryLength" /> and <see cref="BinaryOffset" />
168  /// to know which range of bytes in this
169  /// returned array belong to the field.
170  /// </summary>
171  /// <returns> reference to the Field value as byte[]. </returns>
172  public virtual byte[] GetBinaryValue()
173  {
174  return GetBinaryValue(null);
175  }
176 
177  public virtual byte[] GetBinaryValue(byte[] result)
178  {
179  if (internalIsBinary || fieldsData is byte[])
180  return (byte[]) fieldsData;
181  else
182  return null;
183  }
184 
185  /// <summary> Returns length of byte[] segment that is used as value, if Field is not binary
186  /// returned value is undefined
187  /// </summary>
188  /// <value> length of byte[] segment that represents this Field value </value>
189  public virtual int BinaryLength
190  {
191  get
192  {
193  if (internalIsBinary)
194  {
195  return internalBinaryLength;
196  }
197  return fieldsData is byte[] ? ((byte[]) fieldsData).Length : 0;
198  }
199  }
200 
201  /// <summary> Returns offset into byte[] segment that is used as value, if Field is not binary
202  /// returned value is undefined
203  /// </summary>
204  /// <value> index of the first character in byte[] segment that represents this Field value </value>
205  public virtual int BinaryOffset
206  {
207  get { return internalbinaryOffset; }
208  }
209 
210  /// <summary>True if norms are omitted for this indexed field </summary>
211  public virtual bool OmitNorms
212  {
213  get { return internalOmitNorms; }
214  set { this.internalOmitNorms = value; }
215  }
216 
217  /// <summary>Expert:
218  ///
219  /// If set, omit term freq, positions and payloads from
220  /// postings for this field.
221  ///
222  /// <p/><b>NOTE</b>: While this option reduces storage space
223  /// required in the index, it also means any query
224  /// requiring positional information, such as <see cref="PhraseQuery" />
225  /// or <see cref="SpanQuery" /> subclasses will
226  /// silently fail to find results.
227  /// </summary>
228  public virtual bool OmitTermFreqAndPositions
229  {
230  set { this.internalOmitTermFreqAndPositions = value; }
231  get { return internalOmitTermFreqAndPositions; }
232  }
233 
234  public virtual bool IsLazy
235  {
236  get { return lazy; }
237  }
238 
239  /// <summary>Prints a Field for human consumption. </summary>
240  public override System.String ToString()
241  {
242  System.Text.StringBuilder result = new System.Text.StringBuilder();
243  if (internalIsStored)
244  {
245  result.Append("stored");
246  }
247  if (internalIsIndexed)
248  {
249  if (result.Length > 0)
250  result.Append(",");
251  result.Append("indexed");
252  }
253  if (internalIsTokenized)
254  {
255  if (result.Length > 0)
256  result.Append(",");
257  result.Append("tokenized");
258  }
259  if (storeTermVector)
260  {
261  if (result.Length > 0)
262  result.Append(",");
263  result.Append("termVector");
264  }
265  if (storeOffsetWithTermVector)
266  {
267  if (result.Length > 0)
268  result.Append(",");
269  result.Append("termVectorOffsets");
270  }
271  if (storePositionWithTermVector)
272  {
273  if (result.Length > 0)
274  result.Append(",");
275  result.Append("termVectorPosition");
276  }
277  if (internalIsBinary)
278  {
279  if (result.Length > 0)
280  result.Append(",");
281  result.Append("binary");
282  }
283  if (internalOmitNorms)
284  {
285  result.Append(",omitNorms");
286  }
287  if (internalOmitTermFreqAndPositions)
288  {
289  result.Append(",omitTermFreqAndPositions");
290  }
291  if (lazy)
292  {
293  result.Append(",lazy");
294  }
295  result.Append('<');
296  result.Append(internalName);
297  result.Append(':');
298 
299  if (fieldsData != null && lazy == false)
300  {
301  result.Append(fieldsData);
302  }
303 
304  result.Append('>');
305  return result.ToString();
306  }
307 
308  public abstract TokenStream TokenStreamValue { get; }
309  public abstract TextReader ReaderValue { get; }
310  public abstract string StringValue { get; }
311  }
312 }