001 package gate.creole.annic.apache.lucene.document;
002
003 /**
004 * Copyright 2004 The Apache Software Foundation
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 import java.util.Enumeration;
020 import java.util.Iterator;
021 import java.util.List;
022 import java.util.ArrayList;
023 import java.util.Vector;
024 import gate.creole.annic.apache.lucene.index.IndexReader; // for javadoc
025 import gate.creole.annic.apache.lucene.search.Searcher; // for javadoc
026 import gate.creole.annic.apache.lucene.search.Hits; // for javadoc
027
028 /** Documents are the unit of indexing and search.
029 *
030 * A Document is a set of fields. Each field has a name and a textual value.
031 * A field may be {@link Field#isStored() stored} with the document, in which
032 * case it is returned with search hits on the document. Thus each document
033 * should typically contain one or more stored fields which uniquely identify
034 * it.
035 *
036 * <p>Note that fields which are <i>not</i> {@link Field#isStored() stored} are
037 * <i>not</i> available in documents retrieved from the index, e.g. with {@link
038 * Hits#doc(int)}, {@link Searcher#doc(int)} or {@link
039 * IndexReader#document(int)}.
040 */
041
042 public final class Document implements java.io.Serializable {
043 List fields = new Vector();
044 private float boost = 1.0f;
045
046 /** Constructs a new document with no fields. */
047 public Document() {}
048
049
050 /** Sets a boost factor for hits on any field of this document. This value
051 * will be multiplied into the score of all hits on this document.
052 *
053 * <p>Values are multiplied into the value of {@link Field#getBoost()} of
054 * each field in this document. Thus, this method in effect sets a default
055 * boost for the fields of this document.
056 *
057 * @see Field#setBoost(float)
058 */
059 public void setBoost(float boost) {
060 this.boost = boost;
061 }
062
063 /** Returns the boost factor for hits on any field of this document.
064 *
065 * <p>The default value is 1.0.
066 *
067 * <p>Note: This value is not stored directly with the document in the index.
068 * Documents returned from {@link IndexReader#document(int)} and
069 * {@link Hits#doc(int)} may thus not have the same value present as when
070 * this document was indexed.
071 *
072 * @see #setBoost(float)
073 */
074 public float getBoost() {
075 return boost;
076 }
077
078 /**
079 * <p>Adds a field to a document. Several fields may be added with
080 * the same name. In this case, if the fields are indexed, their text is
081 * treated as though appended for the purposes of search.</p>
082 * <p> Note that add like the removeField(s) methods only makes sense
083 * prior to adding a document to an index. These methods cannot
084 * be used to change the content of an existing index! In order to achieve this,
085 * a document has to be deleted from an index and a new changed version of that
086 * document has to be added.</p>
087 */
088 public final void add(Field field) {
089 fields.add(field);
090 }
091
092 /**
093 * <p>Removes field with the specified name from the document.
094 * If multiple fields exist with this name, this method removes the first field that has been added.
095 * If there is no field with the specified name, the document remains unchanged.</p>
096 * <p> Note that the removeField(s) methods like the add method only make sense
097 * prior to adding a document to an index. These methods cannot
098 * be used to change the content of an existing index! In order to achieve this,
099 * 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 Field field = (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 it = fields.iterator();
124 while (it.hasNext()) {
125 Field field = (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 */
136 public final Field getField(String name) {
137 for (int i = 0; i < fields.size(); i++) {
138 Field field = (Field)fields.get(i);
139 if (field.name().equals(name))
140 return field;
141 }
142 return null;
143 }
144
145 /** Returns the string value of the field with the given name if any exist in
146 * this document, or null. If multiple fields exist with this name, this
147 * method returns the first value added.
148 */
149 public final String get(String name) {
150 Field field = getField(name);
151 if (field != null)
152 return field.stringValue();
153 else
154 return null;
155 }
156
157 /** Returns an Enumeration of all the fields in a document. */
158 public final Enumeration fields() {
159 return ((Vector)fields).elements();
160 }
161
162 /**
163 * Returns an array of {@link Field}s with the given name.
164 * This method can return <code>null</code>.
165 *
166 * @param name the name of the field
167 * @return a <code>Field[]</code> array
168 */
169 public final Field[] getFields(String name) {
170 List result = new ArrayList();
171 for (int i = 0; i < fields.size(); i++) {
172 Field field = (Field)fields.get(i);
173 if (field.name().equals(name)) {
174 result.add(field);
175 }
176 }
177
178 if (result.size() == 0)
179 return null;
180
181 return (Field[])result.toArray(new Field[result.size()]);
182 }
183
184 /**
185 * Returns an array of values of the field specified as the method parameter.
186 * This method can return <code>null</code>.
187 *
188 * @param name the name of the field
189 * @return a <code>String[]</code> of field values
190 */
191 public final String[] getValues(String name) {
192 Field[] namedFields = getFields(name);
193 if (namedFields == null)
194 return null;
195 String[] values = new String[namedFields.length];
196 for (int i = 0; i < namedFields.length; i++) {
197 values[i] = namedFields[i].stringValue();
198 }
199 return values;
200 }
201
202 /** Prints the fields of a document for human consumption. */
203 public final String toString() {
204 StringBuffer buffer = new StringBuffer();
205 buffer.append("Document<");
206 for (int i = 0; i < fields.size(); i++) {
207 Field field = (Field)fields.get(i);
208 buffer.append(field.toString());
209 if (i != fields.size()-1)
210 buffer.append(" ");
211 }
212 buffer.append(">");
213 return buffer.toString();
214 }
215 }
|