1 package org.apache.lucene.document;
2
3 /**
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 import java.util.*; // for javadoc
21 import org.apache.lucene.search.ScoreDoc; // for javadoc
22 import org.apache.lucene.search.Searcher; // for javadoc
23 import org.apache.lucene.index.IndexReader; // for javadoc
24
25 /** Documents are the unit of indexing and search.
26 *
27 * A Document is a set of fields. Each field has a name and a textual value.
28 * A field may be {@link Fieldable#isStored() stored} with the document, in which
29 * case it is returned with search hits on the document. Thus each document
30 * should typically contain one or more stored fields which uniquely identify
31 * it.
32 *
33 * <p>Note that fields which are <i>not</i> {@link Fieldable#isStored() stored} are
34 * <i>not</i> available in documents retrieved from the index, e.g. with {@link
35 * ScoreDoc#doc}, {@link Searcher#doc(int)} or {@link
36 * IndexReader#document(int)}.
37 */
38
39 public final class Document implements java.io.Serializable {
40 List fields = new ArrayList();
41 private float boost = 1.0f;
42
43 /** Constructs a new document with no fields. */
44 public Document() {}
45
46
47 /** Sets a boost factor for hits on any field of this document. This value
48 * will be multiplied into the score of all hits on this document.
49 *
50 * <p>The default value is 1.0.
51 *
52 * <p>Values are multiplied into the value of {@link Fieldable#getBoost()} of
53 * each field in this document. Thus, this method in effect sets a default
54 * boost for the fields of this document.
55 *
56 * @see Fieldable#setBoost(float)
57 */
58 public void setBoost(float boost) {
59 this.boost = boost;
60 }
61
62 /** Returns, at indexing time, the boost factor as set by {@link #setBoost(float)}.
63 *
64 * <p>Note that once a document is indexed this value is no longer available
65 * from the index. At search time, for retrieved documents, this method always
66 * returns 1. This however does not mean that the boost value set at indexing
67 * time was ignored - it was just combined with other indexing time factors and
68 * stored elsewhere, for better indexing and search performance. (For more
69 * information see the "norm(t,d)" part of the scoring formula in
70 * {@link org.apache.lucene.search.Similarity Similarity}.)
71 *
72 * @see #setBoost(float)
73 */
74 public float getBoost() {
75 return boost;
76 }
77
78 /**
79 * <p>Adds a field to a document. Several fields may be added with
80 * the same name. In this case, if the fields are indexed, their text is
81 * treated as though appended for the purposes of search.</p>
82 * <p> Note that add like the removeField(s) methods only makes sense
83 * prior to adding a document to an index. These methods cannot
84 * be used to change the content of an existing index! In order to achieve this,
85 * a document has to be deleted from an index and a new changed version of that
86 * document has to be added.</p>
87 */
88 public final void add(Fieldable field) {
89 fields.add(field);
90 }
91
92 /**
93 * <p>Removes field with the specified name from the document.
94 * If multiple fields exist with this name, this method removes the first field that has been added.
95 * If there is no field with the specified name, the document remains unchanged.</p>
96 * <p> Note that the removeField(s) methods like the add method only make sense
97 * prior to adding a document to an index. These methods cannot
98 * be used to change the content of an existing index! In order to achieve this,
99 * a document has to be deleted from an index and a new changed version of that
100 * document has to be added.</p>
101 */
102 public final void removeField(String name) {
103 Iterator it = fields.iterator();
104 while (it.hasNext()) {
105 Fieldable field = (Fieldable)it.next();
106 if (field.name().equals(name)) {
107 it.remove();
108 return;
109 }
110 }
111 }
112
113 /**
114 * <p>Removes all fields with the given name from the document.
115 * If there is no field with the specified name, the document remains unchanged.</p>
116 * <p> Note that the removeField(s) methods like the add method only make sense
117 * prior to adding a document to an index. These methods cannot
118 * be used to change the content of an existing index! In order to achieve this,
119 * a document has to be deleted from an index and a new changed version of that
120 * document has to be added.</p>
121 */
122 public final void removeFields(String name) {
123 Iterator it = fields.iterator();
124 while (it.hasNext()) {
125 Fieldable field = (Fieldable)it.next();
126 if (field.name().equals(name)) {
127 it.remove();
128 }
129 }
130 }
131
132 /** Returns a field with the given name if any exist in this document, or
133 * null. If multiple fields exists with this name, this method returns the
134 * first value added.
135 * Do not use this method with lazy loaded fields.
136 */
137 public final Field getField(String name) {
138 for (int i = 0; i < fields.size(); i++) {
139 Field field = (Field)fields.get(i);
140 if (field.name().equals(name))
141 return field;
142 }
143 return null;
144 }
145
146
147 /** 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 */
151 public Fieldable getFieldable(String name) {
152 for (int i = 0; i < fields.size(); i++) {
153 Fieldable field = (Fieldable)fields.get(i);
154 if (field.name().equals(name))
155 return field;
156 }
157 return null;
158 }
159
160 /** Returns the string value of the field with the given name if any exist in
161 * this document, or null. If multiple fields exist with this name, this
162 * method returns the first value added. If only binary fields with this name
163 * exist, returns null.
164 */
165 public final String get(String name) {
166 for (int i = 0; i < fields.size(); i++) {
167 Fieldable field = (Fieldable)fields.get(i);
168 if (field.name().equals(name) && (!field.isBinary()))
169 return field.stringValue();
170 }
171 return null;
172 }
173
174 /** Returns an Enumeration of all the fields in a document.
175 * @deprecated use {@link #getFields()} instead
176 */
177 public final Enumeration fields() {
178 return new Enumeration() {
179 final Iterator iter = fields.iterator();
180 public boolean hasMoreElements() {
181 return iter.hasNext();
182 }
183 public Object nextElement() {
184 return iter.next();
185 }
186 };
187 }
188
189 /** Returns a List of all the fields in a document.
190 * <p>Note that fields which are <i>not</i> {@link Fieldable#isStored() stored} are
191 * <i>not</i> available in documents retrieved from the
192 * index, e.g. {@link Searcher#doc(int)} or {@link
193 * IndexReader#document(int)}.
194 */
195 public final List getFields() {
196 return fields;
197 }
198
199 private final static Field[] NO_FIELDS = new Field[0];
200
201 /**
202 * Returns an array of {@link Field}s with the given name.
203 * Do not use with lazy loaded fields.
204 * This method returns an empty array when there are no
205 * matching fields. It never returns null.
206 *
207 * @param name the name of the field
208 * @return a <code>Field[]</code> array
209 */
210 public final Field[] getFields(String name) {
211 List result = new ArrayList();
212 for (int i = 0; i < fields.size(); i++) {
213 Field field = (Field)fields.get(i);
214 if (field.name().equals(name)) {
215 result.add(field);
216 }
217 }
218
219 if (result.size() == 0)
220 return NO_FIELDS;
221
222 return (Field[])result.toArray(new Field[result.size()]);
223 }
224
225
226 private final static Fieldable[] NO_FIELDABLES = new Fieldable[0];
227
228 /**
229 * Returns an array of {@link Fieldable}s with the given name.
230 * This method returns an empty array when there are no
231 * matching fields. It never returns null.
232 *
233 * @param name the name of the field
234 * @return a <code>Fieldable[]</code> array
235 */
236 public Fieldable[] getFieldables(String name) {
237 List result = new ArrayList();
238 for (int i = 0; i < fields.size(); i++) {
239 Fieldable field = (Fieldable)fields.get(i);
240 if (field.name().equals(name)) {
241 result.add(field);
242 }
243 }
244
245 if (result.size() == 0)
246 return NO_FIELDABLES;
247
248 return (Fieldable[])result.toArray(new Fieldable[result.size()]);
249 }
250
251
252 private final static String[] NO_STRINGS = new String[0];
253
254 /**
255 * Returns an array of values of the field specified as the method parameter.
256 * This method returns an empty array when there are no
257 * matching fields. It never returns null.
258 * @param name the name of the field
259 * @return a <code>String[]</code> of field values
260 */
261 public final String[] getValues(String name) {
262 List result = new ArrayList();
263 for (int i = 0; i < fields.size(); i++) {
264 Fieldable field = (Fieldable)fields.get(i);
265 if (field.name().equals(name) && (!field.isBinary()))
266 result.add(field.stringValue());
267 }
268
269 if (result.size() == 0)
270 return NO_STRINGS;
271
272 return (String[])result.toArray(new String[result.size()]);
273 }
274
275 private final static byte[][] NO_BYTES = new byte[0][];
276
277 /**
278 * Returns an array of byte arrays for of the fields that have the name specified
279 * as the method parameter. This method returns an empty
280 * array when there are no matching fields. It never
281 * returns null.
282 *
283 * @param name the name of the field
284 * @return a <code>byte[][]</code> of binary field values
285 */
286 public final byte[][] getBinaryValues(String name) {
287 List result = new ArrayList();
288 for (int i = 0; i < fields.size(); i++) {
289 Fieldable field = (Fieldable)fields.get(i);
290 if (field.name().equals(name) && (field.isBinary()))
291 result.add(field.binaryValue());
292 }
293
294 if (result.size() == 0)
295 return NO_BYTES;
296
297 return (byte[][])result.toArray(new byte[result.size()][]);
298 }
299
300 /**
301 * Returns an array of bytes for the first (or only) field that has the name
302 * specified as the method parameter. This method will return <code>null</code>
303 * if no binary fields with the specified name are available.
304 * There may be non-binary fields with the same name.
305 *
306 * @param name the name of the field.
307 * @return a <code>byte[]</code> containing the binary field value or <code>null</code>
308 */
309 public final byte[] getBinaryValue(String name) {
310 for (int i=0; i < fields.size(); i++) {
311 Fieldable field = (Fieldable)fields.get(i);
312 if (field.name().equals(name) && (field.isBinary()))
313 return field.binaryValue();
314 }
315 return null;
316 }
317
318 /** Prints the fields of a document for human consumption. */
319 public final String toString() {
320 StringBuffer buffer = new StringBuffer();
321 buffer.append("Document<");
322 for (int i = 0; i < fields.size(); i++) {
323 Fieldable field = (Fieldable)fields.get(i);
324 buffer.append(field.toString());
325 if (i != fields.size()-1)
326 buffer.append(" ");
327 }
328 buffer.append(">");
329 return buffer.toString();
330 }
331 }