在界面中实现通勤时间搜索

您可以将通勤时间搜索功能集成到界面中,让求职者能够搜索由通勤时间设置的地理范围内的职位。通勤时间搜索根据用户选择的出行方式和计划的出门时间来估算通勤时间。

  1. 在实现通勤时间搜索之前,必须先将 Cloud Talent Solution (CTS) 集成到您的界面。请按照快速入门指南中的操作来设置 Cloud Talent Solution。

  2. 通勤时间搜索使用您在实现 CTS 时随职位一起上传的地址数据来计算通勤时间。要在现有 CTS 界面上启用此功能,请发出 jobs.search 请求,并在 JobQuery.commuteFilter 字段中包含 CommuteFilter 对象。commuteMethodtravelDurationstartCoordinatesroadTrafficdepartureTime 是必填字段。

Go

import (
	"context"
	"fmt"
	"io"

	talent "cloud.google.com/go/talent/apiv4beta1"
	"github.com/golang/protobuf/ptypes/duration"
	talentpb "google.golang.org/genproto/googleapis/cloud/talent/v4beta1"
	"google.golang.org/genproto/googleapis/type/latlng"
)

// commuteSearch searches for jobs within commute filter.
func commuteSearch(w io.Writer, projectID, companyID string) error {
	ctx := context.Background()

	// Initialize a jobService client.
	c, err := talent.NewJobClient(ctx)
	if err != nil {
		return fmt.Errorf("talent.NewJobClient: %v", err)
	}

	// Construct a jobQuery request with a commute filter.
	jobQuery := &talentpb.JobQuery{
		CommuteFilter: &talentpb.CommuteFilter{
			CommuteMethod:  talentpb.CommuteMethod_TRANSIT,
			TravelDuration: &duration.Duration{Seconds: 1800},
			StartCoordinates: &latlng.LatLng{
				Latitude:  37.422408,
				Longitude: -122.085609,
			},
		},
	}
	if companyID != "" {
		jobQuery.Companies = []string{fmt.Sprintf("projects/%s/companies/%s", projectID, companyID)}
	}

	// Construct a searchJobs request with a jobQuery.
	req := &talentpb.SearchJobsRequest{
		Parent: fmt.Sprintf("projects/%s", projectID),
		// Make sure to set the RequestMetadata the same as the associated
		// search request.
		RequestMetadata: &talentpb.RequestMetadata{
			// Make sure to hash your userID.
			UserId: "HashedUsrID",
			// Make sure to hash the sessionID.
			SessionId: "HashedSessionID",
			// Domain of the website where the search is conducted.
			Domain: "www.googlesample.com",
		},
		// Set the actual search term as defined in the jobQuery.
		JobQuery: jobQuery,
	}

	resp, err := c.SearchJobs(ctx, req)
	if err != nil {
		return fmt.Errorf("SearchJobs: %v", err)
	}

	for _, job := range resp.GetMatchingJobs() {
		fmt.Fprintf(w, "Matcing Job: %q\n", job.GetJob().GetName())
		fmt.Fprintf(w, "Job address: %v\n", job.GetCommuteInfo().GetJobLocation().GetPostalAddress().GetAddressLines())
	}

	return nil
}

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 {
    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.printf("Job summary: %s\n", responseItem.getJobSummary());
        System.out.printf("Job title snippet: %s\n", responseItem.getJobTitleSnippet());
        Job job = responseItem.getJob();
        System.out.printf("Job name: %s\n", job.getName());
        System.out.printf("Job title: %s\n", job.getTitle());
      }
    }
  }
}

PHP

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库

require __DIR__ . '/vendor/autoload.php';

use Google\Cloud\Talent\V4beta1\JobServiceClient;
use Google\Cloud\Talent\V4beta1\CommuteFilter;
use Google\Cloud\Talent\V4beta1\CommuteMethod;
use Google\Cloud\Talent\V4beta1\JobQuery;
use Google\Cloud\Talent\V4beta1\RequestMetadata;
use Google\Protobuf\Duration;
use Google\Type\LatLng;

/** Search Jobs using commute distance */
function sampleSearchJobs($projectId, $tenantId)
{

    $jobServiceClient = new JobServiceClient();

    // $projectId = 'Your Google Cloud Project ID';
    // $tenantId = 'Your Tenant ID (using tenancy is optional)';
    $formattedParent = $jobServiceClient->tenantName($projectId, $tenantId);
    $domain = 'www.example.com';
    $sessionId = 'Hashed session identifier';
    $userId = 'Hashed user identifier';
    $requestMetadata = new RequestMetadata();
    $requestMetadata->setDomain($domain);
    $requestMetadata->setSessionId($sessionId);
    $requestMetadata->setUserId($userId);
    $commuteMethod = CommuteMethod::TRANSIT;
    $seconds = 1800;
    $travelDuration = new Duration();
    $travelDuration->setSeconds($seconds);
    $latitude = 37.422408;
    $longitude = 122.084068;
    $startCoordinates = new LatLng();
    $startCoordinates->setLatitude($latitude);
    $startCoordinates->setLongitude($longitude);
    $commuteFilter = new CommuteFilter();
    $commuteFilter->setCommuteMethod($commuteMethod);
    $commuteFilter->setTravelDuration($travelDuration);
    $commuteFilter->setStartCoordinates($startCoordinates);
    $jobQuery = new JobQuery();
    $jobQuery->setCommuteFilter($commuteFilter);

    try {
        // Iterate through all elements
        $pagedResponse = $jobServiceClient->searchJobs($formattedParent, $requestMetadata, ['customRankingInfo' => $customRankingInfo, 'orderBy' => $orderBy]);
        foreach ($pagedResponse->iterateAllElements() as $responseItem) {
            printf('Job summary: %s'.PHP_EOL, $responseItem->getJobSummary());
            printf('Job title snippet: %s'.PHP_EOL, $responseItem->getJobTitleSnippet());
            $job = $responseItem->getJob();
            printf('Job name: %s'.PHP_EOL, $job->getName());
            printf('Job title: %s'.PHP_EOL, $job->getTitle());
        }
    } finally {
        $jobServiceClient->close();
    }

}

