1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.search
;
5 import com
.google
.appengine
.api
.search
.checkers
.FieldChecker
;
6 import com
.google
.apphosting
.api
.search
.DocumentPb
;
8 import java
.util
.ArrayList
;
9 import java
.util
.Collections
;
10 import java
.util
.HashMap
;
11 import java
.util
.List
;
16 * Contains information about the kinds of document {@link Field Fields}
17 * which are supported by the {@link Index}.
20 * // Get the searchService for the default namespace
21 * SearchService searchService = SearchServiceFactory.getSearchService();
23 * // List the first page of indexes available and retrieve schemas
24 * ListIndexesResponse response = searchService.listIndexes(
25 * ListIndexesRequest.newBuilder().setSchemaFetched(true).build());
27 * // List out elements of Schema
28 * for (Index index : response) {
29 * Schema schema = index.getSchema();
30 * for (String fieldName : schema.getFieldNames()) {
31 * List<FieldType> typesForField = schema.getFieldTypes(fieldName);
37 public final class Schema
{
40 * A builder which constructs Schema objects.
42 public static final class Builder
{
43 private final Map
<String
, List
<Field
.FieldType
>> fieldMap
=
44 new HashMap
<String
, List
<Field
.FieldType
>>();
47 * Constructs a builder for a schema.
53 * Adds typed field name to the schema builder. Allows multiple
54 * field types with the same name.
56 * @param fieldName the field name to add to the schema
57 * @return this document builder
59 public Builder
addTypedField(String fieldName
, Field
.FieldType fieldType
) {
60 FieldChecker
.checkFieldName(fieldName
);
61 List
<Field
.FieldType
> types
= fieldMap
.get(fieldName
);
63 types
= new ArrayList
<Field
.FieldType
>();
64 fieldMap
.put(fieldName
, types
);
71 * Builds a valid document. The builder must have set a valid document
72 * id, and have a non-empty set of valid fields.
74 * @return the schema built by this builder
75 * @throws IllegalArgumentException if the document built is not valid
77 public Schema
build() {
78 return new Schema(this);
82 private final Map
<String
, List
<Field
.FieldType
>> fieldMap
;
85 * Creates a {@link Schema} from a {@link Builder}.
87 * @param builder the builder
89 private Schema(Builder builder
) {
90 this.fieldMap
= Collections
.unmodifiableMap(builder
.fieldMap
);
94 * @return the set of field names supported in the schema
96 public Set
<String
> getFieldNames() {
97 return fieldMap
.keySet();
101 * @param fieldName the name of the field to return supported types
102 * @return a list of {@link Field.FieldType} supported for the given field
105 public List
<Field
.FieldType
> getFieldTypes(String fieldName
) {
106 return Collections
.unmodifiableList(fieldMap
.get(fieldName
));
110 public String
toString() {
111 return String
.format("Schema{fieldMap: %s}", fieldMap
);
115 * Maps between DocumentPb.FieldValue.ContentType enums and the public
118 static Field
.FieldType
mapPBFieldTypeToPublic(DocumentPb
.FieldValue
.ContentType type
) {
121 return Field
.FieldType
.TEXT
;
123 return Field
.FieldType
.HTML
;
125 return Field
.FieldType
.ATOM
;
127 return Field
.FieldType
.DATE
;
129 return Field
.FieldType
.NUMBER
;
131 return Field
.FieldType
.GEO_POINT
;
133 throw new IllegalArgumentException("Unsupported field type " + type
);
138 * Maps between public Field.Field enums and the protocol buffer
139 * TypeDocumentPb.FieldValue.ContentType enums.
141 static DocumentPb
.FieldValue
.ContentType
mapPublicFieldTypeToPB(Field
.FieldType type
) {
144 return DocumentPb
.FieldValue
.ContentType
.TEXT
;
146 return DocumentPb
.FieldValue
.ContentType
.HTML
;
148 return DocumentPb
.FieldValue
.ContentType
.ATOM
;
150 return DocumentPb
.FieldValue
.ContentType
.DATE
;
152 return DocumentPb
.FieldValue
.ContentType
.NUMBER
;
154 return DocumentPb
.FieldValue
.ContentType
.GEO
;
156 throw new IllegalArgumentException("Unsupported field type " + type
);
161 * Creates a {@link Schema} from a {@link SearchServicePb.IndexMetadata}.
163 * @param metadata the proto buffer containing supported field names
165 * @return a {@link Schema} containing supported field names and field
166 * types for those names
168 static Schema
createSchema(SearchServicePb
.IndexMetadata metadata
) {
169 if (metadata
.getFieldCount() == 0) {
172 Map
<String
, List
<Field
.FieldType
>> fieldMap
= new HashMap
<String
, List
<Field
.FieldType
>>();
173 Builder builder
= newBuilder();
174 for (DocumentPb
.FieldTypes fieldTypes
: metadata
.getFieldList()) {
175 String fieldName
= fieldTypes
.getName();
176 for (DocumentPb
.FieldValue
.ContentType type
: fieldTypes
.getTypeList()) {
177 builder
.addTypedField(fieldName
, mapPBFieldTypeToPublic(type
));
180 return builder
.build();
184 public int hashCode() {
185 return fieldMap
.hashCode();
189 public boolean equals(Object obj
) {
196 if (getClass() != obj
.getClass()) {
199 return fieldMap
.equals(((Schema
) obj
).fieldMap
);
203 * Creates a schema builder.
205 * @return a new builder for creating a schema
207 public static Builder
newBuilder() {
208 return new Builder();