Skip to content

Commit 62438ad

Browse files
samples: add Job Search v4 samples (#275)
* Sample code for Jobs - V4 Sample code for Jobs v4 APIs. * feat: Add Job Search v4 API * Updated as per comment * No more required. samples/snippets/pom.xml will take care of specific version * Migrated to parent folder Maintaing v4 as the defacto version for now. Later it can be moved to version specific package if required. * test: configure extra cts environment variables * fixed listjobs test fixed listjobs test * fixed lint. Co-authored-by: Jeff Ching <chingor@google.com>
1 parent 63bd2e2 commit 62438ad

35 files changed

Lines changed: 2216 additions & 0 deletions
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2020 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 com.example.jobs;
18+
19+
// [START job_search_commute_search]
20+
21+
import com.google.cloud.talent.v4.CommuteFilter;
22+
import com.google.cloud.talent.v4.CommuteMethod;
23+
import com.google.cloud.talent.v4.Job;
24+
import com.google.cloud.talent.v4.JobQuery;
25+
import com.google.cloud.talent.v4.JobServiceClient;
26+
import com.google.cloud.talent.v4.RequestMetadata;
27+
import com.google.cloud.talent.v4.SearchJobsRequest;
28+
import com.google.cloud.talent.v4.SearchJobsResponse;
29+
import com.google.cloud.talent.v4.TenantName;
30+
import com.google.protobuf.Duration;
31+
import com.google.type.LatLng;
32+
import java.io.IOException;
33+
34+
public class CommuteSearchJobs {
35+
36+
public static void searchJobs() throws IOException {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String projectId = "your-project-id";
39+
String tenantId = "your-tenant-id";
40+
searchJobs(projectId, tenantId);
41+
}
42+
43+
// Search Jobs with histogram queries.
44+
public static void searchJobs(String projectId, String tenantId) throws IOException {
45+
// Initialize client that will be used to send requests. This client only needs to be created
46+
// once, and can be reused for multiple requests. After completing all of your requests, call
47+
// the "close" method on the client to safely clean up any remaining background resources.
48+
try (JobServiceClient jobServiceClient = JobServiceClient.create()) {
49+
TenantName parent = TenantName.of(projectId, tenantId);
50+
String domain = "www.example.com";
51+
String sessionId = "Hashed session identifier";
52+
String userId = "Hashed user identifier";
53+
RequestMetadata requestMetadata =
54+
RequestMetadata.newBuilder()
55+
.setDomain(domain)
56+
.setSessionId(sessionId)
57+
.setUserId(userId)
58+
.build();
59+
60+
CommuteMethod commuteMethod = CommuteMethod.DRIVING;
61+
long seconds = 3600L;
62+
Duration travelDuration = Duration.newBuilder().setSeconds(seconds).build();
63+
64+
double latitude = 37.422408;
65+
double longitude = -122.084068;
66+
LatLng startCoordinates =
67+
LatLng.newBuilder().setLatitude(latitude).setLongitude(longitude).build();
68+
69+
CommuteFilter commuteFilter =
70+
CommuteFilter.newBuilder()
71+
.setCommuteMethod(commuteMethod)
72+
.setTravelDuration(travelDuration)
73+
.setStartCoordinates(startCoordinates)
74+
.build();
75+
76+
JobQuery jobQuery = JobQuery.newBuilder().setCommuteFilter(commuteFilter).build();
77+
SearchJobsRequest request =
78+
SearchJobsRequest.newBuilder()
79+
.setParent(parent.toString())
80+
.setRequestMetadata(requestMetadata)
81+
.setJobQuery(jobQuery)
82+
.build();
83+
84+
for (SearchJobsResponse.MatchingJob responseItem :
85+
jobServiceClient.searchJobs(request).getMatchingJobsList()) {
86+
System.out.format("Job summary: %s%n", responseItem.getJobSummary());
87+
System.out.format("Job title snippet: %s%n", responseItem.getJobTitleSnippet());
88+
Job job = responseItem.getJob();
89+
System.out.format("Job name: %s%n", job.getName());
90+
System.out.format("Job title: %s%n", job.getTitle());
91+
}
92+
}
93+
}
94+
}
95+
// [END job_search_commute_search]
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2020 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 com.example.jobs;
18+
19+
// [START job_search_custom_ranking_search]
20+
21+
import com.google.cloud.talent.v4.Job;
22+
import com.google.cloud.talent.v4.JobServiceClient;
23+
import com.google.cloud.talent.v4.RequestMetadata;
24+
import com.google.cloud.talent.v4.SearchJobsRequest;
25+
import com.google.cloud.talent.v4.SearchJobsResponse;
26+
import com.google.cloud.talent.v4.TenantName;
27+
import java.io.IOException;
28+
29+
public class CustomRankingSearchJobs {
30+
31+
public static void searchCustomRankingJobs() throws IOException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "your-project-id";
34+
String tenantId = "your-tenant-id";
35+
searchCustomRankingJobs(projectId, tenantId);
36+
}
37+
38+
// Search Jobs using custom rankings.
39+
public static void searchCustomRankingJobs(String projectId, String tenantId) throws IOException {
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests. After completing all of your requests, call
42+
// the "close" method on the client to safely clean up any remaining background resources.
43+
try (JobServiceClient jobServiceClient = JobServiceClient.create()) {
44+
TenantName parent = TenantName.of(projectId, tenantId);
45+
String domain = "www.example.com";
46+
String sessionId = "Hashed session identifier";
47+
String userId = "Hashed user identifier";
48+
RequestMetadata requestMetadata =
49+
RequestMetadata.newBuilder()
50+
.setDomain(domain)
51+
.setSessionId(sessionId)
52+
.setUserId(userId)
53+
.build();
54+
SearchJobsRequest.CustomRankingInfo.ImportanceLevel importanceLevel =
55+
SearchJobsRequest.CustomRankingInfo.ImportanceLevel.EXTREME;
56+
String rankingExpression = "(someFieldLong + 25) * 0.25";
57+
SearchJobsRequest.CustomRankingInfo customRankingInfo =
58+
SearchJobsRequest.CustomRankingInfo.newBuilder()
59+
.setImportanceLevel(importanceLevel)
60+
.setRankingExpression(rankingExpression)
61+
.build();
62+
String orderBy = "custom_ranking desc";
63+
SearchJobsRequest request =
64+
SearchJobsRequest.newBuilder()
65+
.setParent(parent.toString())
66+
.setRequestMetadata(requestMetadata)
67+
.setCustomRankingInfo(customRankingInfo)
68+
.setOrderBy(orderBy)
69+
.build();
70+
for (SearchJobsResponse.MatchingJob responseItem :
71+
jobServiceClient.searchJobs(request).getMatchingJobsList()) {
72+
System.out.format("Job summary: %s%n", responseItem.getJobSummary());
73+
System.out.format("Job title snippet: %s%n", responseItem.getJobTitleSnippet());
74+
Job job = responseItem.getJob();
75+
System.out.format("Job name: %s%n", job.getName());
76+
System.out.format("Job title: %s%n", job.getTitle());
77+
}
78+
}
79+
}
80+
}
81+
// [END job_search_custom_ranking_search]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2020 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 com.example.jobs;
18+
19+
// [START job_search_histogram_search]
20+
21+
import com.google.cloud.talent.v4.HistogramQuery;
22+
import com.google.cloud.talent.v4.Job;
23+
import com.google.cloud.talent.v4.JobServiceClient;
24+
import com.google.cloud.talent.v4.RequestMetadata;
25+
import com.google.cloud.talent.v4.SearchJobsRequest;
26+
import com.google.cloud.talent.v4.SearchJobsResponse;
27+
import com.google.cloud.talent.v4.TenantName;
28+
import java.io.IOException;
29+
30+
public class HistogramSearchJobs {
31+
32+
public static void searchJobs() throws IOException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "your-project-id";
35+
String tenantId = "your-tenant-id";
36+
String query = "count(base_compensation, [bucket(12, 20)])";
37+
searchJobs(projectId, tenantId, query);
38+
}
39+
40+
// Search Jobs with histogram queries.
41+
public static void searchJobs(String projectId, String tenantId, String query)
42+
throws IOException {
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests. After completing all of your requests, call
45+
// the "close" method on the client to safely clean up any remaining background resources.
46+
try (JobServiceClient jobServiceClient = JobServiceClient.create()) {
47+
TenantName parent = TenantName.of(projectId, tenantId);
48+
49+
String domain = "http://www.jobUrl.com";
50+
String sessionId = "Hashed session identifier";
51+
String userId = "Hashed user identifier";
52+
RequestMetadata requestMetadata =
53+
RequestMetadata.newBuilder()
54+
.setDomain(domain)
55+
.setSessionId(sessionId)
56+
.setUserId(userId)
57+
.build();
58+
HistogramQuery histogramQueriesElement =
59+
HistogramQuery.newBuilder().setHistogramQuery(query).build();
60+
SearchJobsRequest request =
61+
SearchJobsRequest.newBuilder()
62+
.setParent(parent.toString())
63+
.setRequestMetadata(requestMetadata)
64+
.addHistogramQueries(histogramQueriesElement)
65+
.build();
66+
67+
for (SearchJobsResponse.MatchingJob responseItem :
68+
jobServiceClient.searchJobs(request).getMatchingJobsList()) {
69+
System.out.format("Job summary: %s%n", responseItem.getJobSummary());
70+
System.out.format("Job title snippet: %s%n", responseItem.getJobTitleSnippet());
71+
Job job = responseItem.getJob();
72+
System.out.format("Job name: %s%n", job.getName());
73+
System.out.format("Job title: %s%n", job.getTitle());
74+
}
75+
}
76+
}
77+
}
78+
// [END job_search_histogram_search]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2020 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 com.example.jobs;
18+
19+
// [START job_search_autocomplete_job_title]
20+
21+
import com.google.cloud.talent.v4.CompleteQueryRequest;
22+
import com.google.cloud.talent.v4.CompleteQueryResponse;
23+
import com.google.cloud.talent.v4.CompletionClient;
24+
import com.google.cloud.talent.v4.TenantName;
25+
import java.io.IOException;
26+
27+
public class JobSearchAutoCompleteJobTitle {
28+
29+
public static void completeQuery() throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "your-project-id";
32+
String tenantId = "your-tenant-id";
33+
String query = "your-query-for-job-title";
34+
completeQuery(projectId, tenantId, query);
35+
}
36+
37+
// Complete job title given partial text (autocomplete).
38+
public static void completeQuery(String projectId, String tenantId, String query)
39+
throws IOException {
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests. After completing all of your requests, call
42+
// the "close" method on the client to safely clean up any remaining background resources.
43+
try (CompletionClient completionClient = CompletionClient.create()) {
44+
TenantName parent = TenantName.of(projectId, tenantId);
45+
CompleteQueryRequest request =
46+
CompleteQueryRequest.newBuilder()
47+
.setTenant(parent.toString())
48+
.setQuery(query)
49+
.setPageSize(5) // limit for number of results
50+
.addLanguageCodes("en-US") // language code
51+
.build();
52+
CompleteQueryResponse response = completionClient.completeQuery(request);
53+
for (CompleteQueryResponse.CompletionResult result : response.getCompletionResultsList()) {
54+
System.out.format("Suggested title: %s%n", result.getSuggestion());
55+
// Suggestion type is JOB_TITLE or COMPANY_TITLE
56+
System.out.format("Suggestion type: %s%n", result.getType());
57+
}
58+
}
59+
}
60+
}
61+
// [END job_search_autocomplete_job_title]

0 commit comments

Comments
 (0)