-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: Ib8644f3b56d1602c3aa79f56ade56c9b9e7f6df1
- Loading branch information
Takashi Matsuo
committed
Apr 20, 2016
1 parent
1b1d17a
commit aaedf38
Showing
11 changed files
with
592 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Eclipse files | ||
.project | ||
.classpath | ||
.settings | ||
|
||
# Target folders | ||
target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Google App Engine Standard Environment Search API Sample | ||
|
||
This sample demonstrates how to use App Engine Search API. | ||
|
||
See the [Google App Engine Search API documentation][search-api-docs] for more | ||
detailed instructions. | ||
|
||
[search-api-docs]: https://cloud.google.com/appengine/docs/java/search/ | ||
|
||
## Setup | ||
1. Update the `<application>` tag in `src/main/webapp/WEB-INF/appengine-web.xml` | ||
with your project name. | ||
1. Update the `<version>` tag in `src/main/webapp/WEB-INF/appengine-web.xml` | ||
with your version name. | ||
|
||
## Running locally | ||
$ mvn appengine:devserver | ||
|
||
## Deploying | ||
$ mvn appengine:update |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<!-- | ||
Copyright 2015 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. | ||
--> | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>war</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<groupId>com.example.appengine</groupId> | ||
<artifactId>appengine-search</artifactId> | ||
<parent> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>doc-samples</artifactId> | ||
<version>1.0.0</version> | ||
<relativePath>../..</relativePath> | ||
</parent> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-api-1.0-sdk</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<version>2.5</version> | ||
<type>jar</type> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- Test Dependencies --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.10</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<version>1.10.19</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-testing</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-api-stubs</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-tools-sdk</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<!-- 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> | ||
<version>3.3</version> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
</configuration> | ||
</plugin> | ||
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). --> | ||
<plugin> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-maven-plugin</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
92 changes: 92 additions & 0 deletions
92
appengine/search/src/main/java/com/example/appengine/search/DeleteServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* 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; | ||
|
||
// @formatter:off | ||
// [START delete_import] | ||
import com.google.appengine.api.search.Document; | ||
import com.google.appengine.api.search.GetRequest; | ||
import com.google.appengine.api.search.GetResponse; | ||
// [END delete_import] | ||
|
||
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 | ||
|
||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* Code snippet for deleting documents from an Index. | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class DeleteServlet extends HttpServlet { | ||
private static final Logger LOG = Logger.getLogger(DeleteServlet.class.getSimpleName()); | ||
|
||
private static final String SEARCH_INDEX = "searchIndexForDelete"; | ||
|
||
private Index getIndex() { | ||
IndexSpec indexSpec = IndexSpec.newBuilder().setName(SEARCH_INDEX).build(); | ||
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec); | ||
return index; | ||
} | ||
|
||
@Override | ||
public void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
throws IOException { | ||
// Put one document to avoid an error | ||
Document document = Document.newBuilder() | ||
.addField(Field.newBuilder().setName("f").setText("v")) | ||
.build(); | ||
try { | ||
Utils.indexADocument(SEARCH_INDEX, document); | ||
} catch (InterruptedException e) { | ||
// ignore | ||
} | ||
// [START delete_documents] | ||
try { | ||
// looping because getRange by default returns up to 100 documents at a time | ||
while (true) { | ||
List<String> docIds = new ArrayList<>(); | ||
// Return a set of doc_ids. | ||
GetRequest request = GetRequest.newBuilder().setReturningIdsOnly(true).build(); | ||
GetResponse<Document> response = getIndex().getRange(request); | ||
if (response.getResults().isEmpty()) { | ||
break; | ||
} | ||
for (Document doc : response) { | ||
docIds.add(doc.getId()); | ||
} | ||
getIndex().delete(docIds); | ||
} | ||
} catch (RuntimeException e) { | ||
LOG.log(Level.SEVERE, "Failed to delete documents", e); | ||
} | ||
// [END delete_documents] | ||
PrintWriter out = resp.getWriter(); | ||
out.println("Deleted documents."); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
appengine/search/src/main/java/com/example/appengine/search/DocumentServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* 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; | ||
|
||
// [START document_import] | ||
import com.google.appengine.api.search.Document; | ||
import com.google.appengine.api.search.Field; | ||
import com.google.appengine.api.users.User; | ||
import com.google.appengine.api.users.UserServiceFactory; | ||
// [END document_import] | ||
|
||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.util.Date; | ||
|
||
|
||
@SuppressWarnings("serial") | ||
public class DocumentServlet extends HttpServlet { | ||
|
||
public Document createDocument() { | ||
// [START create_document] | ||
User currentUser = UserServiceFactory.getUserService().getCurrentUser(); | ||
String userEmail = currentUser == null ? "" : currentUser.getEmail(); | ||
String userDomain = currentUser == null ? "" : currentUser.getAuthDomain(); | ||
String myDocId = "PA6-5000"; | ||
Document doc = Document.newBuilder() | ||
// Setting the document identifer is optional. | ||
// If omitted, the search service will create an identifier. | ||
.setId(myDocId) | ||
.addField(Field.newBuilder().setName("content").setText("the rain in spain")) | ||
.addField(Field.newBuilder().setName("email").setText(userEmail)) | ||
.addField(Field.newBuilder().setName("domain").setAtom(userDomain)) | ||
.addField(Field.newBuilder().setName("published").setDate(new Date())) | ||
.build(); | ||
// [END create_document] | ||
return doc; | ||
} | ||
|
||
@Override | ||
public void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
throws IOException { | ||
// Just to make sure the method works when deployed/dev_appserver. | ||
createDocument(); | ||
PrintWriter out = resp.getWriter(); | ||
Document document = Document.newBuilder() | ||
.addField(Field.newBuilder().setName("coverLetter").setText("CoverLetter")) | ||
.addField(Field.newBuilder().setName("resume").setHTML("<html></html>")) | ||
.addField(Field.newBuilder().setName("fullName").setAtom("Atom")) | ||
.addField(Field.newBuilder().setName("submissionDate").setDate(new Date())) | ||
.build(); | ||
// [START access_document] | ||
String coverLetter = document.getOnlyField("coverLetter").getText(); | ||
String resume = document.getOnlyField("resume").getHTML(); | ||
String fullName = document.getOnlyField("fullName").getAtom(); | ||
Date submissionDate = document.getOnlyField("submissionDate").getDate(); | ||
// [END access_document] | ||
out.println("coverLetter: " + coverLetter); | ||
out.println("resume: " + resume); | ||
out.println("fullName: " + fullName); | ||
out.println("submissionDate: " + submissionDate.toString()); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
appengine/search/src/main/java/com/example/appengine/search/IndexServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* 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; | ||
|
||
// @formatter:off | ||
import com.google.appengine.api.search.Document; | ||
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; | ||
|
||
// [START get_document_import] | ||
import com.google.appengine.api.search.GetRequest; | ||
import com.google.appengine.api.search.GetResponse; | ||
// [END get_document_import] | ||
// @formatter:on | ||
|
||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
|
||
|
||
/** | ||
* Code snippet for getting a document from Index. | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class IndexServlet extends HttpServlet { | ||
|
||
private static final String INDEX = "testIndex"; | ||
|
||
@Override | ||
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { | ||
PrintWriter out = resp.getWriter(); | ||
Document document = Document.newBuilder() | ||
.setId("AZ125") | ||
.addField(Field.newBuilder().setName("myField").setText("myValue")).build(); | ||
try { | ||
Utils.indexADocument(INDEX, document); | ||
} catch (InterruptedException e) { | ||
out.println("Interrupted"); | ||
return; | ||
} | ||
out.println("Indexed a new document."); | ||
// [START get_document] | ||
IndexSpec indexSpec = IndexSpec.newBuilder().setName(INDEX).build(); | ||
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec); | ||
|
||
// Fetch a single document by its doc_id | ||
Document doc = index.get("AZ125"); | ||
|
||
// Fetch a range of documents by their doc_ids | ||
GetResponse<Document> docs = index.getRange( | ||
GetRequest.newBuilder().setStartId("AZ125").setLimit(100).build()); | ||
// [END get_document] | ||
out.println("myField: " + docs.getResults().get(0).getOnlyField("myField").getText()); | ||
} | ||
} |
Oops, something went wrong.