-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: interactive tutorials code samples for search service (#146)
- Loading branch information
1 parent
432c6f0
commit 1173bb0
Showing
15 changed files
with
1,400 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
## Get started with Google Cloud Retail | ||
|
||
### Select your project and enable the Retail API | ||
|
||
Google Cloud organizes resources into projects. This lets you | ||
collect all the related resources for a single application in one place. | ||
|
||
If you don't have a Google Cloud project yet or you're not the Owner of an existing one, you can | ||
[create a new project](https://console.cloud.google.com/projectcreate). | ||
|
||
After the project is created, set your PROJECT_ID to a ```project``` variable. | ||
1. Run the following command in Terminal: | ||
```bash | ||
gcloud config set project <YOUR_PROJECT_ID> | ||
``` | ||
|
||
1. Check that the Retail API is enabled for your Project in the [Admin Console](https://console.cloud.google.com/ai/retail/). | ||
|
||
### Set up authentication | ||
|
||
To run a code sample from the Cloud Shell, you need to authenticate. To do this, use the Application Default Credentials. | ||
|
||
1. Set your user credentials to authenticate your requests to the Retail API | ||
|
||
```bash | ||
gcloud auth application-default login | ||
``` | ||
|
||
1. Type `Y` and press **Enter**. Click the link in Terminal. A browser window should appear asking you to log in using your Google account. | ||
|
||
1. Provide the Google Auth Library with access to your credentials and paste the code from the browser to the Terminal. | ||
|
||
1. Run the code sample and check the Retail API in action. | ||
|
||
**Note**: Click the copy button on the side of the code box to paste the command in the Cloud Shell terminal and run it. | ||
|
||
### Set the GCLOUD_PROJECT environment variable | ||
|
||
Because you are going to run the code samples in your own Google Cloud project, you should specify the **project_number** as an environment variable. It will be used in every request to the Retail API. | ||
|
||
1. You can find the ```project_number``` in the **Home/Dashboard/Project Info card**. | ||
|
||
1. Set the environment variable with the following command: | ||
```bash | ||
export GCLOUD_PROJECT=<YOUR_GCLOUD_PROJECT> | ||
``` | ||
|
||
### Install Google Cloud Retail libraries | ||
|
||
To install all the dependencies, run | ||
|
||
``` | ||
cd cloudshell_open/retail-search-nodejs-samples | ||
npm install | ||
``` | ||
|
||
### Running the code samples | ||
Samples are in the `search/`, `products/`, `events/` directories. | ||
To execute an individual code sample, invoke `node` with a file as a parameter at the command line prompt, e.g.: | ||
|
||
``` | ||
node search/search-simple-query.js | ||
``` |
76 changes: 76 additions & 0 deletions
76
retail/interactive-tutorials/search/search-simple-query.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2022 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
'use strict'; | ||
|
||
async function main() { | ||
// [START retail_search_for_products_with_query_parameter] | ||
// Call Retail API to search for a products in a catalog using only search query. | ||
|
||
// Imports the Google Cloud client library. | ||
const {SearchServiceClient} = require('@google-cloud/retail'); | ||
|
||
const projectNumber = process.env['GCLOUD_PROJECT']; | ||
|
||
// Placement is used to identify the Serving Config name. | ||
const placement = `projects/${projectNumber}/locations/global/catalogs/default_catalog/placements/default_search`; | ||
|
||
// Raw search query. | ||
const query = 'Hoodie'; //TRY DIFFERENT QUERY PHRASES | ||
|
||
// A unique identifier for tracking visitors. | ||
const visitorId = '12345'; | ||
|
||
// Maximum number of Products to return. | ||
const pageSize = 10; | ||
|
||
// Instantiates a client. | ||
const retailClient = new SearchServiceClient(); | ||
|
||
const IResponseParams = { | ||
ISearchResult: 0, | ||
ISearchRequest: 1, | ||
ISearchResponse: 2, | ||
}; | ||
|
||
const callSearch = async () => { | ||
console.log('Search start'); | ||
// Construct request | ||
const request = { | ||
placement, | ||
query, | ||
visitorId, | ||
pageSize, | ||
}; | ||
console.log('Search request: ', request); | ||
|
||
// Run request | ||
const response = await retailClient.search(request, { | ||
autoPaginate: false, | ||
}); | ||
const searchResponse = response[IResponseParams.ISearchResponse]; | ||
console.log('Search result: ', JSON.stringify(searchResponse, null, 4)); | ||
console.log('Search end'); | ||
}; | ||
|
||
callSearch(); | ||
// [END retail_search_for_products_with_query_parameter] | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
|
||
main(); |
89 changes: 89 additions & 0 deletions
89
retail/interactive-tutorials/search/search-with-boost-spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2022 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
'use strict'; | ||
|
||
async function main() { | ||
// [START retail_search_product_with_boost_spec] | ||
// Call Retail API to search for a products in a catalog, rerank the | ||
// results boosting or burying the products that match defined condition. | ||
|
||
// Imports the Google Cloud client library. | ||
const {SearchServiceClient} = require('@google-cloud/retail'); | ||
|
||
const projectNumber = process.env['GCLOUD_PROJECT']; | ||
|
||
// Placement is used to identify the Serving Config name. | ||
const placement = `projects/${projectNumber}/locations/global/catalogs/default_catalog/placements/default_search`; | ||
|
||
// Raw search query. | ||
const query = 'Hoodie'; | ||
|
||
// A unique identifier for tracking visitors. | ||
const visitorId = '12345'; | ||
|
||
//Boost specification to boost certain products. | ||
const boostSpec = { | ||
conditionBoostSpecs: [ | ||
{ | ||
condition: '(colorFamilies: ANY("Blue"))', // TRY OTHER CONDITIONS | ||
boost: 0.0, // TRY DIFFERENT SCORES | ||
}, | ||
], | ||
}; | ||
|
||
// Maximum number of Products to return. | ||
const pageSize = 10; | ||
|
||
// Instantiates a client | ||
const retailClient = new SearchServiceClient(); | ||
|
||
const IResponseParams = { | ||
ISearchResult: 0, | ||
ISearchRequest: 1, | ||
ISearchResponse: 2, | ||
}; | ||
|
||
const callSearch = async () => { | ||
console.log('Search start'); | ||
// Construct request | ||
const request = { | ||
placement, | ||
query, | ||
visitorId, | ||
boostSpec, | ||
pageSize, | ||
}; | ||
|
||
console.log('Search request: ', request); | ||
|
||
// Run request | ||
const response = await retailClient.search(request, { | ||
autoPaginate: false, | ||
}); | ||
const searchResponse = response[IResponseParams.ISearchResponse]; | ||
console.log('Search result: ', JSON.stringify(searchResponse, null, 4)); | ||
console.log('Search end'); | ||
}; | ||
|
||
callSearch(); | ||
// [END retail_search_product_with_boost_spec] | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
|
||
main(); |
80 changes: 80 additions & 0 deletions
80
retail/interactive-tutorials/search/search-with-facet-spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2022 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
'use strict'; | ||
|
||
async function main() { | ||
// [START retail_search_products_with_facet_spec] | ||
// Call Retail API to search for a products in a catalog using only search query. | ||
|
||
// Imports the Google Cloud client library. | ||
const {SearchServiceClient} = require('@google-cloud/retail'); | ||
|
||
const projectNumber = process.env['GCLOUD_PROJECT']; | ||
|
||
// Placement is used to identify the Serving Config name. | ||
const placement = `projects/${projectNumber}/locations/global/catalogs/default_catalog/placements/default_search`; | ||
|
||
// Raw search query. | ||
const query = 'Tee'; | ||
|
||
// A unique identifier for tracking visitors. | ||
const visitorId = '12345'; | ||
|
||
//Facet specifications for faceted search. | ||
const facetSpecs = [{facetKey: {key: 'colorFamilies'}}]; //PUT THE INTERVALS HERE | ||
|
||
// Maximum number of Products to return. | ||
const pageSize = 10; | ||
|
||
// Instantiates a client. | ||
const retailClient = new SearchServiceClient(); | ||
|
||
const IResponseParams = { | ||
ISearchResult: 0, | ||
ISearchRequest: 1, | ||
ISearchResponse: 2, | ||
}; | ||
|
||
const callSearch = async () => { | ||
console.log('Search start'); | ||
// Construct request | ||
const request = { | ||
placement, | ||
query, | ||
visitorId, | ||
facetSpecs, | ||
pageSize, | ||
}; | ||
console.log('Search request: ', request); | ||
|
||
// Run request | ||
const response = await retailClient.search(request, { | ||
autoPaginate: false, | ||
}); | ||
const searchResponse = response[IResponseParams.ISearchResponse]; | ||
console.log('Search result: ', JSON.stringify(searchResponse, null, 4)); | ||
console.log('Search end'); | ||
}; | ||
|
||
callSearch(); | ||
// [END retail_search_products_with_facet_spec] | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
|
||
main(); |
82 changes: 82 additions & 0 deletions
82
retail/interactive-tutorials/search/search-with-filtering.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2022 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
'use strict'; | ||
|
||
async function main() { | ||
// [START retail_search_for_products_with_filter] | ||
// Call Retail API to search for a products in a catalog, filter the results by different product fields. | ||
|
||
// Imports the Google Cloud client library. | ||
const {SearchServiceClient} = require('@google-cloud/retail'); | ||
|
||
const projectNumber = process.env['GCLOUD_PROJECT']; | ||
|
||
// Placement is used to identify the Serving Config name. | ||
const placement = `projects/${projectNumber}/locations/global/catalogs/default_catalog/placements/default_search`; | ||
|
||
// Raw search query. | ||
const query = 'Tee'; | ||
|
||
// A unique identifier for tracking visitors. | ||
const visitorId = '12345'; | ||
|
||
// The filter syntax consists of an expression language for constructing a | ||
// predicate from one or more fields of the products being filtered. | ||
const filter = '(colorFamilies: ANY("Black"))'; // TRY DIFFERENT FILTER EXPRESSIONS | ||
|
||
// Maximum number of Products to return. | ||
const pageSize = 10; | ||
|
||
// Instantiates a client. | ||
const retailClient = new SearchServiceClient(); | ||
|
||
const IResponseParams = { | ||
ISearchResult: 0, | ||
ISearchRequest: 1, | ||
ISearchResponse: 2, | ||
}; | ||
|
||
const callSearch = async () => { | ||
console.log('Search start'); | ||
// Construct request | ||
const request = { | ||
placement, | ||
query, | ||
visitorId, | ||
filter, | ||
pageSize, | ||
}; | ||
|
||
console.log('Search request: ', request); | ||
|
||
// Run request | ||
const response = await retailClient.search(request, { | ||
autoPaginate: false, | ||
}); | ||
const searchResponse = response[IResponseParams.ISearchResponse]; | ||
console.log('Search result: ', JSON.stringify(searchResponse, null, 4)); | ||
console.log('Search end'); | ||
}; | ||
|
||
callSearch(); | ||
// [END retail_search_for_products_with_filter] | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
|
||
main(); |
Oops, something went wrong.