-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathLocationSearchSample.java
250 lines (228 loc) · 10.1 KB
/
LocationSearchSample.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
/*
* 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.Job;
import com.google.api.services.jobs.v3.model.JobQuery;
import com.google.api.services.jobs.v3.model.LocationFilter;
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 java.io.IOException;
import java.util.Arrays;
/**
* The samples in this file introduce how to do a search with location filter, including:
*
* - Basic search with location filter
*
* - Keyword search with location filter
*
* - Location filter on city level
*
* - Broadening search with location filter
*
* - Location filter of multiple locations
*/
public final class LocationSearchSample {
private static final String DEFAULT_PROJECT_ID =
"projects/" + System.getenv("GOOGLE_CLOUD_PROJECT");
private static CloudTalentSolution talentSolutionClient =
JobServiceQuickstart.getTalentSolutionClient();
// [START job_basic_location_search]
/** Basic location Search */
public static void basicLocationSearch(String companyName, String location, double distance)
throws IOException, InterruptedException {
// Make sure to set the requestMetadata the same as the associated search request
RequestMetadata requestMetadata =
new RequestMetadata()
// Make sure to hash the userID
.setUserId("HashedUserId")
// Make sure to hash the sessionID
.setSessionId("HashedSessionID")
// Domain of the website where the search is conducted
.setDomain("www.google.com");
LocationFilter locationFilter =
new LocationFilter().setAddress(location).setDistanceInMiles(distance);
JobQuery jobQuery = new JobQuery().setLocationFilters(Arrays.asList(locationFilter));
if (companyName != null) {
jobQuery.setCompanyNames(Arrays.asList(companyName));
}
SearchJobsRequest request =
new SearchJobsRequest()
.setRequestMetadata(requestMetadata)
.setJobQuery(jobQuery)
.setSearchMode("JOB_SEARCH");
SearchJobsResponse response =
talentSolutionClient.projects().jobs().search(DEFAULT_PROJECT_ID, request).execute();
Thread.sleep(1000);
System.out.printf("Basic location search results: %s", response);
}
// [END job_basic_location_search]
// [START job_keyword_location_search]
/** Keyword location Search */
public static void keywordLocationSearch(
String companyName, String location, double distance, String keyword)
throws IOException, InterruptedException {
// Make sure to set the requestMetadata the same as the associated search request
RequestMetadata requestMetadata =
new RequestMetadata()
// Make sure to hash the userID
.setUserId("HashedUserId")
// Make sure to hash the sessionID
.setSessionId("HashedSessionID")
// Domain of the website where the search is conducted
.setDomain("www.google.com");
LocationFilter locationFilter =
new LocationFilter().setAddress(location).setDistanceInMiles(distance);
JobQuery jobQuery =
new JobQuery().setQuery(keyword).setLocationFilters(Arrays.asList(locationFilter));
if (companyName != null) {
jobQuery.setCompanyNames(Arrays.asList(companyName));
}
SearchJobsRequest request =
new SearchJobsRequest()
.setRequestMetadata(requestMetadata)
.setJobQuery(jobQuery)
.setSearchMode("JOB_SEARCH");
SearchJobsResponse response =
talentSolutionClient.projects().jobs().search(DEFAULT_PROJECT_ID, request).execute();
Thread.sleep(1000);
System.out.printf("Keyword location search results: %s", response);
}
// [END job_keyword_location_search]
// [START job_city_location_search]
/** City location Search */
public static void cityLocationSearch(String companyName, String location)
throws IOException, InterruptedException {
// Make sure to set the requestMetadata the same as the associated search request
RequestMetadata requestMetadata =
new RequestMetadata()
// Make sure to hash the userID
.setUserId("HashedUserId")
// Make sure to hash the sessionID
.setSessionId("HashedSessionID")
// Domain of the website where the search is conducted
.setDomain("www.google.com");
LocationFilter locationFilter = new LocationFilter().setAddress(location);
JobQuery jobQuery = new JobQuery().setLocationFilters(Arrays.asList(locationFilter));
if (companyName != null) {
jobQuery.setCompanyNames(Arrays.asList(companyName));
}
SearchJobsRequest request =
new SearchJobsRequest()
.setRequestMetadata(requestMetadata)
.setJobQuery(jobQuery)
.setSearchMode("JOB_SEARCH");
SearchJobsResponse response =
talentSolutionClient.projects().jobs().search(DEFAULT_PROJECT_ID, request).execute();
Thread.sleep(1000);
System.out.printf("City locations search results: %s", response);
}
// [END job_city_location_search]
// [START job_multi_locations_search]
/** Multiple locations Search */
public static void multiLocationsSearch(
String companyName, String location1, double distance1, String location2)
throws IOException, InterruptedException {
// Make sure to set the requestMetadata the same as the associated search request
RequestMetadata requestMetadata =
new RequestMetadata()
// Make sure to hash the 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()
.setLocationFilters(
Arrays.asList(
new LocationFilter().setAddress(location1).setDistanceInMiles(distance1),
new LocationFilter().setAddress(location2)));
if (companyName != null) {
jobQuery.setCompanyNames(Arrays.asList(companyName));
}
SearchJobsRequest request =
new SearchJobsRequest()
.setRequestMetadata(requestMetadata)
.setJobQuery(jobQuery)
.setSearchMode("JOB_SEARCH");
SearchJobsResponse response =
talentSolutionClient.projects().jobs().search(DEFAULT_PROJECT_ID, request).execute();
Thread.sleep(1000);
System.out.printf("Multiple locations search results: %s", response);
}
// [END job_multi_locations_search]
// [START job_broadening_location_search]
/** Broadening location Search */
public static void broadeningLocationsSearch(String companyName, String location)
throws IOException, InterruptedException {
// Make sure to set the requestMetadata the same as the associated search request
RequestMetadata requestMetadata =
new RequestMetadata()
// Make sure to hash the 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().setLocationFilters(Arrays.asList(new LocationFilter().setAddress(location)));
if (companyName != null) {
jobQuery.setCompanyNames(Arrays.asList(companyName));
}
SearchJobsRequest request =
new SearchJobsRequest()
.setRequestMetadata(requestMetadata)
.setJobQuery(jobQuery)
.setEnableBroadening(true)
.setSearchMode("JOB_SEARCH");
SearchJobsResponse response =
talentSolutionClient.projects().jobs().search(DEFAULT_PROJECT_ID, request).execute();
Thread.sleep(1000);
System.out.printf("Broadening locations search results: %s", response);
}
// [END job_broadening_location_search]
public static void main(String... args) throws Exception {
String location = args.length >= 1 ? args[0] : "Mountain View, CA";
double distance = args.length >= 2 ? Double.parseDouble(args[1]) : 0.5;
String keyword = args.length >= 3 ? args[2] : "Software Engineer";
String location2 = args.length >= 4 ? args[3] : "Sunnyvale, CA";
Company companyToBeCreated = BasicCompanySample.generateCompany();
String companyName = BasicCompanySample.createCompany(companyToBeCreated).getName();
Job jobToBeCreated =
BasicJobSample.generateJobWithRequiredFields(companyName)
.setAddresses(Arrays.asList(location))
.setTitle(keyword);
final String jobName = BasicJobSample.createJob(jobToBeCreated).getName();
Job jobToBeCreated2 =
BasicJobSample.generateJobWithRequiredFields(companyName)
.setAddresses(Arrays.asList(location2))
.setTitle(keyword);
final String jobName2 = BasicJobSample.createJob(jobToBeCreated2).getName();
// Wait several seconds for post processing
Thread.sleep(10000);
basicLocationSearch(companyName, location, distance);
cityLocationSearch(companyName, location);
broadeningLocationsSearch(companyName, location);
keywordLocationSearch(companyName, location, distance, keyword);
multiLocationsSearch(companyName, location, distance, location2);
BasicJobSample.deleteJob(jobName);
BasicJobSample.deleteJob(jobName2);
BasicCompanySample.deleteCompany(companyName);
}
}