1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.search
;
5 import com
.google
.appengine
.api
.search
.checkers
.DocumentChecker
;
6 import com
.google
.appengine
.api
.search
.checkers
.FieldChecker
;
7 import com
.google
.appengine
.api
.search
.checkers
.Preconditions
;
8 import com
.google
.apphosting
.api
.search
.DocumentPb
;
10 import java
.io
.Serializable
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collections
;
13 import java
.util
.HashMap
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
16 import java
.util
.Locale
;
21 * Represents a user generated document. The following example shows how to
22 * create a document consisting of a set of fields, some with plain text and
25 * Document document = Document.newBuilder().setId("document id")
26 * .setLocale(Locale.UK)
27 * .addField(Field.newBuilder()
29 * .setText("going for dinner"))
30 * .addField(Field.newBuilder()
32 * .setHTML("<html>I found a restaurant.</html>"))
33 * .addField(Field.newBuilder()
34 * .setName("signature")
35 * .setText("ten post jest przeznaczony dla odbiorcy")
36 * .setLocale(new Locale("pl")))
37 * .addField(Field.newBuilder()
40 * .addField(Field.newBuilder()
46 * The following example shows how to access the fields within a document:
48 * Document document = ...
50 * for (Field field : document.getFields()) {
51 * switch (field.getType()) {
52 * case TEXT: use(field.getText()); break;
53 * case HTML: use(field.getHtml()); break;
54 * case ATOM: use(field.getAtom()); break;
55 * case DATE: use(field.getDate()); break;
61 public class Document
implements Serializable
{
63 static final int MAX_FIELDS_TO_STRING
= 10;
66 * A builder of documents. This is not thread-safe.
68 public static class Builder
{
69 private final Map
<String
, List
<Field
>> fieldMap
= new HashMap
<String
, List
<Field
>>();
70 private final List
<Field
> fields
= new ArrayList
<Field
>();
72 private final Set
<String
> noRepeatFields
= new HashSet
<String
>();
73 private String documentId
; private Locale locale
; private Integer rank
;
76 * Constructs a builder for a document.
82 * Set the document id to a unique valid value. A valid document id must
83 * be a printable ASCII string of between 1 and
84 * {@literal DocumentChecker#MAXIMUM_DOCUMENT_ID_LENGTH} characters, and
85 * also not start with '!' which is reserved. If no document id is
86 * provided, then the search service will provide one when the document
89 * @param documentId the unique id for the document to be built
90 * @return this builder
91 * @throws IllegalArgumentException if documentId is not valid
93 public Builder
setId(String documentId
) {
94 if (documentId
!= null) {
95 this.documentId
= DocumentChecker
.checkDocumentId(documentId
);
101 * Adds the field builder to the document builder. Allows multiple
102 * fields with the same name.
104 * @param builder the builder of the field to add
105 * @return this document builder
107 public Builder
addField(Field
.Builder builder
) {
108 Preconditions
.checkNotNull(builder
, "field builder cannot be null");
109 return addField(builder
.build());
113 * Adds the field to the builder. Allows multiple
114 * fields with the same name.
116 * @param field the field to add
117 * @return this builder
118 * @throws IllegalArgumentException if the field is invalid
120 public Builder
addField(Field field
) {
121 Preconditions
.checkNotNull(field
, "field cannot be null");
122 if (field
.getType() == Field
.FieldType
.DATE
|| field
.getType() == Field
.FieldType
.NUMBER
) {
123 Preconditions
.checkArgument(!noRepeatFields
.contains(field
.getName()),
124 "Number and date fields cannot be repeated.");
125 noRepeatFields
.add(field
.getName());
129 List
<Field
> fieldsForName
= fieldMap
.get(field
.getName());
130 if (fieldsForName
== null) {
131 fieldsForName
= new ArrayList
<Field
>();
132 fieldMap
.put(field
.getName(), fieldsForName
);
134 fieldsForName
.add(field
);
139 * Sets the {@link Locale} the document is written in.
141 * @param locale the {@link Locale} the document is written in
142 * @return this document builder
144 public Builder
setLocale(Locale locale
) {
145 this.locale
= locale
;
150 * Sets the rank of this document, which determines the order of documents
151 * returned by search, if no sorting or scoring is given. If it is not
152 * specified, then the number of seconds since 2011/1/1 will be used.
154 * @param rank the rank of this document
155 * @return this builder
157 public Builder
setRank(int rank
) {
163 * Builds a valid document. The builder must have set a valid document
164 * id, and have a non-empty set of valid fields.
166 * @return the document built by this builder
167 * @throws IllegalArgumentException if the document built is not valid
169 public Document
build() {
170 return new Document(this);
174 private static final long serialVersionUID
= 309382038422977263L;
176 private final String documentId
;
177 private final Map
<String
, List
<Field
>> fieldMap
;
178 private final List
<Field
> fields
;
180 private final int rank
;
182 private final Locale locale
;
185 * Constructs a document with the given builder.
187 * @param builder the builder capable of building a document
189 protected Document(Builder builder
) {
190 documentId
= builder
.documentId
;
191 fieldMap
= new HashMap
<String
, List
<Field
>>(builder
.fieldMap
);
192 fields
= Collections
.unmodifiableList(builder
.fields
);
193 locale
= builder
.locale
;
194 rank
= Util
.defaultIfNull(builder
.rank
, DocumentChecker
.getNumberOfSecondsSince());
199 * @return an iterable of {@link Field} in the document
201 public Iterable
<Field
> getFields() {
206 * @return an unmodifiable {@link Set} of the field names in the document
208 public Set
<String
> getFieldNames() {
209 return Collections
.unmodifiableSet(fieldMap
.keySet());
213 * Get an iterable of all fields with the given name.
215 * @param name the name of the field name whose values are to be returned
216 * @return an unmodifiable {@link Iterable} of {@link Field} with the given name
219 @Deprecated public Iterable
<Field
> getFields(String name
) {
220 List
<Field
> fieldsForName
= fieldMap
.get(name
);
221 if (fieldsForName
== null) {
224 return Collections
.unmodifiableList(fieldsForName
);
228 * @param name the name of the field name whose values are to be returned
229 * @return an unmodifiable {@link Iterable} of {@link Field} with the given name
231 * @deprecated Use {@link #getFields(String)} to get all fields with a specific name.
233 @Deprecated public Iterable
<Field
> getField(String name
) {
234 return getFields(name
);
238 * Returns the single field with the given name.
240 * @param name the name of the field to return
241 * @return the single field with name
242 * @throws IllegalArgumentException if the document does not have exactly
243 * one field with the name
245 public Field
getOnlyField(String name
) {
246 List
<Field
> fieldsForName
= fieldMap
.get(name
);
247 Preconditions
.checkArgument(
248 fieldsForName
!= null && fieldsForName
.size() == 1,
249 "Field %s is present %d times; expected 1",
250 name
, (fieldsForName
== null?
0 : fieldsForName
.size()));
251 return fieldsForName
.get(0);
255 * Returns the number of times a field with the given name is present
258 * @param name the name of the field to be counted
259 * @return the number of times a field with the given name is present
261 public int getFieldCount(String name
) {
262 List
<Field
> fieldsForName
= fieldMap
.get(name
);
263 return fieldsForName
== null ?
0 : fieldsForName
.size();
267 * @return the id of the document
269 public String
getId() {
274 * @return the {@link Locale} the document is written in. Can be null
276 public Locale
getLocale() {
281 * Returns the rank of this document. A document's rank is used to
282 * determine the default order in which documents are returned by
283 * search, if no sorting or scoring is specified.
285 * @return the rank of this document
287 public int getRank() {
292 public int hashCode() {
293 return documentId
.hashCode();
297 public boolean equals(Object object
) {
298 if (this == object
) {
301 if (!(object
instanceof Document
)) {
304 Document doc
= (Document
) object
;
305 return documentId
.equals(doc
.getId());
309 * Checks whether the document is valid. A document is valid if
310 * it has a valid document id, a locale, a non-empty collection of
313 * @return this document
314 * @throws IllegalArgumentException if the document has an invalid
315 * document id, has no fields, or some field is invalid
317 private Document
checkValid() {
318 if (documentId
!= null) {
319 DocumentChecker
.checkDocumentId(documentId
);
321 Preconditions
.checkArgument(fieldMap
!= null,
322 "Null map of fields in document for indexing");
323 Preconditions
.checkArgument(fields
!= null,
324 "Null list of fields in document for indexing");
329 * Creates a new document builder. You must use this method to obtain a new
330 * builder. The returned builder must be used to specify all properties of
331 * the document. To obtain the document call the {@link Builder#build()}
332 * method on the returned builder.
334 * @return a builder which constructs a document object
336 public static Builder
newBuilder() {
337 return new Builder();
341 * Creates a new document builder from the given document. All document
342 * properties are copied to the returned builder.
344 * @param document the document protocol buffer to build a document object
346 * @return the document builder initialized from a document protocol buffer
347 * @throws SearchException if a field value is invalid
349 static Builder
newBuilder(DocumentPb
.Document document
) {
350 Document
.Builder docBuilder
= Document
.newBuilder().setId(document
.getId());
351 if (document
.hasLanguage()) {
352 docBuilder
.setLocale(FieldChecker
.parseLocale(document
.getLanguage()));
354 for (DocumentPb
.Field field
: document
.getFieldList()) {
355 docBuilder
.addField(Field
.newBuilder(field
));
357 if (document
.hasOrderId()) {
358 docBuilder
.setRank(document
.getOrderId());
364 * Copies a {@link Document} object into a {@link DocumentPb.Document}
367 * @return the document protocol buffer copy of the document object
368 * @throws IllegalArgumentException if any parts of the document are invalid
369 * or the document protocol buffer is too large
371 DocumentPb
.Document
copyToProtocolBuffer() {
372 DocumentPb
.Document
.Builder docBuilder
= DocumentPb
.Document
.newBuilder();
373 if (documentId
!= null) {
374 docBuilder
.setId(documentId
);
376 if (locale
!= null) {
377 docBuilder
.setLanguage(locale
.toString());
379 for (Field field
: fields
) {
380 docBuilder
.addField(field
.copyToProtocolBuffer());
382 docBuilder
.setOrderId(rank
);
383 return DocumentChecker
.checkValid(docBuilder
.build());
387 public String
toString() {
388 return String
.format("Document(documentId=%s, fields=%s%s, rank=%d)",
390 Util
.iterableToString(fields
, MAX_FIELDS_TO_STRING
),
391 Util
.fieldToString("locale", locale
), rank
);