pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / document / Document.java
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<Fieldable> fields = new ArrayList<Fieldable>();
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<Fieldable> it = fields.iterator();
104     while (it.hasNext()) {
105       Fieldable field = 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<Fieldable> it = fields.iterator();
124     while (it.hasNext()) {
125       Fieldable field = 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 or {@link NumericField}.
136    * @deprecated use {@link #getFieldable} instead and cast depending on
137    * data type.
138    * @throws ClassCastException if you try to retrieve a numerical or
139    * lazy loaded field.
140    */
141   @Deprecated
142   public final Field getField(String name) {
143     return (Field) getFieldable(name);
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 (Fieldable field : fields) {
153      if (field.name().equals(name))
154        return field;
155    }
156    return null;
157  }
158
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}.
165    */
166   public final String get(String name) {
167    for (Fieldable field : fields) {
168       if (field.name().equals(name) && (!field.isBinary()))
169         return field.stringValue();
170     }
171     return null;
172   }
173
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)}.
179    */
180   public final List<Fieldable> getFields() {
181     return fields;
182   }
183
184   private final static Field[] NO_FIELDS = new Field[0];
185   
186   /**
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}.
191    *
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
195    * data type.
196    * @throws ClassCastException if you try to retrieve a numerical or
197    * lazy loaded field.
198    */
199    @Deprecated
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);
205        }
206      }
207
208      if (result.size() == 0)
209        return NO_FIELDS;
210
211      return result.toArray(new Field[result.size()]);
212    }
213
214
215    private final static Fieldable[] NO_FIELDABLES = new Fieldable[0];
216
217    /**
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.
221    *
222    * @param name the name of the field
223    * @return a <code>Fieldable[]</code> array
224    */
225    public Fieldable[] getFieldables(String name) {
226      List<Fieldable> result = new ArrayList<Fieldable>();
227      for (Fieldable field : fields) {
228        if (field.name().equals(name)) {
229          result.add(field);
230        }
231      }
232
233      if (result.size() == 0)
234        return NO_FIELDABLES;
235
236      return result.toArray(new Fieldable[result.size()]);
237    }
238
239
240    private final static String[] NO_STRINGS = new String[0];
241
242   /**
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
250    */
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());
256     }
257     
258     if (result.size() == 0)
259       return NO_STRINGS;
260     
261     return result.toArray(new String[result.size()]);
262   }
263
264   private final static byte[][] NO_BYTES = new byte[0][];
265
266   /**
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
270   * returns null.
271   *
272   * @param name the name of the field
273   * @return a <code>byte[][]</code> of binary field values
274   */
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());
280     }
281   
282     if (result.size() == 0)
283       return NO_BYTES;
284   
285     return result.toArray(new byte[result.size()][]);
286   }
287   
288   /**
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.
293   *
294   * @param name the name of the field.
295   * @return a <code>byte[]</code> containing the binary field value or <code>null</code>
296   */
297   public final byte[] getBinaryValue(String name) {
298     for (Fieldable field : fields) {
299       if (field.name().equals(name) && (field.isBinary()))
300         return field.getBinaryValue();
301     }
302     return null;
303   }
304   
305   /** Prints the fields of a document for human consumption. */
306   @Override
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)
314         buffer.append(" ");
315     }
316     buffer.append(">");
317     return buffer.toString();
318   }
319 }