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

How to retrieve several products at once

In vertex AI search for retail, how can I retrieve all products with one function call?

I found out that the get_product function is so slow. So If I get 10 product ids, then I need to do foreach.

It is too slow.

I would like to know how to retrive all fields information of the products with .search function not only id and product name.

If it's impossible, is there any way to retrieve list of products with all fields using one API invoke?

I tried to do with listproduct but it's not working.

 

Thank you in advance.

0 2 154
2 REPLIES 2

Hi @pavlodidushko,

Welcome to Google Cloud Community!

It looks like you are working with slow retrieval times using get_product in Vertex AI Search for Retail.

Here are potential ways that might help with your use case:

  • Use the search API with an empty query and a filter based on a list of product IDs: To retrieve comprehensive product information for multiple products in one API call, you can use the search method with an empty query. This prompts the API to return a full collection of product information without applying any search filters.
  • Add a field mask using the fields parameter: To specify the product fields you want to retrieve, use your fields parameter to tell the API exactly what fields you need for the return.
  • Check your Service Account: Ensure your service account is configured with the correct access rights to retrieve products. This is essential for accessing the product information effectively.
  • Error Handling: Ensure you implement your error handling to gracefully manage cases where products are not found or if there are API issues. This helps maintain the stability and reliability of your application.

You may refer to the documentation below, which offers pertinent information on field masks and the search method:

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.

Thank you for your reply. 
I tried it but actually it doesn't work. 
 
Please kindly review this codebase.

 

import google.auth
from google.cloud.retail import SearchRequest, SearchServiceClient
from google.cloud import retail_v2

 

client = retail_v2.ProductServiceClient()

 

project_id = google.auth.default()[1]



# get search service request:
def get_search_request(query: str😞
    default_search_placement = (
        "projects/"
        + project_id
        + "/locations/global/catalogs/default_catalog/placements/default_search"
    )

 

    search_request = SearchRequest()
    search_request.placement = default_search_placement  # Placement is used to identify the Serving Config name.
    search_request.filter="id:ANY(\"100\") or id:ANY(\"1000\") or id:ANY(\"10000\")"
    search_request.visitor_id = "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d"  # A unique identifier to track visitors
    # search_request.page_size = 10

 

    print("---search request:---")
    print(search_request)

 

    return search_request



# call the Retail Search:
def search():
    # TRY DIFFERENT QUERY PHRASES HERE:
    query_phrase = "em3615t"

 

    search_request = get_search_request(query_phrase)
    search_response = SearchServiceClient().search(search_request)

 

    print("response result woudl be : ", search_response.results)
    results = []
    # for result in search_response.results:
    #     product = client.get_product(name=result.product.name)



    #     results.append(product)



    print("---search response---")
    if not search_response.results:
        print("The search operation returned no matching results.")
    else:
        print(results)
    return results



search()
 
 
 
But the result is just 
[id: "100"
product {
name: "projects/295037490706/locations/global/catalogs/default_catalog/branches/1/products/100"
id: "1010"
product {
name: "projects/295037490706/locations/global/catalogs/default_catalog/branches/1/products/1100"
id: "11100"
product {
name: "projects/295037490706/locations/global/catalogs/default_catalog/branches/1/products/11100"
}
]



Could you please let me know what I missed?
Thank you in advance. @MarvinLlamas