Skip to content

Commit 13f1036

Browse files
author
Takashi Matsuo
committed
Added few more sample code for Search API.
* Moved some checkstyle modules to java-repo-tools * Moved some lines in .gitignore to the top level
1 parent e486daf commit 13f1036

19 files changed

+476
-281
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@ service-account.json
3030
# intellij
3131
.idea/
3232
*.iml
33+
34+
# Eclipse files
35+
.project
36+
.classpath
37+
.settings

appengine/search/.gitignore

-7
This file was deleted.

appengine/search/google-checks.xml

-223
This file was deleted.

appengine/search/pom.xml

-16
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,6 @@ Copyright 2015 Google Inc. All Rights Reserved.
8181
<!-- for hot reload of the web application -->
8282
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
8383
<plugins>
84-
<plugin>
85-
<groupId>org.apache.maven.plugins</groupId>
86-
<artifactId>maven-checkstyle-plugin</artifactId>
87-
<version>2.17</version>
88-
<configuration>
89-
<configLocation>google-checks.xml</configLocation>
90-
<consoleOutput>true</consoleOutput>
91-
<failOnViolation>true</failOnViolation>
92-
<failsOnError>true</failsOnError>
93-
<includeTestSourceDirectory>true</includeTestSourceDirectory>
94-
<suppressionsLocation>suppressions.xml</suppressionsLocation>
95-
</configuration>
96-
<executions>
97-
<execution><goals><goal>check</goal></goals></execution>
98-
</executions>
99-
</plugin>
10084
<plugin>
10185
<groupId>org.apache.maven.plugins</groupId>
10286
<version>3.3</version>

appengine/search/src/main/java/com/example/appengine/search/DeleteServlet.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
import com.google.appengine.api.search.GetResponse;
2424
// [END delete_import]
2525

26-
//CHECKSTYLE:OFF
26+
// CHECKSTYLE:OFF
2727
import com.google.appengine.api.search.Field;
2828
import com.google.appengine.api.search.Index;
2929
import com.google.appengine.api.search.IndexSpec;
3030
import com.google.appengine.api.search.SearchServiceFactory;
3131
// @formatter:on
32-
//CHECKSTYLE:ON
32+
// CHECKSTYLE:ON
3333

3434
import java.io.IOException;
3535
import java.io.PrintWriter;

appengine/search/src/main/java/com/example/appengine/search/IndexServlet.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
import com.google.appengine.api.search.IndexSpec;
2424
import com.google.appengine.api.search.SearchServiceFactory;
2525

26-
//CHECKSTYLE:OFF
26+
// CHECKSTYLE:OFF
2727
// [START get_document_import]
2828
import com.google.appengine.api.search.GetRequest;
2929
import com.google.appengine.api.search.GetResponse;
3030
// [END get_document_import]
3131
// @formatter:on
32-
//CHECKSTYLE:ON
32+
// CHECKSTYLE:ON
3333

3434
import java.io.IOException;
3535
import java.io.PrintWriter;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)