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
Field.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 IndexWriter = Lucene.Net.Index.IndexWriter;
22 using StringHelper = Lucene.Net.Util.StringHelper;
23 
24 namespace Lucene.Net.Documents
25 {
26 
33 
34  [Serializable]
35  public sealed class Field:AbstractField, IFieldable
36  {
38  public enum Store
39  {
45  YES,
46 
48  NO
49  }
50 
52 
53  public enum Index
54  {
59  NO,
60 
65  ANALYZED,
66 
71  NOT_ANALYZED,
72 
88  NOT_ANALYZED_NO_NORMS,
89 
96  ANALYZED_NO_NORMS,
97  }
98 
100  public enum TermVector
101  {
103  NO,
104 
108  YES,
109 
115  WITH_POSITIONS,
116 
122  WITH_OFFSETS,
123 
133  WITH_POSITIONS_OFFSETS,
134  }
135 
136 
141  public override string StringValue
142  {
143  get { return fieldsData is System.String ? (System.String) fieldsData : null; }
144  }
145 
150  public override TextReader ReaderValue
151  {
152  get { return fieldsData is System.IO.TextReader ? (System.IO.TextReader) fieldsData : null; }
153  }
154 
158  public override TokenStream TokenStreamValue
159  {
160  get { return tokenStream; }
161  }
162 
163 
176  public void SetValue(System.String value)
177  {
178  if (internalIsBinary)
179  {
180  throw new System.ArgumentException("cannot set a String value on a binary field");
181  }
182  fieldsData = value;
183  }
184 
186  public void SetValue(System.IO.TextReader value)
187  {
188  if (internalIsBinary)
189  {
190  throw new System.ArgumentException("cannot set a Reader value on a binary field");
191  }
192  if (internalIsStored)
193  {
194  throw new System.ArgumentException("cannot set a Reader value on a stored field");
195  }
196  fieldsData = value;
197  }
198 
200  public void SetValue(byte[] value)
201  {
202  if (!internalIsBinary)
203  {
204  throw new System.ArgumentException("cannot set a byte[] value on a non-binary field");
205  }
206  fieldsData = value;
207  internalBinaryLength = value.Length;
208  internalbinaryOffset = 0;
209  }
210 
212  public void SetValue(byte[] value, int offset, int length)
213  {
214  if (!internalIsBinary)
215  {
216  throw new System.ArgumentException("cannot set a byte[] value on a non-binary field");
217  }
218  fieldsData = value;
219  internalBinaryLength = length;
220  internalbinaryOffset = offset;
221  }
222 
226  public void SetTokenStream(TokenStream tokenStream)
227  {
228  this.internalIsIndexed = true;
229  this.internalIsTokenized = true;
230  this.tokenStream = tokenStream;
231  }
232 
248  public Field(System.String name, System.String value, Store store, Index index)
249  : this(name, value, store, index, TermVector.NO)
250  {
251  }
252 
275  public Field(System.String name, System.String value, Store store, Index index, TermVector termVector)
276  : this(name, true, value, store, index, termVector)
277  {
278  }
279 
304  public Field(System.String name, bool internName, System.String value, Store store, Index index, TermVector termVector)
305  {
306  if (name == null)
307  throw new System.NullReferenceException("name cannot be null");
308  if (value == null)
309  throw new System.NullReferenceException("value cannot be null");
310  if (name.Length == 0 && value.Length == 0)
311  throw new System.ArgumentException("name and value cannot both be empty");
312  if (index == Index.NO && store == Store.NO)
313  throw new System.ArgumentException("it doesn't make sense to have a field that " + "is neither indexed nor stored");
314  if (index == Index.NO && termVector != TermVector.NO)
315  throw new System.ArgumentException("cannot store term vector information " + "for a field that is not indexed");
316 
317  if (internName)
318  // field names are optionally interned
319  name = StringHelper.Intern(name);
320 
321  this.internalName = name;
322 
323  this.fieldsData = value;
324 
325  this.internalIsStored = store.IsStored();
326 
327  this.internalIsIndexed = index.IsIndexed();
328  this.internalIsTokenized = index.IsAnalyzed();
329  this.internalOmitNorms = index.OmitNorms();
330 
331  if (index == Index.NO)
332  {
333  this.internalOmitTermFreqAndPositions = false;
334  }
335 
336  this.internalIsBinary = false;
337 
338  SetStoreTermVector(termVector);
339  }
340 
352  public Field(System.String name, System.IO.TextReader reader):this(name, reader, TermVector.NO)
353  {
354  }
355 
369  public Field(System.String name, System.IO.TextReader reader, TermVector termVector)
370  {
371  if (name == null)
372  throw new System.NullReferenceException("name cannot be null");
373  if (reader == null)
374  throw new System.NullReferenceException("reader cannot be null");
375 
376  this.internalName = StringHelper.Intern(name); // field names are interned
377  this.fieldsData = reader;
378 
379  this.internalIsStored = false;
380 
381  this.internalIsIndexed = true;
382  this.internalIsTokenized = true;
383 
384  this.internalIsBinary = false;
385 
386  SetStoreTermVector(termVector);
387  }
388 
401  public Field(System.String name, TokenStream tokenStream):this(name, tokenStream, TermVector.NO)
402  {
403  }
404 
419  public Field(System.String name, TokenStream tokenStream, TermVector termVector)
420  {
421  if (name == null)
422  throw new System.NullReferenceException("name cannot be null");
423  if (tokenStream == null)
424  throw new System.NullReferenceException("tokenStream cannot be null");
425 
426  this.internalName = StringHelper.Intern(name); // field names are interned
427  this.fieldsData = null;
428  this.tokenStream = tokenStream;
429 
430  this.internalIsStored = false;
431 
432  this.internalIsIndexed = true;
433  this.internalIsTokenized = true;
434 
435  this.internalIsBinary = false;
436 
437  SetStoreTermVector(termVector);
438  }
439 
440 
451  public Field(System.String name, byte[] value_Renamed, Store store):this(name, value_Renamed, 0, value_Renamed.Length, store)
452  {
453  }
454 
469  public Field(System.String name, byte[] value_Renamed, int offset, int length, Store store)
470  {
471 
472  if (name == null)
473  throw new System.ArgumentException("name cannot be null");
474  if (value_Renamed == null)
475  throw new System.ArgumentException("value cannot be null");
476 
477  this.internalName = StringHelper.Intern(name); // field names are interned
478  fieldsData = value_Renamed;
479 
480  if (store == Store.NO)
481  throw new System.ArgumentException("binary values can't be unstored");
482 
483  internalIsStored = store.IsStored();
484  internalIsIndexed = false;
485  internalIsTokenized = false;
486  internalOmitTermFreqAndPositions = false;
487  internalOmitNorms = true;
488 
489  internalIsBinary = true;
490  internalBinaryLength = length;
491  internalbinaryOffset = offset;
492 
493  SetStoreTermVector(TermVector.NO);
494  }
495  }
496 
497  public static class FieldExtensions
498  {
499  public static bool IsStored(this Field.Store store)
500  {
501  switch(store)
502  {
503  case Field.Store.YES:
504  return true;
505  case Field.Store.NO:
506  return false;
507  default:
508  throw new ArgumentOutOfRangeException("store", "Invalid value for Field.Store");
509  }
510  }
511 
512  public static bool IsIndexed(this Field.Index index)
513  {
514  switch(index)
515  {
516  case Field.Index.NO:
517  return false;
518  case Field.Index.ANALYZED:
519  case Field.Index.NOT_ANALYZED:
520  case Field.Index.NOT_ANALYZED_NO_NORMS:
521  case Field.Index.ANALYZED_NO_NORMS:
522  return true;
523  default:
524  throw new ArgumentOutOfRangeException("index", "Invalid value for Field.Index");
525  }
526  }
527 
528  public static bool IsAnalyzed(this Field.Index index)
529  {
530  switch (index)
531  {
532  case Field.Index.NO:
533  case Field.Index.NOT_ANALYZED:
534  case Field.Index.NOT_ANALYZED_NO_NORMS:
535  return false;
536  case Field.Index.ANALYZED:
537  case Field.Index.ANALYZED_NO_NORMS:
538  return true;
539  default:
540  throw new ArgumentOutOfRangeException("index", "Invalid value for Field.Index");
541  }
542  }
543 
544  public static bool OmitNorms(this Field.Index index)
545  {
546  switch (index)
547  {
548  case Field.Index.ANALYZED:
549  case Field.Index.NOT_ANALYZED:
550  return false;
551  case Field.Index.NO:
552  case Field.Index.NOT_ANALYZED_NO_NORMS:
553  case Field.Index.ANALYZED_NO_NORMS:
554  return true;
555  default:
556  throw new ArgumentOutOfRangeException("index", "Invalid value for Field.Index");
557  }
558  }
559 
560  public static bool IsStored(this Field.TermVector tv)
561  {
562  switch(tv)
563  {
564  case Field.TermVector.NO:
565  return false;
566  case Field.TermVector.YES:
567  case Field.TermVector.WITH_OFFSETS:
568  case Field.TermVector.WITH_POSITIONS:
569  case Field.TermVector.WITH_POSITIONS_OFFSETS:
570  return true;
571  default:
572  throw new ArgumentOutOfRangeException("tv", "Invalid value for Field.TermVector");
573  }
574  }
575 
576  public static bool WithPositions(this Field.TermVector tv)
577  {
578  switch (tv)
579  {
580  case Field.TermVector.NO:
581  case Field.TermVector.YES:
582  case Field.TermVector.WITH_OFFSETS:
583  return false;
584  case Field.TermVector.WITH_POSITIONS:
585  case Field.TermVector.WITH_POSITIONS_OFFSETS:
586  return true;
587  default:
588  throw new ArgumentOutOfRangeException("tv", "Invalid value for Field.TermVector");
589  }
590  }
591 
592  public static bool WithOffsets(this Field.TermVector tv)
593  {
594  switch (tv)
595  {
596  case Field.TermVector.NO:
597  case Field.TermVector.YES:
598  case Field.TermVector.WITH_POSITIONS:
599  return false;
600  case Field.TermVector.WITH_OFFSETS:
601  case Field.TermVector.WITH_POSITIONS_OFFSETS:
602  return true;
603  default:
604  throw new ArgumentOutOfRangeException("tv", "Invalid value for Field.TermVector");
605  }
606  }
607 
608  public static Field.Index ToIndex(bool indexed, bool analyed)
609  {
610  return ToIndex(indexed, analyed, false);
611  }
612 
613  public static Field.Index ToIndex(bool indexed, bool analyzed, bool omitNorms)
614  {
615 
616  // If it is not indexed nothing else matters
617  if (!indexed)
618  {
619  return Field.Index.NO;
620  }
621 
622  // typical, non-expert
623  if (!omitNorms)
624  {
625  if (analyzed)
626  {
627  return Field.Index.ANALYZED;
628  }
629  return Field.Index.NOT_ANALYZED;
630  }
631 
632  // Expert: Norms omitted
633  if (analyzed)
634  {
635  return Field.Index.ANALYZED_NO_NORMS;
636  }
637  return Field.Index.NOT_ANALYZED_NO_NORMS;
638  }
639 
643  public static Field.TermVector ToTermVector(bool stored, bool withOffsets, bool withPositions)
644  {
645  // If it is not stored, nothing else matters.
646  if (!stored)
647  {
648  return Field.TermVector.NO;
649  }
650 
651  if (withOffsets)
652  {
653  if (withPositions)
654  {
655  return Field.TermVector.WITH_POSITIONS_OFFSETS;
656  }
657  return Field.TermVector.WITH_OFFSETS;
658  }
659 
660  if (withPositions)
661  {
662  return Field.TermVector.WITH_POSITIONS;
663  }
664  return Field.TermVector.YES;
665  }
666  }
667 }