搜索请求可包含通勤时间过滤条件,以便仅返回从起始点开始通勤时间在指定行程时间内的职位。结果包括匹配职位的预计通勤时间,以秒为单位。
特定通勤时间搜索
要返回特定通勤时间内的职位,请发送 jobs.search
请求,并在 JobQuery.commuteFilter
字段中包含 CommuteFilter
对象。Cloud Talent Solution 使用职位的 address
来计算该职位的通勤时间。如果未提供详细地址,Cloud Talent Solution 会尝试推断职位的实际街道地址。
Java
import com.google.cloud.talent.v4.CommuteFilter;
import com.google.cloud.talent.v4.CommuteMethod;
import com.google.cloud.talent.v4.Job;
import com.google.cloud.talent.v4.JobQuery;
import com.google.cloud.talent.v4.JobServiceClient;
import com.google.cloud.talent.v4.RequestMetadata;
import com.google.cloud.talent.v4.SearchJobsRequest;
import com.google.cloud.talent.v4.SearchJobsResponse;
import com.google.cloud.talent.v4.TenantName;
import com.google.protobuf.Duration;
import com.google.type.LatLng;
import java.io.IOException;
public class CommuteSearchJobs {
public static void searchJobs() throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String tenantId = "your-tenant-id";
searchJobs(projectId, tenantId);
}
// Search Jobs with histogram queries.
public static void searchJobs(String projectId, String tenantId) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (JobServiceClient jobServiceClient = JobServiceClient.create()) {
TenantName parent = TenantName.of(projectId, tenantId);
String domain = "www.example.com";
String sessionId = "Hashed session identifier";
String userId = "Hashed user identifier";
RequestMetadata requestMetadata =
RequestMetadata.newBuilder()
.setDomain(domain)
.setSessionId(sessionId)
.setUserId(userId)
.build();
CommuteMethod commuteMethod = CommuteMethod.DRIVING;
long seconds = 3600L;
Duration travelDuration = Duration.newBuilder().setSeconds(seconds).build();
double latitude = 37.422408;
double longitude = -122.084068;
LatLng startCoordinates =
LatLng.newBuilder().setLatitude(latitude).setLongitude(longitude).build();
CommuteFilter commuteFilter =
CommuteFilter.newBuilder()
.setCommuteMethod(commuteMethod)
.setTravelDuration(travelDuration)
.setStartCoordinates(startCoordinates)
.build();
JobQuery jobQuery = JobQuery.newBuilder().setCommuteFilter(commuteFilter).build();
SearchJobsRequest request =
SearchJobsRequest.newBuilder()
.setParent(parent.toString())
.setRequestMetadata(requestMetadata)
.setJobQuery(jobQuery)
.build();
for (SearchJobsResponse.MatchingJob responseItem :
jobServiceClient.searchJobs(request).getMatchingJobsList()) {
System.out.format("Job summary: %s%n", responseItem.getJobSummary());
System.out.format("Job title snippet: %s%n", responseItem.getJobTitleSnippet());
Job job = responseItem.getJob();
System.out.format("Job name: %s%n", job.getName());
System.out.format("Job title: %s%n", job.getTitle());
}
}
}
}
Java
如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库。
import com.google.cloud.talent.v4beta1.CommuteFilter;
import com.google.cloud.talent.v4beta1.CommuteMethod;
import com.google.cloud.talent.v4beta1.Job;
import com.google.cloud.talent.v4beta1.JobQuery;
import com.google.cloud.talent.v4beta1.JobServiceClient;
import com.google.cloud.talent.v4beta1.RequestMetadata;
import com.google.cloud.talent.v4beta1.SearchJobsRequest;
import com.google.cloud.talent.v4beta1.SearchJobsResponse;
import com.google.cloud.talent.v4beta1.TenantName;
import com.google.protobuf.Duration;
import com.google.type.LatLng;
import java.io.IOException;
public class CommuteSearchJobs {
public static void searchJobs() throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String tenantId = "your-tenant-id";
searchJobs(projectId, tenantId);
}
// Search Jobs with histogram queries.
public static void searchJobs(String projectId, String tenantId) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (JobServiceClient jobServiceClient = JobServiceClient.create()) {
TenantName parent = TenantName.of(projectId, tenantId);
String domain = "www.example.com";
String sessionId = "Hashed session identifier";
String userId = "Hashed user identifier";
RequestMetadata requestMetadata =
RequestMetadata.newBuilder()
.setDomain(domain)
.setSessionId(sessionId)
.setUserId(userId)
.build();
CommuteMethod commuteMethod = CommuteMethod.DRIVING;
long seconds = 3600L;
Duration travelDuration = Duration.newBuilder().setSeconds(seconds).build();
double latitude = 37.422408;
double longitude = -122.084068;
LatLng startCoordinates =
LatLng.newBuilder().setLatitude(latitude).setLongitude(longitude).build();
CommuteFilter commuteFilter =
CommuteFilter.newBuilder()
.setCommuteMethod(commuteMethod)
.setTravelDuration(travelDuration)
.setStartCoordinates(startCoordinates)
.build();
JobQuery jobQuery = JobQuery.newBuilder().setCommuteFilter(commuteFilter).build();
SearchJobsRequest request =
SearchJobsRequest.newBuilder()
.setParent(parent.toString())
.setRequestMetadata(requestMetadata)
.setJobQuery(jobQuery)
.build();
for (SearchJobsResponse.MatchingJob responseItem :
jobServiceClient.searchJobs(request).iterateAll()) {
System.out.format("Job summary: %s%n", responseItem.getJobSummary());
System.out.format("Job title snippet: %s%n", responseItem.getJobTitleSnippet());
Job job = responseItem.getJob();
System.out.format("Job name: %s%n", job.getName());
System.out.format("Job title: %s%n", job.getTitle());
}
}
}
}
Node.js
如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库。
const talent = require('@google-cloud/talent').v4;
/** Search Jobs using commute distance */
function sampleSearchJobs(projectId, tenantId) {
const client = new talent.JobServiceClient();
// Iterate over all elements.
// const projectId = 'Your Google Cloud Project ID';
// const tenantId = 'Your Tenant ID (using tenancy is optional)';
const formattedParent = client.tenantPath(projectId, tenantId);
const domain = 'www.example.com';
const sessionId = 'Hashed session identifier';
const userId = 'Hashed user identifier';
const requestMetadata = {
domain: domain,
sessionId: sessionId,
userId: userId,
};
const commuteMethod = 'TRANSIT';
const seconds = 1800;
const travelDuration = {
seconds: seconds,
};
const latitude = 37.422408;
const longitude = 122.084068;
const startCoordinates = {
latitude: latitude,
longitude: longitude,
};
const commuteFilter = {
commuteMethod: commuteMethod,
travelDuration: travelDuration,
startCoordinates: startCoordinates,
};
const jobQuery = {
commuteFilter: commuteFilter,
};
const request = {
parent: formattedParent,
requestMetadata: requestMetadata,
jobQuery: jobQuery,
};
client
.searchJobs(request)
.then(responses => {
const resources = responses[0];
for (const resource of resources) {
console.log(`Job summary: ${resource.jobSummary}`);
console.log(`Job title snippet: ${resource.jobTitleSnippet}`);
const job = resource.job;
console.log(`Job name: ${job.name}`);
console.log(`Job title: ${job.title}`);
}
})
.catch(err => {
console.error(err);
});
}
Python
如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库。
from google.cloud import talent
import six
def search_jobs(project_id, tenant_id):
"""Search Jobs using commute distance"""
client = talent.JobServiceClient()
# project_id = 'Your Google Cloud Project ID'
# tenant_id = 'Your Tenant ID (using tenancy is optional)'
if isinstance(project_id, six.binary_type):
project_id = project_id.decode("utf-8")
if isinstance(tenant_id, six.binary_type):
tenant_id = tenant_id.decode("utf-8")
parent = f"projects/{project_id}/tenants/{tenant_id}"
domain = "www.example.com"
session_id = "Hashed session identifier"
user_id = "Hashed user identifier"
request_metadata = talent.RequestMetadata(
domain=domain, session_id=session_id, user_id=user_id
)
commute_method = talent.CommuteMethod.TRANSIT
seconds = 1800
travel_duration = {"seconds": seconds}
latitude = 37.422408
longitude = -122.084068
start_coordinates = {"latitude": latitude, "longitude": longitude}
commute_filter = talent.CommuteFilter(
commute_method=commute_method,
travel_duration=travel_duration,
start_coordinates=start_coordinates,
)
job_query = talent.JobQuery(commute_filter=commute_filter)
# Iterate over all results
results = []
request = talent.SearchJobsRequest(
parent=parent, request_metadata=request_metadata, job_query=job_query,
)
for response_item in client.search_jobs(request=request).matching_jobs:
print(f"Job summary: {response_item.job_summary}")
print(f"Job title snippet: {response_item.job_title_snippet}")
job = response_item.job
results.append(job.name)
print(f"Job name: {job.name}")
print(f"Job title: {job.title}")
return results
必填字段
commuteMethod
:用于计算通勤时间的交通方法。选项包括DRIVING
、TRANSIT
、WALKING
和CYCLING
出行方式。步行和骑行路线可能无法反映现实情况(如施工路段),或不包括专门的步行或自行车道。返回的结果中将包含针对这些响应的warnings
,您必须向用户显示这些信息。travelDuration
:最长行程时间,以秒为单位。允许的最大值为3600s
(一小时)。格式为123s
。startCoordinates
:用于计算通勤时间的起始地点的纬度和经度。该字段接受LatLng
对象。roadTraffic
或departureTime
:通勤时间搜索结果基于历史记录数据和汇总数据,而不是实时路况信息。无论用户在一天中的什么时间发送查询,他们都会收到相同的通勤时间搜索结果。roadTraffic
:指定计算通勤时间时要使用的车流密度。BUSY_HOUR
/TRAFFIC_FREE
选项分别是早高峰时段和午夜的平均路况信息。如果指定了departureTime
,则此字段不得存在。departureTime
:用于计算交通影响的出发小时数。接受 0 到 23 之间的整数,表示startLocation
的时区中的小时。路况信息是根据一天中指定时间的平均路况信息计算得出的。如果指定了roadTraffic
,则此字段不得存在。
可选字段
allowImpreciseAddresses
:“精确”地址定义为街道级地址或 GPS 坐标。如果allowImpreciseAddresses
设置为true
,也可以返回地址“不精确”(仅包含城市、州/省或国家/地区信息)的职位。对于城市级别和更宽泛级别的地址,API 使用文本匹配来返回结果。如果此字段设置为false
或未指定,则通勤时间搜索仅返回包含精确地址的职位。