今天看了一下Document.cs,Document这个类内部包含两个字段,一个是field的集合,通过ArrayList.Synchronized方法创建的,是线程安全的,另一个是boost表示排序时的优先级。下面是Document类的主要方法介绍:
今天看了一下Document.cs,Document这个类内部包含两个字段,一个是field的集合,通过ArrayList.Synchronized方法创建的,是线程安全的,另一个是boost表示排序时的优先级。下面是Document类的主要方法介绍:
方法
|
功能
|
GetFieldsCount()
|
获取field集合的数量
|
Void SetBoost(float boost)
|
设置document的boost值
|
GetBoost()
|
获取document的boost值
|
Void Add(Field field)
|
增加一个field
|
Void RemoveField(String name)
|
根据field的名字移除第一个符合条件的field
|
Void RemoveFields(String name)
|
根据field的名字移除所有符合条件的field
|
Field GetField(String name)
|
根据field的名字获取field对象,返回第一个结果
|
Field[] GetFields(String name)
|
根据field的名字获取field集合
|
String Get(String name)
|
根据field的名字获取一组字符串值,返回第一个结果
|
String[] GetValues(String name)
|
根据field的名字获取一组字符串值
|
byte[] GetBinaryValue(String name)
|
根据field的名字获取一个二进制流值,返回第一个结果
|
byte[][] GetBinaryValues(String name)
|
根据field的名字获取一组
|
String ToString()
|
重写ToString方法
|
Document类主要提供了对field集合的操作方法,结果比较简单,下面是源码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Document.cs
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
using System;
19
using IndexReader = Lucene.Net.Index.IndexReader;
20
using Hits = Lucene.Net.Search.Hits;
21
using Searcher = Lucene.Net.Search.Searcher;
22![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
namespace Lucene.Net.Documents
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
25
26![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>Documents are the unit of indexing and search.
27
///
28
/// A Document is a set of fields. Each field has a name and a textual value.
29
/// A field may be {@link Field#IsStored() stored} with the document, in which
30
/// case it is returned with search hits on the document. Thus each document
31
/// should typically contain one or more stored fields which uniquely identify
32
/// it.
33
///
34
/// <p>Note that fields which are <i>not</i> {@link Field#IsStored() stored} are
35
/// <i>not</i> available in documents retrieved from the index, e.g. with {@link
36
/// Hits#Doc(int)}, {@link Searcher#Doc(int)} or {@link
37
/// IndexReader#Document(int)}.
38
/// </summary>
39
40
[Serializable]
41
public sealed class Document
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
43
internal System.Collections.IList fields = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
44
private float boost = 1.0f;
45
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>Constructs a new document with no fields. </summary>
47
public Document()
48![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
49
}
50
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>Returns the number of fields in this document</summary>
52
/// Added as a helper for Lucene.Net
53
public int GetFieldsCount()
54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
55
return fields.Count;
56
}
57
58![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>Sets a boost factor for hits on any field of this document. This value
59
/// will be multiplied into the score of all hits on this document.
60
///
61
/// <p>Values are multiplied into the value of {@link Field#GetBoost()} of
62
/// each field in this document. Thus, this method in effect sets a default
63
/// boost for the fields of this document.
64
///
65
/// </summary>
66
/// <seealso cref="Field.SetBoost(float)">
67
/// </seealso>
68
public void SetBoost(float boost)
69![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
70
this.boost = boost;
71
}
72
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>Returns the boost factor for hits on any field of this document.
74
///
75
/// <p>The default value is 1.0.
76
///
77
/// <p>Note: This value is not stored directly with the document in the index.
78
/// Documents returned from {@link IndexReader#Document(int)} and
79
/// {@link Hits#Doc(int)} may thus not have the same value present as when
80
/// this document was indexed.
81
///
82
/// </summary>
83
/// <seealso cref="SetBoost(float)">
84
/// </seealso>
85
public float GetBoost()
86![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
87
return boost;
88
}
89
90![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary> <p>Adds a field to a document. Several fields may be added with
91
/// the same name. In this case, if the fields are indexed, their text is
92
/// treated as though appended for the purposes of search.</p>
93
/// <p> Note that add like the removeField(s) methods only makes sense
94
/// prior to adding a document to an index. These methods cannot
95
/// be used to change the content of an existing index! In order to achieve this,
96
/// a document has to be deleted from an index and a new changed version of that
97
/// document has to be added.</p>
98
/// </summary>
99
public void Add(Field field)
100![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
101
fields.Add(field);
102
}
103
104![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary> <p>Removes field with the specified name from the document.
105
/// If multiple fields exist with this name, this method removes the first field that has been added.
106
/// If there is no field with the specified name, the document remains unchanged.</p>
107
/// <p> Note that the removeField(s) methods like the add method only make sense
108
/// prior to adding a document to an index. These methods cannot
109
/// be used to change the content of an existing index! In order to achieve this,
110
/// a document has to be deleted from an index and a new changed version of that
111
/// document has to be added.</p>
112
/// </summary>
113
public void RemoveField(System.String name)
114![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
115
System.Collections.IEnumerator it = fields.GetEnumerator();
116
while (it.MoveNext())
117![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
118
Field field = (Field) it.Current;
119
if (field.Name().Equals(name))
120![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
121
fields.Remove(field);
122
return ;
123
}
124
}
125
}
126
127![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary> <p>Removes all fields with the given name from the document.
128
/// If there is no field with the specified name, the document remains unchanged.</p>
129
/// <p> Note that the removeField(s) methods like the add method only make sense
130
/// prior to adding a document to an index. These methods cannot
131
/// be used to change the content of an existing index! In order to achieve this,
132
/// a document has to be deleted from an index and a new changed version of that
133
/// document has to be added.</p>
134
/// </summary>
135
public void RemoveFields(System.String name)
136![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
137
for (int i = fields.Count - 1; i >= 0; i--)
138![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
139
Field field = (Field) fields[i];
140
if (field.Name().Equals(name))
141![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
142
fields.RemoveAt(i);
143
}
144
}
145
}
146
147![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>Returns a field with the given name if any exist in this document, or
148
/// null. If multiple fields exists with this name, this method returns the
149
/// first value added.
150
/// </summary>
151
public Field GetField(System.String name)
152![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
153
for (int i = 0; i < fields.Count; i++)
154![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
155
Field field = (Field) fields[i];
156
if (field.Name().Equals(name))
157
return field;
158
}
159
return null;
160
}
161
162![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>Returns the string value of the field with the given name if any exist in
163
/// this document, or null. If multiple fields exist with this name, this
164
/// method returns the first value added. If only binary fields with this name
165
/// exist, returns null.
166
/// </summary>
167
public System.String Get(System.String name)
168![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
169
for (int i = 0; i < fields.Count; i++)
170![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
171
Field field = (Field) fields[i];
172
if (field.Name().Equals(name) && (!field.IsBinary()))
173
return field.StringValue();
174
}
175
return null;
176
}
177
178![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>Returns an Enumeration of all the fields in a document. </summary>
179
public System.Collections.IEnumerable Fields()
180![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
181
return fields;
182
}
183
184![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary> Returns an array of {@link Field}s with the given name.
185
/// This method can return <code>null</code>.
186
///
187
/// </summary>
188
/// <param name="name">the name of the field
189
/// </param>
190
/// <returns> a <code>Field[]</code> array
191
/// </returns>
192
public Field[] GetFields(System.String name)
193![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
194
System.Collections.ArrayList result = new System.Collections.ArrayList();
195
for (int i = 0; i < fields.Count; i++)
196![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
197
Field field = (Field) fields[i];
198
if (field.Name().Equals(name))
199![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
200
result.Add(field);
201
}
202
}
203
204
if (result.Count == 0)
205
return null;
206
207
return (Field[]) result.ToArray(typeof(Field));
208
}
209
210![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary> Returns an array of values of the field specified as the method parameter.
211
/// This method can return <code>null</code>.
212
///
213
/// </summary>
214
/// <param name="name">the name of the field
215
/// </param>
216
/// <returns> a <code>String[]</code> of field values
217
/// </returns>
218
public System.String[] GetValues(System.String name)
219![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
220
System.Collections.ArrayList result = new System.Collections.ArrayList();
221
for (int i = 0; i < fields.Count; i++)
222![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
223
Field field = (Field) fields[i];
224
if (field.Name().Equals(name) && (!field.IsBinary()))
225
result.Add(field.StringValue());
226
}
227
228
if (result.Count == 0)
229
return null;
230
231
return (System.String[]) (result.ToArray(typeof(System.String)));
232
}
233
234![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary> Returns an array of byte arrays for of the fields that have the name specified
235
/// as the method parameter. This method will return <code>null</code> if no
236
/// binary fields with the specified name are available.
237
///
238
/// </summary>
239
/// <param name="name">the name of the field
240
/// </param>
241
/// <returns> a <code>byte[][]</code> of binary field values.
242
/// </returns>
243
public byte[][] GetBinaryValues(System.String name)
244![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
245
System.Collections.IList result = new System.Collections.ArrayList();
246
for (int i = 0; i < fields.Count; i++)
247![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
248
Field field = (Field) fields[i];
249
if (field.Name().Equals(name) && (field.IsBinary()))
250![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
251
byte[] byteArray = field.BinaryValue();
252
byte[] resultByteArray = new byte[byteArray.Length];
253
for (int index = 0; index < byteArray.Length; index++)
254
resultByteArray[index] = (byte) byteArray[index];
255![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
256
result.Add(resultByteArray);
257
}
258
}
259
260
if (result.Count == 0)
261
return null;
262
263
System.Collections.ICollection c = result;
264
System.Object[] objects = new byte[result.Count][];
265![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
266
System.Type type = objects.GetType().GetElementType();
267
System.Object[] objs = (System.Object[]) Array.CreateInstance(type, c.Count );
268![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
269
System.Collections.IEnumerator e = c.GetEnumerator();
270
int ii = 0;
271![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
272
while (e.MoveNext())
273
objs[ii++] = e.Current;
274![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
275
// If objects is smaller than c then do not return the new array in the parameter
276
if (objects.Length >= c.Count)
277
objs.CopyTo(objects, 0);
278![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
279
return (byte[][]) objs;
280
}
281
282![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary> Returns an array of bytes for the first (or only) field that has the name
283
/// specified as the method parameter. This method will return <code>null</code>
284
/// if no binary fields with the specified name are available.
285
/// There may be non-binary fields with the same name.
286
///
287
/// </summary>
288
/// <param name="name">the name of the field.
289
/// </param>
290
/// <returns> a <code>byte[]</code> containing the binary field value.
291
/// </returns>
292
public byte[] GetBinaryValue(System.String name)
293![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
294
for (int i = 0; i < fields.Count; i++)
295![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
296
Field field = (Field) fields[i];
297
if (field.Name().Equals(name) && (field.IsBinary()))
298
return field.BinaryValue();
299
}
300
return null;
301
}
302
303![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>Prints the fields of a document for human consumption. </summary>
304
public override System.String ToString()
305![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
306
System.Text.StringBuilder buffer = new System.Text.StringBuilder();
307
buffer.Append("Document<");
308
for (int i = 0; i < fields.Count; i++)
309![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
310
Field field = (Field) fields[i];
311
buffer.Append(field.ToString());
312
if (i != fields.Count - 1)
313
buffer.Append(" ");
314
}
315
buffer.Append(">");
316
return buffer.ToString();
317
}
318
}
319
}