Skip to content

Commit aa17416

Browse files
t-karasovakweinmeisterpartheatkarasyovagcf-owl-bot[bot]
authored andcommitted
docs(samples): add retail search service samples (#133)
* feat: Retail Interactive Tutorials. Search service code samples * use default project for testing purposes * add pytest-xdist as a requirement for testing * fail on missing environment variable * for testing purposes * for testing purposes * for testing purposes * updated according to the review comments * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * try out GOOGLE_CLOUD_PROJECT_NUMBER instead of PROJECT_NUMBER * add BUCKET_NAME as an environment variable * adjust filepath so tests can run * tests are fixed * Update samples/interactive-tutorials/README.md Co-authored-by: Anthonios Partheniou <partheniou@google.com> * README file is updated * fix typo in pip install command Co-authored-by: Karl Weinmeister <11586922+kweinmeister@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <partheniou@google.com> Co-authored-by: t-karasova <tkarasyova@griddynamics.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 4bcffc3 commit aa17416

23 files changed

+1506
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Google Retail API: Python Code Samples
2+
3+
## Get started with the Google Cloud Retail API
4+
5+
The Retail API provides you with the following possibilities to:
6+
- Create and maintaining the catalog data.
7+
- Fine-tune the search configuration.
8+
- Import and maintain the user events data.
9+
10+
You can find the information about the Retail services in the [documentation](https://cloud.google.com/retail/docs)
11+
12+
<!--TODO(tkarasova) update the link to the tutorials when will be published-->
13+
If you would like to have a closer look at the Retail API features and try them yourself,
14+
the best option is to use the [Interactive Tutorials](https://cloud.google.com/retail/docs/overview). The tutorials will be launched in the CloudShell environment, and you will be able to request the Retail services and check the response with minimum time and effort.
15+
16+
The code samples in the directory **python-retail/samples/interactive-tutorials** are explicitly created for use with the Retail Interactive Tutorials.
17+
18+
If, for some reason, you have decided to proceed with these code samples without the tutorial, please go through the following steps and set up the required preconditions.
19+
20+
### Select your project and enable the Retail API
21+
22+
Google Cloud organizes resources into projects. This lets you
23+
collect all the related resources for a single application in one place.
24+
25+
If you don't have a Google Cloud project yet or you're not the owner of an existing one, you can
26+
[create a new project](https://console.cloud.google.com/projectcreate).
27+
28+
After the project is created, set your PROJECT_ID to a ```project``` variable.
29+
1. Run the following command in Terminal:
30+
```bash
31+
gcloud config set project <YOUR_PROJECT_ID>
32+
```
33+
34+
1. To check that the Retail API is enabled for your Project, go to the [Admin Console](https://console.cloud.google.com/ai/retail/).
35+
36+
### Create service account
37+
38+
To access the Retail API, you must create a service account.
39+
40+
1. To create a service account, follow this [instruction](https://cloud.google.com/retail/docs/setting-up#service-account)
41+
42+
1. Find your service account on the [IAM page](https://console.cloud.google.com/iam-admin/iam),
43+
click `Edit` icon, add the 'Storage Admin' and 'BigQuery Admin' roles. It may take some time for changes to apply.
44+
45+
1. Copy the service account email in the Principal field.
46+
47+
### Set up authentication
48+
49+
To run a code sample from the Cloud Shell, you need to be authenticated using the service account credentials.
50+
51+
1. Login with your user credentials.
52+
```bash
53+
gcloud auth login
54+
```
55+
56+
1. Type `Y` and press **Enter**. Click the link in a Terminal. A browser window should appear asking you to log in using your Gmail account.
57+
58+
1. Provide the Google Auth Library with access to your credentials and paste the code from the browser to the Terminal.
59+
60+
1. Upload your service account key JSON file and use it to activate the service account:
61+
62+
```bash
63+
gcloud iam service-accounts keys create ~/key.json --iam-account <YOUR_SERVICE_ACCOUNT_EMAIL>
64+
```
65+
66+
```bash
67+
gcloud auth activate-service-account --key-file ~/key.json
68+
```
69+
70+
1. To request the Retail API, set your service account key JSON file as the GOOGLE_APPLICATION_CREDENTIALS environment variable :
71+
```bash
72+
export GOOGLE_APPLICATION_CREDENTIALS=~/key.json
73+
```
74+
75+
### Set the PROJECT_NUMBER and PROJECT_ID environment variables
76+
77+
You will run the code samples in your own Google Cloud project. To use the **project_number** and **project_id** in every request to the Retail API, you should first specify them as environment variables.
78+
79+
1. Find the project number and project ID in the Project Info card displayed on **Home/Dashboard**.
80+
81+
1. Set the **project_number** with the following command:
82+
```bash
83+
export PROJECT_NUMBER=<YOUR_PROJECT_NUMBER>
84+
```
85+
1. Set the **project_id** with the following command:
86+
```bash
87+
export PROJECT_ID=<YOUR_PROJECT_ID>
88+
```
89+
90+
### Install Google Cloud Retail libraries
91+
92+
To run Python code samples for the Retail API tutorial, you need to set up your virtual environment.
93+
94+
1. Run the following commands in a Terminal to create an isolated Python environment:
95+
```bash
96+
pip install virtualenv
97+
virtualenv myenv
98+
source myenv/bin/activate
99+
```
100+
1. Next, install Google packages:
101+
```bash
102+
pip install google
103+
pip install google-cloud-retail
104+
pip install google-cloud-storage
105+
pip install google-cloud-bigquery
106+
107+
```
108+
109+
## Import Catalog Data
110+
111+
This step is required if this is the first Retail API Tutorial you run.
112+
Otherwise, you can skip it.
113+
114+
### Upload catalog data to Cloud Storage
115+
116+
There is a JSON file with valid products prepared in the `product` directory:
117+
`product/resources/products.json`.
118+
119+
Another file, `product/resources/products_some_invalid.json`, contains both valid and invalid products, and you will use it to check the error handling.
120+
121+
In your own project, create a Cloud Storage bucket and put the JSON file there.
122+
The bucket name must be unique. For convenience, you can name it `<YOUR_PROJECT_ID>_<TIMESTAMP>`.
123+
124+
1. To create the bucket and upload the JSON file, run the following command in the Terminal:
125+
126+
```bash
127+
python product/setup/create_gcs_bucket.py
128+
```
129+
130+
Now you can see the bucket is created in the [Cloud Storage](https://console.cloud.google.com/storage/browser), and the files are uploaded.
131+
132+
1. The name of the created Retail Search bucket is printed in the Terminal. Copy the name and set it as the environment variable `BUCKET_NAME`:
133+
134+
```bash
135+
export BUCKET_NAME=<YOUR_BUCKET_NAME>
136+
```
137+
138+
### Import products to the Retail Catalog
139+
140+
To import the prepared products to a catalog, run the following command in the Terminal:
141+
142+
```bash
143+
python product/import_products_gcs.py
144+
```
145+
146+
## Run your code sample
147+
148+
Run the sample in a terminal with the following command:
149+
```bash
150+
python search/search_with_boost_spec.py
151+
```

0 commit comments

Comments
 (0)