Home

Setting Limit and Handling Pagination in Google Cloud Search Requests

52 views

To set the limit of the search (i.e., the number of search results returned), you can use the pageSize parameter in your search request. The pageSize parameter specifies the maximum number of results to return.

Here’s how you can add the pageSize parameter to your search request:

// Import the library
const {SearchServiceClient} = require('@google-cloud/retail');

// Create a client
const client = new SearchServiceClient();

// Create a search request with pageSize
async function searchItems() {
  const request = {
    placement: 'projects/YOUR-PROJECT-ID/locations/global/catalogs/default_catalog/placements/default_search', // Replace YOUR-PROJECT-ID with your project ID
    query: 'your search query',
    visitorId: 'unique_visitor_id', // Assign a unique visitor ID
    pageSize: 10, // Set the number of results to return
  };

  try {
    // Execute the search request
    const [response] = await client.search(request);
    console.log('Search results:');
    response.results.forEach(item => {
      console.log(`Product ID: ${item.id}`);
      console.log(`Product Title: ${item.title}`);
      console.log(`Score: ${item.matchingVariantFields}`);
    });
  } catch (error) {
    console.error('Error during search:', error);
  }
}

// Call the search function
searchItems();

Key Points:

  1. pageSize:

    • The pageSize parameter is set to 10 in this example, meaning the search request will return up to 10 results. You can adjust this value based on your requirements.
  2. Other parameters:

    • All other parameters such as placement, query, and visitorId remain the same as explained previously.
  3. Handling Pagination:

    • If you expect a large number of results, the response will be paginated. The response object will include a nextPageToken field which you can use to fetch the next set of results.

Example of Handling Pagination:

async function searchItems(pageToken = null) {
  const request = {
    placement: 'projects/YOUR-PROJECT-ID/locations/global/catalogs/default_catalog/placements/default_search',
    query: 'your search query',
    visitorId: 'unique_visitor_id',
    pageSize: 10,
    pageToken: pageToken // Use pageToken if provided
  };

  try {
    const [response] = await client.search(request);
    console.log('Search results:');
    response.results.forEach(item => {
      console.log(`Product ID: ${item.id}`);
      console.log(`Product Title: ${item.title}`);
      console.log(`Score: ${item.matchingVariantFields}`);
    });

    // If there are more pages, fetch the next page
    if (response.nextPageToken) {
      console.log('Fetching next page...');
      await searchItems(response.nextPageToken);
    }
  } catch (error) {
    console.error('Error during search:', error);
  }
}

searchItems();

This example demonstrates how you can paginate through the results by recursively calling the searchItems function with the nextPageToken. Adjust the pageSize parameter to suit the number of results you need per page.