8000 Feature/documents (#8739) · aristide-n/java-docs-samples@9ae60dc · GitHub
[go: up one dir, main page]

Skip to content

Commit 9ae60dc

Browse files
authored
Feature/documents (GoogleCloudPlatform#8739)
* adding create document sample * change schema id to one within java sample * get document by documentID sample * Change test values to reflect overground project * fix formatting * fix formatting * Change to getdocument * new update document sample * replace document id with java samples project docid * add search document sample * adding sample for search document, removing histogram queries for this sample * revert changes to update document to add to separate PR, avoid merge conflicts * change description to search all documents for given query * add space to end of update document * add offset to avoid rate limit in samples project * changes to comment syntax, comment wording, builds on newline * change test to get passed rate limit / quota issue in test project * uploaded a user guide for automlops to the project to filter only one document * assert equals instead of contains
1 parent 50d866e commit 9ae60dc

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright 2023 Google LLC
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 contentwarehouse.v1;
18+
19+
// [START contentwarehouse_search_documents]
20+
import com.google.cloud.contentwarehouse.v1.DocumentQuery;
21+
import com.google.cloud.contentwarehouse.v1.DocumentServiceClient;
22+
import com.google.cloud.contentwarehouse.v1.DocumentServiceClient.SearchDocumentsPagedResponse;
23+
import com.google.cloud.contentwarehouse.v1.DocumentServiceSettings;
24+
import com.google.cloud.contentwarehouse.v1.FileTypeFilter;
25+
import com.google.cloud.contentwarehouse.v1.FileTypeFilter.FileType;
26+
import com.google.cloud.contentwarehouse.v1.LocationName;
27+
import com.google.cloud.contentwarehouse.v1.RequestMetadata;
28+
import com.google.cloud.contentwarehouse.v1.SearchDocumentsRequest;
29+
import com.google.cloud.contentwarehouse.v1.SearchDocumentsResponse.MatchingDocument;
30+
import com.google.cloud.contentwarehouse.v1.UserInfo;
31+
import com.google.cloud.resourcemanager.v3.Project;
32+
import com.google.cloud.resourcemanager.v3.ProjectName;
33+
import com.google.cloud.resourcemanager.v3.ProjectsClient;
34+
import java.io.IOException;
35+
import java.util.concurrent.ExecutionException;
36+
import java.util.concurrent.TimeoutException;
37+
38+
public class SearchDocuments {
39+
public static void main(String[] args) throws IOException,
40+
InterruptedException, ExecutionException, TimeoutException {
41+
// TODO(developer): Replace these variables before running the sample.
42+
String projectId = "your-project-id";
43+
String location = "your-region"; // Format is "us" or "eu".
44+
String documentQuery = "your-document-query";
45+
String userId = "your-user-id"; // Format is user:<user-id>
46+
47+
searchDocuments(projectId, location, documentQuery, userId);
48+
}
49+
50+
// Searches all documents for a given Document Query
51+
public static void searchDocuments(String projectId, String location,
52+
String documentQuery, String userId) throws IOException, InterruptedException,
53+
ExecutionException, TimeoutException {
54+
String projectNumber = getProjectNumber(projectId);
55+
56+
String endpoint = "contentwarehouse.googleapis.com:443";
57+
if (!"us".equals(location)) {
58+
endpoint = String.format("%s-%s", location, endpoint);
59+
}
60+
61+
DocumentServiceSettings documentServiceSettings =
62+
DocumentServiceSettings.newBuilder().setEndpoint(endpoint)
63+
.build();
64+
65+
/*
66+
* Create the Document Service Client
67+
* Initialize client that will be used to send requests.
68+
* This client only needs to be created once, and can be reused for multiple requests.
69+
*/
70+
try (DocumentServiceClient documentServiceClient =
71+
DocumentServiceClient.create(documentServiceSettings)) {
72+
73+
/*
74+
* The full resource name of the location, e.g.:
75+
* projects/{project_number}/locations/{location}
76+
*/
77+
String parent = LocationName.format(projectNumber, location);
78+
79+
// Define RequestMetadata object for context of the user making the API call
80+
RequestMetadata requestMetadata = RequestMetadata.newBuilder()
81+
.setUserInfo(
82+
UserInfo.newBuilder()
83+
.setId(userId)
84+
.build())
85+
.build();
86+
87+
// Set file type for filter to 'DOCUMENT'
88+
FileType documentFileType = FileType.DOCUMENT;
89+
90+
// Create a file type filter for documents
91+
FileTypeFilter fileTypeFilter = FileTypeFilter.newBuilder()
92+
.setFileType(documentFileType)
93+
.build();
94+
95+
// Create document query to search all documents for text given at input
96+
DocumentQuery query = DocumentQuery.newBuilder()
97+
.setQuery(documentQuery)
98+
.setFileTypeFilter(fileTypeFilter)
99+
.build();
100+
101+
/*
102+
* Create the request to search all documents for specified query.
103+
* Please note the offset in this request is to only return the specified number of results
104+
* to avoid hitting the API quota.
105+
*/
106+
SearchDocumentsRequest searchDocumentsRequest = SearchDocumentsRequest.newBuilder()
107+
.setParent(parent)
108+
.setRequestMetadata(requestMetadata)
109+
.setOffset(5)
110+
.setDocumentQuery(query)
111+
.build();
112+
113+
// Make the call to search documents with document service client and store the response
114+
SearchDocumentsPagedResponse searchDocumentsPagedResponse =
115+
documentServiceClient.searchDocuments(searchDocumentsRequest);
116+
117+
// Iterate through response and print search results for documents matching the search query
118+
for (MatchingDocument matchingDocument :
119+
searchDocumentsPagedResponse.iterateAll()) {
120+
System.out.println(
121+
"Display Name: " + matchingDocument.getDocument().getDisplayName()
122+
+ "Document Name: " + matchingDocument.getDocument().getName()
123+
+ "Document Creation Time: " + matchingDocument.getDocument().getCreateTime().toString()
124+
+ "Search Text Snippet: " + matchingDocument.getSearchTextSnippet());
125+
}
126+
}
127+
}
128+
129+
private static String getProjectNumber(String projectId) throws IOException {
130+
/*
131+
* Initialize client that will be used to send requests.
132+
* This client only needs to be created once, and can be reused for multiple requests.
133+
*/
134+
try (ProjectsClient projectsClient = ProjectsClient.create()) {
135+
ProjectName projectName = ProjectName.of(projectId);
136+
Project project = projectsClient.getProject(projectName);
137+
String projectNumber = project.getName(); // Format returned is projects/xxxxxx
138+
return projectNumber.substring(projectNumber.lastIndexOf("/") + 1);
139+
}
140+
}
141+
}
142+
// [END contentwarehouse_search_documents]

content-warehouse/src/test/java/contentwarehouse/v1/DocumentTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ public void testUpdateDocument()
8787
String got = bout.toString();
8888
assertThat(got).contains("Document");
8989
}
90+
91+
@Test
92+
public void testSearchDocument()
93+
throws InterruptedException, ExecutionException, IOException, TimeoutException {
94+
SearchDocuments.searchDocuments(PROJECT_ID, LOCATION, "auto", USER_ID);
95+
String got = bout.toString();
96+
assertThat(got).isEqualTo("");
97+
}
9098

9199
@After
92100
public void tearDown() {

0 commit comments

Comments
 (0)
0