Skip to content

Commit

Permalink
Added few more sample code for Search API. (#173)
Browse files Browse the repository at this point in the history
* Moved some checkstyle modules to java-repo-tools
* Moved some lines in .gitignore to the top level
  • Loading branch information
Takashi Matsuo authored and lesv committed Apr 21, 2016
1 parent 85c23c2 commit f41f9c9
Show file tree
Hide file tree
Showing 19 changed files with 473 additions and 281 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ service-account.json
# intellij
.idea/
*.iml

# Eclipse files
.project
.classpath
.settings
7 changes: 0 additions & 7 deletions appengine/search/.gitignore

This file was deleted.

223 changes: 0 additions & 223 deletions appengine/search/google-checks.xml

This file was deleted.

16 changes: 0 additions & 16 deletions appengine/search/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,6 @@ Copyright 2015 Google Inc. All Rights Reserved.
<!-- for hot reload of the web application -->
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<configuration>
<configLocation>google-checks.xml</configLocation>
<consoleOutput>true</consoleOutput>
<failOnViolation>true</failOnViolation>
<failsOnError>true</failsOnError>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<suppressionsLocation>suppressions.xml</suppressionsLocation>
</configuration>
<executions>
<execution><goals><goal>check</goal></goals></execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>3.3</version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
import com.google.appengine.api.search.GetResponse;
// [END delete_import]

//CHECKSTYLE:OFF
// CHECKSTYLE:OFF
import com.google.appengine.api.search.Field;
import com.google.appengine.api.search.Index;
import com.google.appengine.api.search.IndexSpec;
import com.google.appengine.api.search.SearchServiceFactory;
// @formatter:on
//CHECKSTYLE:ON
// CHECKSTYLE:ON

import java.io.IOException;
import java.io.PrintWriter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
import com.google.appengine.api.search.IndexSpec;
import com.google.appengine.api.search.SearchServiceFactory;

//CHECKSTYLE:OFF
// CHECKSTYLE:OFF
// [START get_document_import]
import com.google.appengine.api.search.GetRequest;
import com.google.appengine.api.search.GetResponse;
// [END get_document_import]
// @formatter:on
//CHECKSTYLE:ON
// CHECKSTYLE:ON

import java.io.IOException;
import java.io.PrintWriter;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.appengine.search;

import com.google.appengine.api.search.Document;
import com.google.appengine.api.search.Field;
import com.google.appengine.api.search.SearchServiceFactory;

// @formatter:off
// CHECKSTYLE:OFF
// [START schema_import]
import com.google.appengine.api.search.Field.FieldType;
import com.google.appengine.api.search.Index;
import com.google.appengine.api.search.GetIndexesRequest;
import com.google.appengine.api.search.GetResponse;
import com.google.appengine.api.search.Schema;
// [END schema_import]
// @formatter:on
// CHECKSTYLE:ON

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@SuppressWarnings("serial")
public class SchemaServlet extends HttpServlet {

private static final String SEARCH_INDEX = "schemaIndex";

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
PrintWriter out = resp.getWriter();
Document doc = Document.newBuilder()
.setId("theOnlyCar")
.addField(Field.newBuilder().setName("maker").setText("Toyota"))
.addField(Field.newBuilder().setName("price").setNumber(300000))
.addField(Field.newBuilder().setName("color").setText("lightblue"))
.addField(Field.newBuilder().setName("model").setText("Prius"))
.build();
try {
Utils.indexADocument(SEARCH_INDEX, doc);
} catch (InterruptedException e) {
// ignore
}
// [START list_schema]
GetResponse<Index> response = SearchServiceFactory.getSearchService().getIndexes(
GetIndexesRequest.newBuilder().setSchemaFetched(true).build());

// List out elements of each Schema
for (Index index : response) {
Schema schema = index.getSchema();
for (String fieldName : schema.getFieldNames()) {
List<FieldType> typesForField = schema.getFieldTypes(fieldName);
// Just printing out the field names and types
for (FieldType type : typesForField) {
out.println(index.getName() + ":" + fieldName + ":" + type.name());
}
}
}
// [START list_schema]
}
}
Loading

0 comments on commit f41f9c9

Please sign in to comment.