Skip to content

Commit aaedf38

Browse files
author
Takashi Matsuo
committed
Adding Search API sample code.
Change-Id: Ib8644f3b56d1602c3aa79f56ade56c9b9e7f6df1
1 parent 1b1d17a commit aaedf38

File tree

11 files changed

+592
-1
lines changed

11 files changed

+592
-1
lines changed

appengine/search/.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Eclipse files
2+
.project
3+
.classpath
4+
.settings
5+
6+
# Target folders
7+
target/

appengine/search/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Google App Engine Standard Environment Search API Sample
2+
3+
This sample demonstrates how to use App Engine Search API.
4+
5+
See the [Google App Engine Search API documentation][search-api-docs] for more
6+
detailed instructions.
7+
8+
[search-api-docs]: https://cloud.google.com/appengine/docs/java/search/
9+
10+
## Setup
11+
1. Update the `<application>` tag in `src/main/webapp/WEB-INF/appengine-web.xml`
12+
with your project name.
13+
1. Update the `<version>` tag in `src/main/webapp/WEB-INF/appengine-web.xml`
14+
with your version name.
15+
16+
## Running locally
17+
$ mvn appengine:devserver
18+
19+
## Deploying
20+
$ mvn appengine:update

appengine/search/pom.xml

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<!--
2+
Copyright 2015 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+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<packaging>war</packaging>
19+
<version>1.0-SNAPSHOT</version>
20+
<groupId>com.example.appengine</groupId>
21+
<artifactId>appengine-search</artifactId>
22+
<parent>
23+
<groupId>com.google.cloud</groupId>
24+
<artifactId>doc-samples</artifactId>
25+
<version>1.0.0</version>
26+
<relativePath>../..</relativePath>
27+
</parent>
28+
<dependencies>
29+
<dependency>
30+
<groupId>com.google.appengine</groupId>
31+
<artifactId>appengine-api-1.0-sdk</artifactId>
32+
<version>${appengine.sdk.version}</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>javax.servlet</groupId>
36+
<artifactId>servlet-api</artifactId>
37+
<version>2.5</version>
38+
<type>jar</type>
39+
<scope>provided</scope>
40+
</dependency>
41+
42+
<!-- Test Dependencies -->
43+
<dependency>
44+
<groupId>junit</groupId>
45+
<artifactId>junit</artifactId>
46+
<version>4.10</version>
47+
<scope>test</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.mockito</groupId>
51+
<artifactId>mockito-all</artifactId>
52+
<version>1.10.19</version>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.google.appengine</groupId>
57+
<artifactId>appengine-testing</artifactId>
58+
<version>${appengine.sdk.version}</version>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>com.google.appengine</groupId>
63+
<artifactId>appengine-api-stubs</artifactId>
64+
<version>${appengine.sdk.version}</version>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>com.google.appengine</groupId>
69+
<artifactId>appengine-tools-sdk</artifactId>
70+
<version>${appengine.sdk.version}</version>
71+
<scope>test</scope>
72+
</dependency>
73+
</dependencies>
74+
<build>
75+
<!-- for hot reload of the web application -->
76+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
77+
<plugins>
78+
<plugin>
79+
<groupId>org.apache.maven.plugins</groupId>
80+
<version>3.3</version>
81+
<artifactId>maven-compiler-plugin</artifactId>
82+
<configuration>
83+
<source>1.7</source>
84+
<target>1.7</target>
85+
</configuration>
86+
</plugin>
87+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
88+
<plugin>
89+
<groupId>com.google.appengine</groupId>
90+
<artifactId>appengine-maven-plugin</artifactId>
91+
<version>${appengine.sdk.version}</version>
92+
</plugin>
93+
</plugins>
94+
</build>
95+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
// @formatter:off
20+
// [START delete_import]
21+
import com.google.appengine.api.search.Document;
22+
import com.google.appengine.api.search.GetRequest;
23+
import com.google.appengine.api.search.GetResponse;
24+
// [END delete_import]
25+
26+
import com.google.appengine.api.search.Field;
27+
import com.google.appengine.api.search.Index;
28+
import com.google.appengine.api.search.IndexSpec;
29+
import com.google.appengine.api.search.SearchServiceFactory;
30+
// @formatter:on
31+
32+
import javax.servlet.http.HttpServlet;
33+
import javax.servlet.http.HttpServletRequest;
34+
import javax.servlet.http.HttpServletResponse;
35+
import java.io.IOException;
36+
import java.io.PrintWriter;
37+
import java.util.ArrayList;
38+
import java.util.List;
39+
import java.util.logging.Level;
40+
import java.util.logging.Logger;
41+
42+
/**
43+
* Code snippet for deleting documents from an Index.
44+
*/
45+
@SuppressWarnings("serial")
46+
public class DeleteServlet extends HttpServlet {
47+
private static final Logger LOG = Logger.getLogger(DeleteServlet.class.getSimpleName());
48+
49+
private static final String SEARCH_INDEX = "searchIndexForDelete";
50+
51+
private Index getIndex() {
52+
IndexSpec indexSpec = IndexSpec.newBuilder().setName(SEARCH_INDEX).build();
53+
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
54+
return index;
55+
}
56+
57+
@Override
58+
public void doGet(HttpServletRequest req, HttpServletResponse resp)
59+
throws IOException {
60+
// Put one document to avoid an error
61+
Document document = Document.newBuilder()
62+
.addField(Field.newBuilder().setName("f").setText("v"))
63+
.build();
64+
try {
65+
Utils.indexADocument(SEARCH_INDEX, document);
66+
} catch (InterruptedException e) {
67+
// ignore
68+
}
69+
// [START delete_documents]
70+
try {
71+
// looping because getRange by default returns up to 100 documents at a time
72+
while (true) {
73+
List<String> docIds = new ArrayList<>();
74+
// Return a set of doc_ids.
75+
GetRequest request = GetRequest.newBuilder().setReturningIdsOnly(true).build();
76+
GetResponse<Document> response = getIndex().getRange(request);
77+
if (response.getResults().isEmpty()) {
78+
break;
79+
}
80+
for (Document doc : response) {
81+
docIds.add(doc.getId());
82+
}
83+
getIndex().delete(docIds);
84+
}
85+
} catch (RuntimeException e) {
86+
LOG.log(Level.SEVERE, "Failed to delete documents", e);
87+
}
88+
// [END delete_documents]
89+
PrintWriter out = resp.getWriter();
90+
out.println("Deleted documents.");
91+
}
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
// [START document_import]
20+
import com.google.appengine.api.search.Document;
21+
import com.google.appengine.api.search.Field;
22+
import com.google.appengine.api.users.User;
23+
import com.google.appengine.api.users.UserServiceFactory;
24+
// [END document_import]
25+
26+
import javax.servlet.http.HttpServlet;
27+
import javax.servlet.http.HttpServletRequest;
28+
import javax.servlet.http.HttpServletResponse;
29+
import java.io.IOException;
30+
import java.io.PrintWriter;
31+
import java.util.Date;
32+
33+
34+
@SuppressWarnings("serial")
35+
public class DocumentServlet extends HttpServlet {
36+
37+
public Document createDocument() {
38+
// [START create_document]
39+
User currentUser = UserServiceFactory.getUserService().getCurrentUser();
40+
String userEmail = currentUser == null ? "" : currentUser.getEmail();
41+
String userDomain = currentUser == null ? "" : currentUser.getAuthDomain();
42+
String myDocId = "PA6-5000";
43+
Document doc = Document.newBuilder()
44+
// Setting the document identifer is optional.
45+
// If omitted, the search service will create an identifier.
46+
.setId(myDocId)
47+
.addField(Field.newBuilder().setName("content").setText("the rain in spain"))
48+
.addField(Field.newBuilder().setName("email").setText(userEmail))
49+
.addField(Field.newBuilder().setName("domain").setAtom(userDomain))
50+
.addField(Field.newBuilder().setName("published").setDate(new Date()))
51+
.build();
52+
// [END create_document]
53+
return doc;
54+
}
55+
56+
@Override
57+
public void doGet(HttpServletRequest req, HttpServletResponse resp)
58+
throws IOException {
59+
// Just to make sure the method works when deployed/dev_appserver.
60+
createDocument();
61+
PrintWriter out = resp.getWriter();
62+
Document document = Document.newBuilder()
63+
.addField(Field.newBuilder().setName("coverLetter").setText("CoverLetter"))
64+
.addField(Field.newBuilder().setName("resume").setHTML("<html></html>"))
65+
.addField(Field.newBuilder().setName("fullName").setAtom("Atom"))
66+
.addField(Field.newBuilder().setName("submissionDate").setDate(new Date()))
67+
.build();
68+
// [START access_document]
69+
String coverLetter = document.getOnlyField("coverLetter").getText();
70+
String resume = document.getOnlyField("resume").getHTML();
71+
String fullName = document.getOnlyField("fullName").getAtom();
72+
Date submissionDate = document.getOnlyField("submissionDate").getDate();
73+
// [END access_document]
74+
out.println("coverLetter: " + coverLetter);
75+
out.println("resume: " + resume);
76+
out.println("fullName: " + fullName);
77+
out.println("submissionDate: " + submissionDate.toString());
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
// @formatter:off
20+
import com.google.appengine.api.search.Document;
21+
import com.google.appengine.api.search.Field;
22+
import com.google.appengine.api.search.Index;
23+
import com.google.appengine.api.search.IndexSpec;
24+
import com.google.appengine.api.search.SearchServiceFactory;
25+
26+
// [START get_document_import]
27+
import com.google.appengine.api.search.GetRequest;
28+
import com.google.appengine.api.search.GetResponse;
29+
// [END get_document_import]
30+
// @formatter:on
31+
32+
import javax.servlet.http.HttpServlet;
33+
import javax.servlet.http.HttpServletRequest;
34+
import javax.servlet.http.HttpServletResponse;
35+
import java.io.IOException;
36+
import java.io.PrintWriter;
37+
38+
39+
/**
40+
* Code snippet for getting a document from Index.
41+
*/
42+
@SuppressWarnings("serial")
43+
public class IndexServlet extends HttpServlet {
44+
45+
private static final String INDEX = "testIndex";
46+
47+
@Override
48+
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
49+
PrintWriter out = resp.getWriter();
50+
Document document = Document.newBuilder()
51+
.setId("AZ125")
52+
.addField(Field.newBuilder().setName("myField").setText("myValue")).build();
53+
try {
54+
Utils.indexADocument(INDEX, document);
55+
} catch (InterruptedException e) {
56+
out.println("Interrupted");
57+
return;
58+
}
59+
out.println("Indexed a new document.");
60+
// [START get_document]
61+
IndexSpec indexSpec = IndexSpec.newBuilder().setName(INDEX).build();
62+
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
63+
64+
// Fetch a single document by its doc_id
65+
Document doc = index.get("AZ125");
66+
67+
// Fetch a range of documents by their doc_ids
68+
GetResponse<Document> docs = index.getRange(
69+
GetRequest.newBuilder().setStartId("AZ125").setLimit(100).build());
70+
// [END get_document]
71+
out.println("myField: " + docs.getResults().get(0).getOnlyField("myField").getText());
72+
}
73+
}

0 commit comments

Comments
 (0)