|
| 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] |
0 commit comments