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 804
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

The search API doesn't currently support filtering products based on hierarchical category pages. The pageCategories parameter is used to provide information about the current page the user is on, but it doesn't affect the search results.

If you want to build hierarchical category pages before launching your app, you'll need to implement a custom solution. One way to do this is to create a separate database that stores the hierarchical category structure. When a user clicks on a category, you can query the database to get the list of child categories and display them to the user.

Here's an example of how you could implement this using a NoSQL database like Cloud Firestore:

// Store the hierarchical category structure in a NoSQL database
const categories = {
'Apparel & Accessories': {
Clothing: {
Dresses: {
Maxi: {},
Midi: {},
},
Pants: {},
},
},
};

// Get the list of child categories for a given category
function getChildCategories(categoryPath) {
const category = categories;
for (const part of categoryPath.split('>')) {
category = category[part];
}
return Object.keys(category);
}

// When a user clicks on a category, query the database for the list of child categories
const categoryPath = 'Apparel & Accessories > Clothing > Dresses';
const childCategories = getChildCategories(categoryPath);
console.log(childCategories); // ['Maxi', 'Midi']

Hi quangtrunghuynh

Thank you for your reply. My problem is to get results from Google Retail API using the category structure. i.e. the CategoryPath you've mentioned in your response, how can I use it with the Retail API to search for products that have this category in their "categories" value? 

The hierarchical aspect of the search result is also a challenge, meaning if I search for the main category, it should get all products that falls under this category, but when I search for a child category it should go deeper and return subset of the main category. How can I achieve this using Retail API query?

 

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!

Hi quangtrunghuynh,

Thank you, this has been useful. we were thinking of using the Filter parameter in the search request. However, faceted search could be a better option.