-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
GeneralSearchSample.java
396 lines (344 loc) · 15.4 KB
/
GeneralSearchSample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Copyright 2018 Google LLC
*
* 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.google.samples;
import com.google.api.services.jobs.v3.CloudTalentSolution;
import com.google.api.services.jobs.v3.model.Company;
import com.google.api.services.jobs.v3.model.CompensationEntry;
import com.google.api.services.jobs.v3.model.CompensationFilter;
import com.google.api.services.jobs.v3.model.CompensationInfo;
import com.google.api.services.jobs.v3.model.CompensationRange;
import com.google.api.services.jobs.v3.model.Job;
import com.google.api.services.jobs.v3.model.JobQuery;
import com.google.api.services.jobs.v3.model.Money;
import com.google.api.services.jobs.v3.model.RequestMetadata;
import com.google.api.services.jobs.v3.model.SearchJobsRequest;
import com.google.api.services.jobs.v3.model.SearchJobsResponse;
import com.google.api.services.jobs.v3.model.TimestampRange;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
/**
* The samples in this file introduce how to do a general search, including:
*
* - Basic keyword search
*
* - Filter on categories
*
* - Filter on employment types
*
* - Filter on date range
*
* - Filter on language codes
*
* - Filter on company display names
*
* - Filter on compensations
*/
public final class GeneralSearchSample {
private static final String DEFAULT_PROJECT_ID =
"projects/" + System.getenv("GOOGLE_CLOUD_PROJECT");
private static CloudTalentSolution talentSolutionClient =
JobServiceQuickstart.getTalentSolutionClient();
// [START job_discovery_basic_keyword_search]
// [START basic_keyword_search]
/** Simple search jobs with keyword. */
public static void basicSearcJobs(String companyName, String query)
throws IOException, InterruptedException {
// Make sure to set the requestMetadata the same as the associated search request
RequestMetadata requestMetadata =
new RequestMetadata()
// Make sure to hash your userID
.setUserId("HashedUserId")
// Make sure to hash the sessionID
.setSessionId("HashedSessionID")
// Domain of the website where the search is conducted
.setDomain("www.google.com");
// Perform a search for analyst related jobs
JobQuery jobQuery = new JobQuery().setQuery(query);
if (companyName != null) {
jobQuery.setCompanyNames(Arrays.asList(companyName));
}
SearchJobsRequest searchJobsRequest =
new SearchJobsRequest()
.setRequestMetadata(requestMetadata)
.setJobQuery(jobQuery) // Set the actual search term as defined in the jobQurey
.setSearchMode("JOB_SEARCH"); // Set the search mode to a regular search
SearchJobsResponse searchJobsResponse =
talentSolutionClient
.projects()
.jobs()
.search(DEFAULT_PROJECT_ID, searchJobsRequest)
.execute();
Thread.sleep(1000);
System.out.printf("Simple search jobs results: %s\n", searchJobsResponse);
}
// [END basic_keyword_search]
// [END job_discovery_basic_keyword_search]
// [START job_discovery_category_filter_search]
// [START category_filter]
/** Search on category filter. */
public static void categoryFilterSearch(String companyName, List<String> categories)
throws IOException, InterruptedException {
// Make sure to set the requestMetadata the same as the associated search request
RequestMetadata requestMetadata =
new RequestMetadata()
// Make sure to hash your userID
.setUserId("HashedUserId")
// Make sure to hash the sessionID
.setSessionId("HashedSessionID")
// Domain of the website where the search is conducted
.setDomain("www.google.com");
JobQuery jobQuery = new JobQuery().setJobCategories(categories);
if (companyName != null) {
jobQuery.setCompanyNames(Arrays.asList(companyName));
}
SearchJobsRequest searchJobsRequest =
new SearchJobsRequest()
.setRequestMetadata(requestMetadata)
.setJobQuery(jobQuery) // Set the actual search term as defined in the jobQurey
.setSearchMode("JOB_SEARCH"); // Set the search mode to a regular search
SearchJobsResponse searchJobsResponse =
talentSolutionClient
.projects()
.jobs()
.search(DEFAULT_PROJECT_ID, searchJobsRequest)
.execute();
Thread.sleep(1000);
System.out.printf("Category search jobs results: %s\n", searchJobsResponse);
}
// [END category_filter]
// [END job_discovery_category_filter_search]
// [START job_discovery_employment_types_filter_search]
// [START employment_types_filter]
/** Search on employment types. */
public static void employmentTypesSearch(String companyName, List<String> employmentTypes)
throws IOException, InterruptedException {
// Make sure to set the requestMetadata the same as the associated search request
RequestMetadata requestMetadata =
new RequestMetadata()
// Make sure to hash your userID
.setUserId("HashedUserId")
// Make sure to hash the sessionID
.setSessionId("HashedSessionID")
// Domain of the website where the search is conducted
.setDomain("www.google.com");
JobQuery jobQuery = new JobQuery().setEmploymentTypes(employmentTypes);
if (companyName != null) {
jobQuery.setCompanyNames(Arrays.asList(companyName));
}
SearchJobsRequest searchJobsRequest =
new SearchJobsRequest()
.setRequestMetadata(requestMetadata)
.setJobQuery(jobQuery) // Set the actual search term as defined in the jobQurey
.setSearchMode("JOB_SEARCH"); // Set the search mode to a regular searchch
SearchJobsResponse searchJobsResponse =
talentSolutionClient
.projects()
.jobs()
.search(DEFAULT_PROJECT_ID, searchJobsRequest)
.execute();
Thread.sleep(1000);
System.out.printf("Employee type search jobs results: %s\n", searchJobsResponse);
}
// [END employment_types_filter]
// [END job_discovery_employment_types_filter_search]
// [START job_discovery_date_range_filter_search]
// [START date_range_filter]
/**
* Search on date range. In JSON format, the Timestamp type is encoded as a string in the [RFC
* 3339](https://www.ietf.org/rfc/rfc3339.txt) format. That is, the format is
* "{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z" e.g. "2017-01-15T01:30:15.01Z"
*/
public static void dateRangeSearch(String companyName, String startTime, String endTime)
throws IOException, InterruptedException {
// Make sure to set the requestMetadata the same as the associated search request
RequestMetadata requestMetadata =
new RequestMetadata()
// Make sure to hash your userID
.setUserId("HashedUserId")
// Make sure to hash the sessionID
.setSessionId("HashedSessionID")
// Domain of the website where the search is conducted
.setDomain("www.google.com");
TimestampRange timestampRange =
new TimestampRange().setStartTime(startTime).setEndTime(endTime);
JobQuery jobQuery = new JobQuery().setPublishTimeRange(timestampRange);
// JobQuery jobQuery = new JobQuery().setPublishTimeRange(dateRange);
if (companyName != null) {
jobQuery.setCompanyNames(Arrays.asList(companyName));
}
SearchJobsRequest searchJobsRequest =
new SearchJobsRequest()
.setRequestMetadata(requestMetadata)
.setJobQuery(jobQuery) // Set the actual search term as defined in the jobQurey
.setSearchMode("JOB_SEARCH"); // Set the search mode to a regular search
SearchJobsResponse searchJobsResponse =
talentSolutionClient
.projects()
.jobs()
.search(DEFAULT_PROJECT_ID, searchJobsRequest)
.execute();
Thread.sleep(1000);
System.out.printf("Search results on jobs with a date range: %s\n", searchJobsResponse);
}
// [END date_range_filter]
// [END job_discovery_date_range_filter_search]
// [START job_discovery_language_code_filter_search]
// [START language_code_filter]
/** Search on language codes. */
public static void languageCodeSearch(String companyName, List<String> languageCodes)
throws IOException, InterruptedException {
// Make sure to set the requestMetadata the same as the associated search request
RequestMetadata requestMetadata =
new RequestMetadata()
// Make sure to hash your userID
.setUserId("HashedUserId")
// Make sure to hash the sessionID
.setSessionId("HashedSessionID")
// Domain of the website where the search is conducted
.setDomain("www.google.com");
JobQuery jobQuery = new JobQuery().setLanguageCodes(languageCodes);
if (companyName != null) {
jobQuery.setCompanyNames(Arrays.asList(companyName));
}
SearchJobsRequest searchJobsRequest =
new SearchJobsRequest()
.setRequestMetadata(requestMetadata)
.setJobQuery(jobQuery) // Set the actual search term as defined in the jobQurey
.setSearchMode("JOB_SEARCH"); // Set the search mode to a regular search
SearchJobsResponse searchJobsResponse =
talentSolutionClient
.projects()
.jobs()
.search(DEFAULT_PROJECT_ID, searchJobsRequest)
.execute();
Thread.sleep(1000);
System.out.printf("Search results on jobs with a language code: %s\n", searchJobsResponse);
}
// [END language_code_filter]
// [END job_discovery_language_code_filter_search]
// [START job_discovery_company_display_name_search]
// [START company_display_name_filter]
/** Search on company display name. */
public static void companyDisplayNameSearch(String companyName, List<String> companyDisplayNames)
throws IOException, InterruptedException {
// Make sure to set the requestMetadata the same as the associated search request
RequestMetadata requestMetadata =
new RequestMetadata()
// Make sure to hash your userID
.setUserId("HashedUserId")
// Make sure to hash the sessionID
.setSessionId("HashedSessionID")
// Domain of the website where the search is conducted
.setDomain("www.google.com");
JobQuery jobQuery = new JobQuery().setCompanyDisplayNames(companyDisplayNames);
if (companyName != null) {
jobQuery.setCompanyNames(Arrays.asList(companyName));
}
SearchJobsRequest searchJobsRequest =
new SearchJobsRequest()
.setRequestMetadata(requestMetadata)
.setJobQuery(jobQuery) // Set the actual search term as defined in the jobQurey
.setSearchMode("JOB_SEARCH"); // Set the search mode to a regular search
SearchJobsResponse searchJobsResponse =
talentSolutionClient
.projects()
.jobs()
.search(DEFAULT_PROJECT_ID, searchJobsRequest)
.execute();
Thread.sleep(1000);
System.out.printf("Search results by display name of company: %s\n", searchJobsResponse);
}
// [END company_display_name_filter]
// [END job_discovery_company_display_name_search]
// [START job_discovery_compensation_search]
// [START compensation_filter]
/** Search on compensation. */
public static void compensationSearch(String companyName)
throws IOException, InterruptedException {
// Make sure to set the requestMetadata the same as the associated search request
RequestMetadata requestMetadata =
new RequestMetadata()
// Make sure to hash your userID
.setUserId("HashedUserId")
// Make sure to hash the sessionID
.setSessionId("HashedSessionID")
// Domain of the website where the search is conducted
.setDomain("www.google.com");
// Search jobs that pay between 10.50 and 15 USD per hour
JobQuery jobQuery =
new JobQuery()
.setCompensationFilter(
new CompensationFilter()
.setType("UNIT_AND_AMOUNT")
.setUnits(Arrays.asList("HOURLY"))
.setRange(
new CompensationRange()
.setMaxCompensation(new Money().setCurrencyCode("USD").setUnits(15L))
.setMinCompensation(
new Money()
.setCurrencyCode("USD")
.setUnits(10L)
.setNanos(500000000))));
if (companyName != null) {
jobQuery.setCompanyNames(Arrays.asList(companyName));
}
SearchJobsRequest searchJobsRequest =
new SearchJobsRequest()
.setRequestMetadata(requestMetadata)
.setJobQuery(jobQuery) // Set the actual search term as defined in the jobQurey
.setSearchMode("JOB_SEARCH"); // Set the search mode to a regular search
SearchJobsResponse searchJobsResponse =
talentSolutionClient
.projects()
.jobs()
.search(DEFAULT_PROJECT_ID, searchJobsRequest)
.execute();
Thread.sleep(1000);
System.out.printf("Search results by compensation: %s\n", searchJobsResponse);
}
// [END compensation_filter]
// [END job_discovery_compensation_search]
public static void main(String... args) throws Exception {
Company companyToBeCreated = BasicCompanySample.generateCompany().setDisplayName("Google");
String companyName = BasicCompanySample.createCompany(companyToBeCreated).getName();
Job jobToBeCreated =
BasicJobSample.generateJobWithRequiredFields(companyName)
.setTitle("Systems Administrator")
.setEmploymentTypes(Arrays.asList("FULL_TIME"))
.setLanguageCode("en-US")
.setCompensationInfo(
new CompensationInfo()
.setEntries(
Arrays.asList(
new CompensationEntry()
.setType("BASE")
.setUnit("HOURLY")
.setAmount(new Money().setCurrencyCode("USD").setUnits(12L)))));
final String jobName = BasicJobSample.createJob(jobToBeCreated).getName();
// Wait several seconds for post processing
Thread.sleep(10000);
basicSearcJobs(companyName, "Systems Administrator");
categoryFilterSearch(companyName, Arrays.asList("COMPUTER_AND_IT"));
dateRangeSearch(companyName, "1980-01-15T01:30:15.01Z", "2099-01-15T01:30:15.01Z");
employmentTypesSearch(companyName, Arrays.asList("FULL_TIME", "CONTRACTOR", "PER_DIEM"));
companyDisplayNameSearch(companyName, Arrays.asList("Google"));
compensationSearch(companyName);
languageCodeSearch(companyName, Arrays.asList("pt-BR", "en-US"));
BasicJobSample.deleteJob(jobName);
BasicCompanySample.deleteCompany(companyName);
}
}