Ruby

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库


# Search Jobs using commute distance
def sample_search_jobs project_id, tenant_id
  # Instantiate a client
  job_client = Google::Cloud::Talent::JobService.new version: :v4beta1

  # project_id = "Your Google Cloud Project ID"
  # tenant_id = "Your Tenant ID (using tenancy is optional)"
  formatted_parent = job_client.class.tenant_path(project_id, tenant_id)
  domain = "www.example.com"
  session_id = "Hashed session identifier"
  user_id = "Hashed user identifier"
  request_metadata = {
    domain: domain,
    session_id: session_id,
    user_id: user_id
  }
  commute_method = :TRANSIT
  seconds = 1800
  travel_duration = { seconds: seconds }
  latitude = 37.422408
  longitude = -122.084068
  start_coordinates = { latitude: latitude, longitude: longitude }
  commute_filter = {
    commute_method: commute_method,
    travel_duration: travel_duration,
    start_coordinates: start_coordinates
  }
  job_query = { commute_filter: commute_filter }

  # Iterate over all results.
  job_client.search_jobs(formatted_parent, request_metadata, job_query: job_query).each do |element|
    puts "Job summary: #{element.job_summary}"
    puts "Job title snippet: #{element.job_title_snippet}"
    job = element.job
    puts "Job name: #{job.name}"
    puts "Job title: #{job.title}"
  end
end

C#

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库

public static object CommuteSearchJobs(string projectId, string tenantId)
{
    JobServiceClient jobServiceClient = JobServiceClient.Create();
    TenantName name = TenantName.FromProjectTenant(projectId, tenantId);

    string domain = "www.example.com";
    string sessionId = "Hashed session identifier";
    string userId = "Hashed user identifier";
    RequestMetadata requestMetadata = new RequestMetadata
    {
        Domain = domain,
        SessionId = sessionId,
        UserId = userId
    };

    CommuteMethod commuteMethod = CommuteMethod.Driving;
    long seconds = 3600L;
    Duration travelDuration = new Duration
    {
        Seconds = seconds
    };

    double latitude = 37.422408;
    double longitude = -122.084068;
    LatLng startCoordinates = new LatLng
    {
        Latitude = latitude,
        Longitude = longitude
    };

    CommuteFilter commuteFilter = new CommuteFilter
    {
        CommuteMethod = commuteMethod,
        TravelDuration = travelDuration,
        StartCoordinates = startCoordinates
    };

    JobQuery jobQuery = new JobQuery
    {
        CommuteFilter = commuteFilter
    };

    SearchJobsRequest request = new SearchJobsRequest
    {
        ParentAsTenantName = name,
        RequestMetadata = requestMetadata,
        JobQuery = jobQuery
    };

    var response = jobServiceClient.SearchJobs(request);
    foreach (var result in response)
    {
        Console.WriteLine($"Job summary: {result.JobSummary}");
        Console.WriteLine($"Job title snippet: {result.JobTitleSnippet}");
        Job job = result.Job;
        Console.WriteLine($"Job name: {job.Name}");
        Console.WriteLine($"Job title: {job.Title}");
    }

    return 0;
}

界面建议

  1. Cloud Talent Solution 不允许在同一 API 调用中同时按距离(使用 CTS 位置过滤器)和通勤时间进行搜索。求职者若想同时访问这两个选项,可在两个标签页中分别使用不同的搜索条件或者使用类似方法。

  2. 修改应用程序的前端,以确保后端自动将求职者的相关信息填充到通勤过滤器中。后端应该像在常规搜索请求中那样调用 API。

  3. 在界面中包括以下项目:

    • 用于选择距离搜索还是通勤时间搜索的选项。例如,您的搜索界面可能类似以下示例:

    • 通勤方式选项的下拉菜单。

    • 用于调整交通状况的选项。

    • 总交通时间(支持的最长交通时间为 60 分钟)。

    • 出门时间。

  4. 使用从 API 返回的通勤时间信息向求职者显示相关信息。结果列表中只会返回位于指定通勤时间范围内的相关职位。请参阅职位搜索的最佳做法文档,了解如何调整在此时间范围内返回的职位顺序和数量。

  5. 通勤时间搜索结果基于历史记录数据和汇总数据,而不是实时路况信息。departureTime 路况信息是根据一天中指定时间的平均路况信息计算得出的。roadTraffic 下的 BUSY_HOUR/TRAFFIC_FREE 选项分别是早高峰时段和午夜的平均路况信息。无论用户在一天中的什么时间发送查询,他们都会收到相同的通勤时间搜索结果。

您可以利用 Google 地图并根据从 CTS API 返回的通勤时间信息生成地图,然后将地图嵌入返回给求职者的结果中。Maps API 套件有几个选项用来显示地图。某些 Maps API 选项比其他选项更有效。例如,将 Google Maps JavaScript 热图直观呈现功能与标记聚簇功能配合使用就是一种有效的方式,能够向求职者直观呈现根据其设定的通勤偏好确定的范围内的相关职位。相反,方向模式不显示搜索请求中返回的所有职位,因此不推荐使用此选项。

如需详细了解如何实现基于通勤时间的搜索,请参阅通勤时间搜索方法指南