2 * Copyright 2004 The Apache Software Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using Document
= Lucene
.Net
.Documents
.Document
;
19 using Field
= Lucene
.Net
.Documents
.Field
;
20 using Directory
= Lucene
.Net
.Store
.Directory
;
21 using IndexInput
= Lucene
.Net
.Store
.IndexInput
;
22 using IndexOutput
= Lucene
.Net
.Store
.IndexOutput
;
24 namespace Lucene
.Net
.Index
27 /// <summary>Access to the Field Info file that describes document fields and whether or
28 /// not they are indexed. Each segment has a separate Field Info file. Objects
29 /// of this class are thread-safe for multiple readers, but only one thread can
30 /// be adding documents at a time, with no other reader or writer threads
31 /// accessing this object.
33 public sealed class FieldInfos
36 internal const byte IS_INDEXED
= (byte) (0x1);
37 internal const byte STORE_TERMVECTOR
= (byte) (0x2);
38 internal const byte STORE_POSITIONS_WITH_TERMVECTOR
= (byte) (0x4);
39 internal const byte STORE_OFFSET_WITH_TERMVECTOR
= (byte) (0x8);
40 internal const byte OMIT_NORMS
= (byte) (0x10);
42 private System
.Collections
.ArrayList byNumber
= new System
.Collections
.ArrayList();
43 private System
.Collections
.Hashtable byName
= new System
.Collections
.Hashtable();
45 public /*internal*/ FieldInfos()
49 /// <summary> Construct a FieldInfos object using the directory and the name of the file
52 /// <param name="d">The directory to open the IndexInput from
54 /// <param name="name">The name of the file to open the IndexInput from in the Directory
56 /// <throws> IOException </throws>
57 public /*internal*/ FieldInfos(Directory d
, System
.String name
)
59 IndexInput input
= d
.OpenInput(name
);
70 /// <summary>Adds field info for a Document. </summary>
71 public void Add(Document doc
)
73 foreach(Field field
in doc
.Fields())
75 Add(field
.Name(), field
.IsIndexed(), field
.IsTermVectorStored(), field
.IsStorePositionWithTermVector(), field
.IsStoreOffsetWithTermVector(), field
.GetOmitNorms());
79 /// <summary> Add fields that are indexed. Whether they have termvectors has to be specified.
82 /// <param name="names">The names of the fields
84 /// <param name="storeTermVectors">Whether the fields store term vectors or not
86 /// <param name="storePositionWithTermVector">treu if positions should be stored.
88 /// <param name="storeOffsetWithTermVector">true if offsets should be stored
90 public void AddIndexed(System
.Collections
.ICollection names
, bool storeTermVectors
, bool storePositionWithTermVector
, bool storeOffsetWithTermVector
)
92 System
.Collections
.IEnumerator i
= names
.GetEnumerator();
95 System
.Collections
.DictionaryEntry t
= (System
.Collections
.DictionaryEntry
) i
.Current
;
96 Add((System
.String
) t
.Key
, true, storeTermVectors
, storePositionWithTermVector
, storeOffsetWithTermVector
);
100 /// <summary> Assumes the fields are not storing term vectors.
103 /// <param name="names">The names of the fields
105 /// <param name="isIndexed">Whether the fields are indexed or not
108 /// <seealso cref="Add(String, boolean)">
110 public void Add(System
.Collections
.ICollection names
, bool isIndexed
)
112 System
.Collections
.IEnumerator i
= names
.GetEnumerator();
115 System
.Collections
.DictionaryEntry t
= (System
.Collections
.DictionaryEntry
) i
.Current
;
116 Add((System
.String
) t
.Key
, isIndexed
);
120 /// <summary> Calls 5 parameter add with false for all TermVector parameters.
123 /// <param name="name">The name of the Field
125 /// <param name="isIndexed">true if the field is indexed
127 /// <seealso cref="Add(String, boolean, boolean, boolean, boolean)">
129 public void Add(System
.String name
, bool isIndexed
)
131 Add(name
, isIndexed
, false, false, false, false);
134 /// <summary> Calls 5 parameter add with false for term vector positions and offsets.
137 /// <param name="name">The name of the field
139 /// <param name="isIndexed"> true if the field is indexed
141 /// <param name="storeTermVector">true if the term vector should be stored
143 public void Add(System
.String name
, bool isIndexed
, bool storeTermVector
)
145 Add(name
, isIndexed
, storeTermVector
, false, false, false);
148 /// <summary>If the field is not yet known, adds it. If it is known, checks to make
149 /// sure that the isIndexed flag is the same as was given previously for this
150 /// field. If not - marks it as being indexed. Same goes for the TermVector
154 /// <param name="name">The name of the field
156 /// <param name="isIndexed">true if the field is indexed
158 /// <param name="storeTermVector">true if the term vector should be stored
160 /// <param name="storePositionWithTermVector">true if the term vector with positions should be stored
162 /// <param name="storeOffsetWithTermVector">true if the term vector with offsets should be stored
164 public void Add(System
.String name
, bool isIndexed
, bool storeTermVector
, bool storePositionWithTermVector
, bool storeOffsetWithTermVector
)
167 Add(name
, isIndexed
, storeTermVector
, storePositionWithTermVector
, storeOffsetWithTermVector
, false);
170 /// <summary>If the field is not yet known, adds it. If it is known, checks to make
171 /// sure that the isIndexed flag is the same as was given previously for this
172 /// field. If not - marks it as being indexed. Same goes for the TermVector
176 /// <param name="name">The name of the field
178 /// <param name="isIndexed">true if the field is indexed
180 /// <param name="storeTermVector">true if the term vector should be stored
182 /// <param name="storePositionWithTermVector">true if the term vector with positions should be stored
184 /// <param name="storeOffsetWithTermVector">true if the term vector with offsets should be stored
186 /// <param name="omitNorms">true if the norms for the indexed field should be omitted
188 public void Add(System
.String name
, bool isIndexed
, bool storeTermVector
, bool storePositionWithTermVector
, bool storeOffsetWithTermVector
, bool omitNorms
)
190 FieldInfo fi
= FieldInfo(name
);
193 AddInternal(name
, isIndexed
, storeTermVector
, storePositionWithTermVector
, storeOffsetWithTermVector
, omitNorms
);
197 if (fi
.isIndexed
!= isIndexed
)
199 fi
.isIndexed
= true; // once indexed, always index
201 if (fi
.storeTermVector
!= storeTermVector
)
203 fi
.storeTermVector
= true; // once vector, always vector
205 if (fi
.storePositionWithTermVector
!= storePositionWithTermVector
)
207 fi
.storePositionWithTermVector
= true; // once vector, always vector
209 if (fi
.storeOffsetWithTermVector
!= storeOffsetWithTermVector
)
211 fi
.storeOffsetWithTermVector
= true; // once vector, always vector
213 if (fi
.omitNorms
!= omitNorms
)
215 fi
.omitNorms
= false; // once norms are stored, always store
221 private void AddInternal(System
.String name
, bool isIndexed
, bool storeTermVector
, bool storePositionWithTermVector
, bool storeOffsetWithTermVector
, bool omitNorms
)
223 FieldInfo fi
= new FieldInfo(name
, isIndexed
, byNumber
.Count
, storeTermVector
, storePositionWithTermVector
, storeOffsetWithTermVector
, omitNorms
);
228 public int FieldNumber(System
.String fieldName
)
230 FieldInfo fi
= FieldInfo(fieldName
);
236 public FieldInfo
FieldInfo(System
.String fieldName
)
238 return (FieldInfo
) byName
[fieldName
];
241 /// <summary> Return the fieldName identified by its number.
244 /// <param name="fieldNumber">
246 /// <returns> the fieldName or an empty string when the field
247 /// with the given number doesn't exist.
249 public System
.String
FieldName(int fieldNumber
)
251 FieldInfo info
= FieldInfo(fieldNumber
);
258 /// <summary> Return the fieldinfo object referenced by the fieldNumber.</summary>
259 /// <param name="fieldNumber">
261 /// <returns> the FieldInfo object or null when the given fieldNumber
264 public FieldInfo
FieldInfo(int fieldNumber
)
266 if (fieldNumber
< 0 || fieldNumber
>= byNumber
.Count
)
270 return (FieldInfo
) byNumber
[fieldNumber
];
275 return byNumber
.Count
;
278 public bool HasVectors()
280 bool hasVectors
= false;
281 for (int i
= 0; i
< Size(); i
++)
283 if (FieldInfo(i
).storeTermVector
)
292 public void Write(Directory d
, System
.String name
)
294 IndexOutput output
= d
.CreateOutput(name
);
305 public void Write(IndexOutput output
)
307 output
.WriteVInt(Size());
308 for (int i
= 0; i
< Size(); i
++)
310 FieldInfo fi
= FieldInfo(i
);
311 byte bits
= (byte) (0x0);
314 if (fi
.storeTermVector
)
315 bits
|= STORE_TERMVECTOR
;
316 if (fi
.storePositionWithTermVector
)
317 bits
|= STORE_POSITIONS_WITH_TERMVECTOR
;
318 if (fi
.storeOffsetWithTermVector
)
319 bits
|= STORE_OFFSET_WITH_TERMVECTOR
;
322 output
.WriteString(fi
.name
);
323 output
.WriteByte(bits
);
327 private void Read(IndexInput input
)
329 int size
= input
.ReadVInt(); //read in the size
330 for (int i
= 0; i
< size
; i
++)
332 System
.String name
= String
.Intern(input
.ReadString());
333 byte bits
= input
.ReadByte();
334 bool isIndexed
= (bits
& IS_INDEXED
) != 0;
335 bool storeTermVector
= (bits
& STORE_TERMVECTOR
) != 0;
336 bool storePositionsWithTermVector
= (bits
& STORE_POSITIONS_WITH_TERMVECTOR
) != 0;
337 bool storeOffsetWithTermVector
= (bits
& STORE_OFFSET_WITH_TERMVECTOR
) != 0;
338 bool omitNorms
= (bits
& OMIT_NORMS
) != 0;
340 AddInternal(name
, isIndexed
, storeTermVector
, storePositionsWithTermVector
, storeOffsetWithTermVector
, omitNorms
);