今天看了一下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集合的操作方法,结果比较简单,下面是源码:
1

/**//*2
* Licensed to the Apache Software Foundation (ASF) under one or more3
* contributor license agreements. See the NOTICE file distributed with4
* this work for additional information regarding copyright ownership.5
* The ASF licenses this file to You under the Apache License, Version 2.06
* (the "License"); you may not use this file except in compliance with7
* the License. You may obtain a copy of the License at8
* 9
* http://www.apache.org/licenses/LICENSE-2.010
* 11
* Unless required by applicable law or agreed to in writing, software12
* 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 and15
* limitations under the License.16
*/17

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

23
namespace Lucene.Net.Documents24


{25
26

/**//// <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 which30
/// case it is returned with search hits on the document. Thus each document31
/// should typically contain one or more stored fields which uniquely identify32
/// it.33
/// 34
/// <p>Note that fields which are <i>not</i> {@link Field#IsStored() stored} are35
/// <i>not</i> available in documents retrieved from the index, e.g. with {@link36
/// Hits#Doc(int)}, {@link Searcher#Doc(int)} or {@link37
/// IndexReader#Document(int)}.38
/// </summary>39
40
[Serializable]41
public sealed class Document42

{43
internal System.Collections.IList fields = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));44
private float boost = 1.0f;45
46

/**//// <summary>Constructs a new document with no fields. </summary>47
public Document()48

{49
}50
51

/**//// <summary>Returns the number of fields in this document</summary>52
/// Added as a helper for Lucene.Net53
public int GetFieldsCount()54

{55
return fields.Count;56
}57
58

/**//// <summary>Sets a boost factor for hits on any field of this document. This value59
/// 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()} of62
/// each field in this document. Thus, this method in effect sets a default63
/// 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

{70
this.boost = boost;71
}72
73

/**//// <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)} and79
/// {@link Hits#Doc(int)} may thus not have the same value present as when80
/// this document was indexed.81
/// 82
/// </summary>83
/// <seealso cref="SetBoost(float)">84
/// </seealso>85
public float GetBoost()86

{87
return boost;88
}89
90

/**//// <summary> <p>Adds a field to a document. Several fields may be added with91
/// the same name. In this case, if the fields are indexed, their text is92
/// 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 cannot95
/// 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 that97
/// document has to be added.</p>98
/// </summary>99
public void Add(Field field)100

{101
fields.Add(field);102
}103
104

/**//// <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 cannot109
/// 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 that111
/// document has to be added.</p>112
/// </summary>113
public void RemoveField(System.String name)114

{115
System.Collections.IEnumerator it = fields.GetEnumerator();116
while (it.MoveNext())117

{118
Field field = (Field) it.Current;119
if (field.Name().Equals(name))120

{121
fields.Remove(field);122
return ;123
}124
}125
}126
127

/**//// <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 cannot131
/// 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 that133
/// document has to be added.</p>134
/// </summary>135
public void RemoveFields(System.String name)136

{137
for (int i = fields.Count - 1; i >= 0; i--)138

{139
Field field = (Field) fields[i];140
if (field.Name().Equals(name))141

{142
fields.RemoveAt(i);143
}144
}145
}146
147

/**//// <summary>Returns a field with the given name if any exist in this document, or148
/// null. If multiple fields exists with this name, this method returns the149
/// first value added.150
/// </summary>151
public Field GetField(System.String name)152

{153
for (int i = 0; i < fields.Count; i++)154

{155
Field field = (Field) fields[i];156
if (field.Name().Equals(name))157
return field;158
}159
return null;160
}161
162

/**//// <summary>Returns the string value of the field with the given name if any exist in163
/// this document, or null. If multiple fields exist with this name, this164
/// method returns the first value added. If only binary fields with this name165
/// exist, returns null.166
/// </summary>167
public System.String Get(System.String name)168

{169
for (int i = 0; i < fields.Count; i++)170

{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

/**//// <summary>Returns an Enumeration of all the fields in a document. </summary>179
public System.Collections.IEnumerable Fields()180

{181
return fields;182
}183
184

/**//// <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 field189
/// </param>190
/// <returns> a <code>Field[]</code> array191
/// </returns>192
public Field[] GetFields(System.String name)193

{194
System.Collections.ArrayList result = new System.Collections.ArrayList();195
for (int i = 0; i < fields.Count; i++)196

{197
Field field = (Field) fields[i];198
if (field.Name().Equals(name))199

{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

/**//// <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 field215
/// </param>216
/// <returns> a <code>String[]</code> of field values217
/// </returns>218
public System.String[] GetValues(System.String name)219

{220
System.Collections.ArrayList result = new System.Collections.ArrayList();221
for (int i = 0; i < fields.Count; i++)222

{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

/**//// <summary> Returns an array of byte arrays for of the fields that have the name specified235
/// as the method parameter. This method will return <code>null</code> if no236
/// binary fields with the specified name are available.237
/// 238
/// </summary>239
/// <param name="name">the name of the field240
/// </param>241
/// <returns> a <code>byte[][]</code> of binary field values.242
/// </returns>243
public byte[][] GetBinaryValues(System.String name)244

{245
System.Collections.IList result = new System.Collections.ArrayList();246
for (int i = 0; i < fields.Count; i++)247

{248
Field field = (Field) fields[i];249
if (field.Name().Equals(name) && (field.IsBinary()))250

{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

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

266
System.Type type = objects.GetType().GetElementType();267
System.Object[] objs = (System.Object[]) Array.CreateInstance(type, c.Count );268

269
System.Collections.IEnumerator e = c.GetEnumerator();270
int ii = 0;271

272
while (e.MoveNext())273
objs[ii++] = e.Current;274

275
// If objects is smaller than c then do not return the new array in the parameter276
if (objects.Length >= c.Count)277
objs.CopyTo(objects, 0);278

279
return (byte[][]) objs;280
}281
282

/**//// <summary> Returns an array of bytes for the first (or only) field that has the name283
/// 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

{294
for (int i = 0; i < fields.Count; i++)295

{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

/**//// <summary>Prints the fields of a document for human consumption. </summary>304
public override System.String ToString()305

{306
System.Text.StringBuilder buffer = new System.Text.StringBuilder();307
buffer.Append("Document<");308
for (int i = 0; i < fields.Count; i++)309

{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
}
posted on
浙公网安备 33010602011771号