|
| 1 | +/** |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.appengine.search; |
| 18 | + |
| 19 | +import com.google.appengine.api.search.Document; |
| 20 | +import com.google.appengine.api.search.Field; |
| 21 | +import com.google.appengine.api.search.SearchServiceFactory; |
| 22 | + |
| 23 | +// @formatter:off |
| 24 | +// CHECKSTYLE:OFF |
| 25 | +// [START schema_import] |
| 26 | +import com.google.appengine.api.search.Field.FieldType; |
| 27 | +import com.google.appengine.api.search.Index; |
| 28 | +import com.google.appengine.api.search.GetIndexesRequest; |
| 29 | +import com.google.appengine.api.search.GetResponse; |
| 30 | +import com.google.appengine.api.search.Schema; |
| 31 | +// [END schema_import] |
| 32 | +// @formatter:on |
| 33 | +// CHECKSTYLE:ON |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.io.PrintWriter; |
| 37 | +import java.util.List; |
| 38 | +import javax.servlet.http.HttpServlet; |
| 39 | +import javax.servlet.http.HttpServletRequest; |
| 40 | +import javax.servlet.http.HttpServletResponse; |
| 41 | + |
| 42 | + |
| 43 | +@SuppressWarnings("serial") |
| 44 | +public class SchemaServlet extends HttpServlet { |
| 45 | + |
| 46 | + private static final String SEARCH_INDEX = "schemaIndex"; |
| 47 | + |
| 48 | + @Override |
| 49 | + public void doGet(HttpServletRequest req, HttpServletResponse resp) |
| 50 | + throws IOException { |
| 51 | + PrintWriter out = resp.getWriter(); |
| 52 | + Document doc = Document.newBuilder() |
| 53 | + .setId("theOnlyCar") |
| 54 | + .addField(Field.newBuilder().setName("maker").setText("Toyota")) |
| 55 | + .addField(Field.newBuilder().setName("price").setNumber(300000)) |
| 56 | + .addField(Field.newBuilder().setName("color").setText("lightblue")) |
| 57 | + .addField(Field.newBuilder().setName("model").setText("Prius")) |
| 58 | + .build(); |
| 59 | + try { |
| 60 | + Utils.indexADocument(SEARCH_INDEX, doc); |
| 61 | + } catch (InterruptedException e) { |
| 62 | + // ignore |
| 63 | + } |
| 64 | + // [START list_schema] |
| 65 | + GetResponse<Index> response = SearchServiceFactory.getSearchService().getIndexes( |
| 66 | + GetIndexesRequest.newBuilder().setSchemaFetched(true).build()); |
| 67 | + |
| 68 | + // List out elements of each Schema |
| 69 | + for (Index index : response) { |
| 70 | + Schema schema = index.getSchema(); |
| 71 | + for (String fieldName : schema.getFieldNames()) { |
| 72 | + List<FieldType> typesForField = schema.getFieldTypes(fieldName); |
| 73 | + // Just printing out the field names and types |
| 74 | + for (FieldType type : typesForField) { |
| 75 | + out.println(index.getName() + ":" + fieldName + ":" + type.name()); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + // [START list_schema] |
| 80 | + } |
| 81 | +} |
0 commit comments