Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added few more sample code for Search API. #173

Merged
merged 1 commit into from
Apr 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove checkstyle here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah - I guess it's so that it stays in the right place w/ your comments.

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