KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > snapper > wrapper > lucene > Document


1 package org.enhydra.snapper.wrapper.lucene;
2
3 /**
4
5  */

6
7 import java.util.Enumeration JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.Vector JavaDoc;
12
13
14 /** Documents are the unit of indexing and search.
15  *
16  * A Document is a set of fields. Each field has a name and a textual value.
17  * A field may be {@link Field#isStored() stored} with the document, in which
18  * case it is returned with search hits on the document. Thus each document
19  * should typically contain one or more stored fields which uniquely identify
20  * it.
21  *
22  * <p>Note that fields which are <i>not</i> {@link Field#isStored() stored} are
23  * <i>not</i> available in documents retrieved from the index, e.g. with {@link
24  * Hits#doc(int)}, {@link Searcher#doc(int)} or {@link
25  * IndexReader#document(int)}.
26  */

27
28 public final class Document implements java.io.Serializable JavaDoc {
29   List JavaDoc fields = new Vector JavaDoc();
30   private float boost = 1.0f;
31
32   /** Constructs a new document with no fields. */
33   public Document() {}
34
35
36   /** Sets a boost factor for hits on any field of this document. This value
37    * will be multiplied into the score of all hits on this document.
38    *
39    * <p>Values are multiplied into the value of {@link Field#getBoost()} of
40    * each field in this document. Thus, this method in effect sets a default
41    * boost for the fields of this document.
42    *
43    * @see Field#setBoost(float)
44    */

45   public void setBoost(float boost) {
46     this.boost = boost;
47   }
48
49   /** Returns the boost factor for hits on any field of this document.
50    *
51    * <p>The default value is 1.0.
52    *
53    * <p>Note: This value is not stored directly with the document in the index.
54    * Documents returned from {@link IndexReader#document(int)} and
55    * {@link Hits#doc(int)} may thus not have the same value present as when
56    * this document was indexed.
57    *
58    * @see #setBoost(float)
59    */

60   public float getBoost() {
61     return boost;
62   }
63
64   /**
65    * <p>Adds a field to a document. Several fields may be added with
66    * the same name. In this case, if the fields are indexed, their text is
67    * treated as though appended for the purposes of search.</p>
68    * <p> Note that add like the removeField(s) methods only makes sense
69    * prior to adding a document to an index. These methods cannot
70    * be used to change the content of an existing index! In order to achieve this,
71    * a document has to be deleted from an index and a new changed version of that
72    * document has to be added.</p>
73    */

74   public final void add(Field field) {
75     fields.add(field);
76   }
77   
78   /**
79    * <p>Removes field with the specified name from the document.
80    * If multiple fields exist with this name, this method removes the first field that has been added.
81    * If there is no field with the specified name, the document remains unchanged.</p>
82    * <p> Note that the removeField(s) methods like the add method only make 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 removeField(String JavaDoc name) {
89     Iterator JavaDoc it = fields.iterator();
90     while (it.hasNext()) {
91       Field field = (Field)it.next();
92       if (field.name().equals(name)) {
93         it.remove();
94         return;
95       }
96     }
97   }
98   
99   /**
100    * <p>Removes all fields with the given name from the document.
101    * If there is no field with the specified name, the document remains unchanged.</p>
102    * <p> Note that the removeField(s) methods like the add method only make sense
103    * prior to adding a document to an index. These methods cannot
104    * be used to change the content of an existing index! In order to achieve this,
105    * a document has to be deleted from an index and a new changed version of that
106    * document has to be added.</p>
107    */

108   public final void removeFields(String JavaDoc name) {
109     Iterator JavaDoc it = fields.iterator();
110     while (it.hasNext()) {
111       Field field = (Field)it.next();
112       if (field.name().equals(name)) {
113         it.remove();
114       }
115     }
116   }
117
118   /** Returns a field with the given name if any exist in this document, or
119    * null. If multiple fields exists with this name, this method returns the
120    * first value added.
121    */

122   public final Field getField(String JavaDoc name) {
123     for (int i = 0; i < fields.size(); i++) {
124       Field field = (Field)fields.get(i);
125       if (field.name().equals(name))
126     return field;
127     }
128     return null;
129   }
130
131   /** Returns the string value of the field with the given name if any exist in
132    * this document, or null. If multiple fields exist with this name, this
133    * method returns the first value added.
134    */

135   public final String JavaDoc get(String JavaDoc name) {
136     Field field = getField(name);
137     if (field != null)
138       return field.stringValue();
139     else
140       return null;
141   }
142
143   /** Returns an Enumeration of all the fields in a document. */
144   public final Enumeration JavaDoc fields() {
145     return ((Vector JavaDoc)fields).elements();
146   }
147
148   /**
149    * Returns an array of {@link Field}s with the given name.
150    * This method can return <code>null</code>.
151    *
152    * @param name the name of the field
153    * @return a <code>Field[]</code> array
154    */

155    public final Field[] getFields(String JavaDoc name) {
156      List JavaDoc result = new ArrayList JavaDoc();
157      for (int i = 0; i < fields.size(); i++) {
158        Field field = (Field)fields.get(i);
159        if (field.name().equals(name)) {
160          result.add(field);
161        }
162      }
163
164      if (result.size() == 0)
165        return null;
166
167      return (Field[])result.toArray(new Field[result.size()]);
168    }
169
170   /**
171    * Returns an array of values of the field specified as the method parameter.
172    * This method can return <code>null</code>.
173    *
174    * @param name the name of the field
175    * @return a <code>String[]</code> of field values
176    */

177   public final String JavaDoc[] getValues(String JavaDoc name) {
178     Field[] namedFields = getFields(name);
179     if (namedFields == null)
180       return null;
181     String JavaDoc[] values = new String JavaDoc[namedFields.length];
182     for (int i = 0; i < namedFields.length; i++) {
183       values[i] = namedFields[i].stringValue();
184     }
185     return values;
186   }
187
188   /** Prints the fields of a document for human consumption. */
189   public final String JavaDoc toString() {
190     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
191     buffer.append("Document<");
192     for (int i = 0; i < fields.size(); i++) {
193       Field field = (Field)fields.get(i);
194       buffer.append(field.toString());
195       if (i != fields.size()-1)
196         buffer.append(" ");
197     }
198     buffer.append(">");
199     return buffer.toString();
200   }
201 }
202
203
Popular Tags