Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
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 {
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 
84  public virtual float Boost
85  {
86  get { return internalBoost; }
87  set { this.internalBoost = value; }
88  }
89 
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 
109  public bool IsStored
110  {
111  get { return internalIsStored; }
112  }
113 
117  public bool IsIndexed
118  {
119  get { return internalIsIndexed; }
120  }
121 
126  public bool IsTokenized
127  {
128  get { return internalIsTokenized; }
129  }
130 
140  public bool IsTermVectorStored
141  {
142  get { return storeTermVector; }
143  }
144 
148  public virtual bool IsStoreOffsetWithTermVector
149  {
150  get { return storeOffsetWithTermVector; }
151  }
152 
154  public virtual bool IsStorePositionWithTermVector
155  {
156  get { return storePositionWithTermVector; }
157  }
158 
160  public bool IsBinary
161  {
162  get { return internalIsBinary; }
163  }
164 
165 
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 
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 
205  public virtual int BinaryOffset
206  {
207  get { return internalbinaryOffset; }
208  }
209 
211  public virtual bool OmitNorms
212  {
213  get { return internalOmitNorms; }
214  set { this.internalOmitNorms = value; }
215  }
216 
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 
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 }