Implementing Commute Search in your UI

You can integrate Commute Search into your UI to allow job seekers to search for jobs within a geographic area set by commute time. Commute Search estimates commute time based on a user's selected transit mode and the time of day they plan to travel.

  1. Before you can implement Commute Search, Cloud Talent Solution must be hooked up to your UI. Follow the quickstart guides to set up Cloud Talent Solution.

  2. Commute Search uses the address data that you uploaded with your jobs during CTS implementation to calculate commute time. To enable this feature on your existing CTS UI, send a jobs.search request and include a CommuteFilter object in the JobQuery.commuteFilter field. commuteMethod, travelDuration, and startCoordinates are required fields.

Go

import (
	"context"
	"fmt"
	"io"

	talent "cloud.google.com/go/talent/apiv4beta1"
	"github.com/golang/protobuf/ptypes/duration"
	"google.golang.org/api/iterator"
	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,
	}

	it := c.SearchJobs(ctx, req)

	for {
		resp, err := it.Next()
		if err == iterator.Done {
			return nil
		}
		if err != nil {
			return fmt.Errorf("it.Next: %v", err)
		}
		fmt.Fprintf(w, "Matcing job: %q\n", resp.GetJob().GetName())
		fmt.Fprintf(w, "Job address: %v\n", resp.GetCommuteInfo().GetJobLocation().GetPostalAddress().GetAddressLines())
	}
}

Java

For more on installing and creating a Cloud Talent Solution client, see Cloud Talent Solution Client Libraries.

/*
 * Please include the following imports to run this sample.
 *
 * 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.MatchingJob;
 * import com.google.cloud.talent.v4beta1.TenantName;
 * import com.google.cloud.talent.v4beta1.TenantOrProjectName;
 * import com.google.protobuf.Duration;
 * import com.google.type.LatLng;
 */

/** Search Jobs using commute distance */
public static void sampleSearchJobs(String projectId, String tenantId) {
  try (JobServiceClient jobServiceClient = JobServiceClient.create()) {
    // projectId = "Your Google Cloud Project ID";
    // tenantId = "Your Tenant ID (using tenancy is optional)";
    TenantOrProjectName 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.TRANSIT;
    long seconds = 1800L;
    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());
    }
  } catch (Exception exception) {
    System.err.println("Failed to create the client due to: " + exception);
  }
}

Node.js

For more on installing and creating a Cloud Talent Solution client, see Cloud Talent Solution Client Libraries.


const talent = require('@google-cloud/talent').v4beta1;

/** 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);
    });
}

PHP

For more on installing and creating a Cloud Talent Solution client, see Cloud Talent Solution Client Libraries.

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();
    }

}

Python

For more on installing and creating a Cloud Talent Solution client, see Cloud Talent Solution Client Libraries.


from google.cloud import talent_v4beta1
from google.cloud.talent_v4beta1 import enums
import six


def sample_search_jobs(project_id, tenant_id):
    """Search Jobs using commute distance"""

    client = talent_v4beta1.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 = client.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 = enums.CommuteMethod.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
    for response_item in client.search_jobs(parent,
                                            request_metadata,
                                            job_query=job_query):
        print('Job summary: {}'.format(response_item.job_summary))
        print('Job title snippet: {}'.format(response_item.job_title_snippet))
        job = response_item.job
        print('Job name: {}'.format(job.name))
        print('Job title: {}'.format(job.title))


Ruby

For more on installing and creating a Cloud Talent Solution client, see Cloud Talent Solution Client Libraries.


# 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, custom_ranking_info: custom_ranking_info, order_by: order_by).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

Recommendations for your UI

  1. Cloud Talent Solution doesn't allow searching by both distance (using the CTS location filter) AND commute time. To allow job seekers to access both options, use a 2-tab approach or similar.

  2. Modify the front-end of your application so that when a job seeker requests a commute search, the back-end populates the relevant information in the commute filter and calls the API as it would in a regular search request.

  3. Include these newly added items in your UI:

    • An option to choose whether this is a distance search or commute search. For example, your Search UI could look like the sample below:

    • A drop-down menu of commute method options.

    • An option to adjust traffic conditions.

    • The total travel time (the maximum supported travel time is 60 minutes).

    • Commute start time.

  4. The commute time information returned from the API is then used to display information to the job seeker. Only relevant jobs located within the designated commute time area are returned in the results list. See the Job Search Best Practices documentation for a discussion of ways to adjust the order and number of jobs returned within this area.

You can leverage Google Maps to generate a map based on the commute time information returned from CTS and embed it into the results returned to a job seeker. The Maps API suite has several options for displaying the map. Some Maps API options are more effective than others. For example, the Google Maps JavaScript Heatmap visualization paired with marker clustering is a great way to visualize the relevant jobs returned to a job seeker inside the area determined by their set commute preferences. Conversely, Directions Mode does not show all jobs returned in a search request and is not a recommended option.

For more information on implementing a commute-based search, see the Commute Search how-to guide.

Czy ta strona była pomocna? Podziel się z nami swoją opinią:

Wyślij opinię na temat...

Job Search documentation
Potrzebujesz pomocy? Odwiedź naszą stronę wsparcia.