1 package org.apache.lucene.document;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
25 /** Documents are the unit of indexing and search.
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
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)}.
39 public final class Document implements java.io.Serializable {
40 List<Fieldable> fields = new ArrayList<Fieldable>();
41 private float boost = 1.0f;
43 /** Constructs a new document with no fields. */
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.
50 * <p>The default value is 1.0.
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.
56 * @see Fieldable#setBoost(float)
58 public void setBoost(float boost) {
62 /** Returns, at indexing time, the boost factor as set by {@link #setBoost(float)}.
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}.)
72 * @see #setBoost(float)
74 public float getBoost() {
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>
88 public final void add(Fieldable field) {
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>
102 public final void removeField(String name) {
103 Iterator<Fieldable> it = fields.iterator();
104 while (it.hasNext()) {
105 Fieldable field = it.next();
106 if (field.name().equals(name)) {
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>
122 public final void removeFields(String name) {
123 Iterator<Fieldable> it = fields.iterator();
124 while (it.hasNext()) {
125 Fieldable field = it.next();
126 if (field.name().equals(name)) {
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
135 * Do not use this method with lazy loaded fields or {@link NumericField}.
136 * @deprecated use {@link #getFieldable} instead and cast depending on
138 * @throws ClassCastException if you try to retrieve a numerical or
142 public final Field getField(String name) {
143 return (Field) getFieldable(name);
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
151 public Fieldable getFieldable(String name) {
152 for (Fieldable field : fields) {
153 if (field.name().equals(name))
159 /** Returns the string value of the field with the given name if any exist in
160 * this document, or null. If multiple fields exist with this name, this
161 * method returns the first value added. If only binary fields with this name
162 * exist, returns null.
163 * For {@link NumericField} it returns the string value of the number. If you want
164 * the actual {@code NumericField} instance back, use {@link #getFieldable}.
166 public final String get(String name) {
167 for (Fieldable field : fields) {
168 if (field.name().equals(name) && (!field.isBinary()))
169 return field.stringValue();
174 /** Returns a List of all the fields in a document.
175 * <p>Note that fields which are <i>not</i> {@link Fieldable#isStored() stored} are
176 * <i>not</i> available in documents retrieved from the
177 * index, e.g. {@link Searcher#doc(int)} or {@link
178 * IndexReader#document(int)}.
180 public final List<Fieldable> getFields() {
184 private final static Field[] NO_FIELDS = new Field[0];
187 * Returns an array of {@link Field}s with the given name.
188 * This method returns an empty array when there are no
189 * matching fields. It never returns null.
190 * Do not use this method with lazy loaded fields or {@link NumericField}.
192 * @param name the name of the field
193 * @return a <code>Field[]</code> array
194 * @deprecated use {@link #getFieldable} instead and cast depending on
196 * @throws ClassCastException if you try to retrieve a numerical or
200 public final Field[] getFields(String name) {
201 List<Field> result = new ArrayList<Field>();
202 for (Fieldable field : fields) {
203 if (field.name().equals(name)) {
204 result.add((Field) field);
208 if (result.size() == 0)
211 return result.toArray(new Field[result.size()]);
215 private final static Fieldable[] NO_FIELDABLES = new Fieldable[0];
218 * Returns an array of {@link Fieldable}s with the given name.
219 * This method returns an empty array when there are no
220 * matching fields. It never returns null.
222 * @param name the name of the field
223 * @return a <code>Fieldable[]</code> array
225 public Fieldable[] getFieldables(String name) {
226 List<Fieldable> result = new ArrayList<Fieldable>();
227 for (Fieldable field : fields) {
228 if (field.name().equals(name)) {
233 if (result.size() == 0)
234 return NO_FIELDABLES;
236 return result.toArray(new Fieldable[result.size()]);
240 private final static String[] NO_STRINGS = new String[0];
243 * Returns an array of values of the field specified as the method parameter.
244 * This method returns an empty array when there are no
245 * matching fields. It never returns null.
246 * For {@link NumericField}s it returns the string value of the number. If you want
247 * the actual {@code NumericField} instances back, use {@link #getFieldables}.
248 * @param name the name of the field
249 * @return a <code>String[]</code> of field values
251 public final String[] getValues(String name) {
252 List<String> result = new ArrayList<String>();
253 for (Fieldable field : fields) {
254 if (field.name().equals(name) && (!field.isBinary()))
255 result.add(field.stringValue());
258 if (result.size() == 0)
261 return result.toArray(new String[result.size()]);
264 private final static byte[][] NO_BYTES = new byte[0][];
267 * Returns an array of byte arrays for of the fields that have the name specified
268 * as the method parameter. This method returns an empty
269 * array when there are no matching fields. It never
272 * @param name the name of the field
273 * @return a <code>byte[][]</code> of binary field values
275 public final byte[][] getBinaryValues(String name) {
276 List<byte[]> result = new ArrayList<byte[]>();
277 for (Fieldable field : fields) {
278 if (field.name().equals(name) && (field.isBinary()))
279 result.add(field.getBinaryValue());
282 if (result.size() == 0)
285 return result.toArray(new byte[result.size()][]);
289 * Returns an array of bytes for the first (or only) field that has the name
290 * specified as the method parameter. This method will return <code>null</code>
291 * if no binary fields with the specified name are available.
292 * There may be non-binary fields with the same name.
294 * @param name the name of the field.
295 * @return a <code>byte[]</code> containing the binary field value or <code>null</code>
297 public final byte[] getBinaryValue(String name) {
298 for (Fieldable field : fields) {
299 if (field.name().equals(name) && (field.isBinary()))
300 return field.getBinaryValue();
305 /** Prints the fields of a document for human consumption. */
307 public final String toString() {
308 StringBuilder buffer = new StringBuilder();
309 buffer.append("Document<");
310 for (int i = 0; i < fields.size(); i++) {
311 Fieldable field = fields.get(i);
312 buffer.append(field.toString());
313 if (i != fields.size()-1)
317 return buffer.toString();