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

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

  1. 在实现通勤搜索之前,必须将 Cloud Talent Solution 连接到您的界面。请按照快速入门指南中的操作来设置 Cloud Talent Solution。

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

    Java

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

    
    public static void commuteSearch(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");
      JobQuery jobQuery =
          new JobQuery()
              .setCommuteFilter(
                  new CommuteFilter()
                      .setRoadTraffic("TRAFFIC_FREE")
                      .setCommuteMethod("TRANSIT")
                      .setTravelDuration("1000s")
                      .setStartCoordinates(
                          new LatLng().setLatitude(37.422408).setLongitude(-122.085609)));
      if (companyName != null) {
        jobQuery.setCompanyNames(Arrays.asList(companyName));
      }
    
      SearchJobsRequest searchJobsRequest =
          new SearchJobsRequest()
              .setJobQuery(jobQuery)
              .setRequestMetadata(requestMetadata)
              .setJobView("JOB_VIEW_FULL")
              .setRequirePreciseResultSize(true);
      SearchJobsResponse response =
          talentSolutionClient
              .projects()
              .jobs()
              .search(DEFAULT_PROJECT_ID, searchJobsRequest)
              .execute();
      Thread.sleep(1000);
      System.out.printf("Search jobs for commute results: %s\n", response);
    }

    Python

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

    def commute_search(client_service, company_name):
        request_metadata = {
            "user_id": "HashedUserId",
            "session_id": "HashedSessionId",
            "domain": "www.google.com",
        }
        start_location = {"latitude": 37.422408, "longitude": -122.085609}
        commute_preference = {
            "road_traffic": "TRAFFIC_FREE",
            "commute_method": "TRANSIT",
            "travel_duration": "1000s",
            "start_coordinates": start_location,
        }
        job_query = {"commute_filter": commute_preference}
        if company_name is not None:
            job_query.update({"company_names": [company_name]})
        request = {
            "job_query": job_query,
            "request_metadata": request_metadata,
            "job_view": "JOB_VIEW_FULL",
            "require_precise_result_size": True,
        }
        response = (
            client_service.projects().jobs().search(parent=parent, body=request).execute()
        )
        print(response)
    
    

    Go

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

    
    // commuteSearch searches for jobs within commute filter.
    func commuteSearch(w io.Writer, projectID, companyName string) (*talent.SearchJobsResponse, error) {
    	ctx := context.Background()
    
    	client, err := google.DefaultClient(ctx, talent.CloudPlatformScope)
    	if err != nil {
    		return nil, fmt.Errorf("google.DefaultClient: %w", err)
    	}
    	// Create the jobs service client.
    	service, err := talent.New(client)
    	if err != nil {
    		return nil, fmt.Errorf("talent.New: %w", err)
    	}
    
    	jobQuery := &talent.JobQuery{
    		CommuteFilter: &talent.CommuteFilter{
    			RoadTraffic:    "TRAFFIC_FREE",
    			CommuteMethod:  "TRANSIT",
    			TravelDuration: "1000s",
    			StartCoordinates: &talent.LatLng{
    				Latitude:  37.422408,
    				Longitude: -122.085609,
    			},
    		},
    	}
    	if companyName != "" {
    		jobQuery.CompanyNames = []string{companyName}
    	}
    
    	parent := "projects/" + projectID
    	req := &talent.SearchJobsRequest{
    		// Make sure to set the RequestMetadata the same as the associated
    		// search request.
    		RequestMetadata: &talent.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,
    		// Set the search mode to a regular search.
    		SearchMode:               "JOB_SEARCH",
    		RequirePreciseResultSize: true,
    	}
    	resp, err := service.Projects.Jobs.Search(parent, req).Do()
    	if err != nil {
    		return nil, fmt.Errorf("failed to search for jobs with commute filter: %w", err)
    	}
    	return resp, nil
    }
    

    Ruby

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

    # project_id              = "Id of the project"
    # commute_method          = "The method of transportation for which to calculate the commute time"
    # travel_duration         = "The maximum travel time in seconds"
    # start_coordinates       = "The latitude and longitude of the location from which to calculate the commute time"
    
    require "google/apis/jobs_v3"
    
    jobs = Google::Apis::JobsV3
    talent_solution_client = jobs::CloudTalentSolutionService.new
    # @see https://developers.google.com/identity/protocols/application-default-credentials#callingruby
    talent_solution_client.authorization = Google::Auth.get_application_default(
      "https://www.googleapis.com/auth/jobs"
    )
    
    # Make sure to set the request_metadata the same as the associated search request
    request_metadata = jobs::RequestMetadata.new user_id:    "HashedUserId",
                                                 session_id: "HashedSessionId",
                                                 domain:     "www.google.com"
    # Set location filter
    commute_filter = jobs::CommuteFilter.new road_traffic:      "TRAFFIC_FREE",
                                             commute_method:    commute_method,
                                             travel_duration:   travel_duration,
                                             start_coordinates: start_coordinates
    # Perform a search for analyst  related jobs
    search_jobs_request =
      jobs::SearchJobsRequest.new request_metadata:            request_metadata,
                                  job_query:                   (jobs::JobQuery.new commute_filter: commute_filter),
                                  job_view:                    "JOB_VIEW_FULL",
                                  require_precise_result_size: true
    search_jobs_response = talent_solution_client.search_jobs project_id, search_jobs_request
    puts search_jobs_response.to_json
    search_jobs_response

界面建议

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

  2. 修改应用的前端,实现以下目标:当求职者请求执行通勤时间搜索时,后端在通勤过滤器中填充相关信息,就像在常规搜索请求中一样调用 API。

  3. 在您的界面中包括以下新添加的内容:

    • 一个指示是根据距离搜索还是根据通勤时间搜索的选项。例如,您的搜索界面可能类似以下示例:

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

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

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

    • 通勤开始时间。

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

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

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