Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

google Retail API - Browse Search

Hi,

I'm trying to build hierarchical category pages similar to the ones we see in common e-commerce stores, the user will press on Dresses, or Pants, under Clothing for example.

I've added the Categories values in the product catalog similar to: 

Apparel & Accessories > Clothing > Dresses

Apparel & Accessories > Clothing > Dresses > Maxi

Apparel & Accessories > Clothing > Dresses > Midi

however, when I use the below query in the search API, I get the same results regardless of what the pageCatogories value is. 

POST https://retail.googleapis.com/v2/projects/******/locations/global/catalogs/default_catalog/servingCo...] HTTP/1.1

Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json
Content-Type: application/json

{
"pageCategories": [
"Apparel & Accessories > Clothing > Dresses"
],
"visitorId": "1",
"query": ""
}

I do not have user events logged yet as I didn't launch the app so far. If this method depends on active user events, how can I build hierarchical category pages before the launch?

Solved Solved
1 4 1,825
1 ACCEPTED SOLUTION

Hi,

To use the Google Retail API to search for products that have a specific category in their "categories" value, you can use the facet_specs parameter in the SearchProductsRequest message. The facet_specs parameter allows you to specify a list of facets to filter the search results by. Each facet is a key-value pair, where the key is the name of the facet and the value is a list of values to filter by.

Here is an example of how to use the facet_specs parameter to search for products that have the category "Apparel & Accessories" in their "categories" value:

// Create a SearchProductsRequest message
SearchProductsRequest search_products_request = SearchProductsRequest.newBuilder()
  .setBranchName(branchName)
  .setVisitorId(visitorId)
  .setQuery("Apparel & Accessories")
  .addFacetSpecs(FacetSpec.newBuilder()
      .setType(FacetSpec.Type.CATEGORY)
      .setName("categories")
      .addValues("Apparel & Accessories"))
  .build();

// Send the request to the Retail API
SearchProductsResponse search_products_response = retailApi.searchProducts(search_products_request);

// Process the response
for (Product product : search_products_response.getProductsList()) {
  System.out.println("Product: " + product.getName());
  for (Category category : product.getCategoriesList()) {
    System.out.println("  Category: " + category.getName());
  }
}

This code will search for products that have the category "Apparel & Accessories" in their "categories" value and print the name of each product and each of its categories to the console.

The hierarchical aspect of the search result can be achieved by using the parent_category_filter parameter in the SearchProductsRequest message. The parent_category_filter parameter allows you to specify a category to filter the search results by. The search results will only include products that have the specified category as a parent category.

Here is an example of how to use the parent_category_filter parameter to search for products that have the category "Apparel & Accessories > Clothing" in their "categories" value:

// Create a SearchProductsRequest message
SearchProductsRequest search_products_request = SearchProductsRequest.newBuilder()
  .setBranchName(branchName)
  .setVisitorId(visitorId)
  .setQuery("Apparel & Accessories")
  .addFacetSpecs(FacetSpec.newBuilder()
      .setType(FacetSpec.Type.CATEGORY)
      .setName("categories")
      .addValues("Apparel & Accessories")
      .addValues("Apparel & Accessories > Clothing"))
  .setParentCategoryFilter("Apparel & Accessories")
  .build();

// Send the request to the Retail API
SearchProductsResponse search_products_response = retailApi.searchProducts(search_products_request);

// Process the response
for (Product product : search_products_response.getProductsList()) {
  System.out.println("Product: " + product.getName());
  for (Category category : product.getCategoriesList()) {
    System.out.println("  Category: " + category.getName());
  }
}

This code will search for products that have the category "Apparel & Accessories > Clothing" in their "categories" value and print the name of each product and each of its categories to the console.

I hope this helps!

View solution in original post

4 REPLIES 4