Skip to content

Commit

Permalink
chore(samples): Code samples for Retail Tutorials. Search service sam…
Browse files Browse the repository at this point in the history
…ples (part 2) (#290)

* Configure modules settings.

* Add search tutorials.

* 🦉 Updates from OwlBot

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* Minor fixes.

* 🦉 Updates from OwlBot

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* Replace PROJECT_NUMBER with PROJECT_ID

* Minor fixes.

* 🦉 Updates from OwlBot

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* kokoro config files are added

* Minor fixes.

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: tetiana-karasova <tetiana.karasova@gmail.com>
Co-authored-by: Neenu Shaji <Neenu1995@users.noreply.github.com>
  • Loading branch information
4 people authored and Shabirmean committed Nov 15, 2022
1 parent 03e9c79 commit a49c7ec
Show file tree
Hide file tree
Showing 10 changed files with 670 additions and 0 deletions.
69 changes: 69 additions & 0 deletions retail/interactive-tutorials/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.cloud</groupId>
<artifactId>retail-interactive-tutorials</artifactId>
<packaging>jar</packaging>
<name>Google Cloud Retail Interactive Tutorials</name>
<url>https://github.com/googleapis/java-retail</url>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
</parent>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-retail</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigquery</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2022 Google LLC
*
* 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.
*/

// [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.
*/

package search;

import com.google.cloud.retail.v2.SearchRequest;
import com.google.cloud.retail.v2.SearchResponse;
import com.google.cloud.retail.v2.SearchServiceClient;
import java.io.IOException;
import java.util.UUID;

public class SearchWithFiltering {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = System.getenv("PROJECT_ID");
String defaultCatalogName =
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
String defaultSearchPlacementName = defaultCatalogName + "/placements/default_search";

getSearchResponse(defaultSearchPlacementName);
}

public static SearchResponse getSearchResponse(String defaultSearchPlacementName)
throws IOException {
// TRY DIFFERENT FILTER EXPRESSIONS HERE:
String filter = "(colorFamilies: ANY(\"Black\"))";
String queryPhrase = "Tee";
int pageSize = 10;
String visitorId = UUID.randomUUID().toString();

SearchRequest searchRequest =
SearchRequest.newBuilder()
.setPlacement(defaultSearchPlacementName)
.setVisitorId(visitorId)
.setQuery(queryPhrase)
.setPageSize(pageSize)
.setFilter(filter)
.build();

System.out.println("Search request: " + searchRequest);

try (SearchServiceClient client = SearchServiceClient.create()) {
SearchResponse searchResponse = client.search(searchRequest).getPage().getResponse();
System.out.println("Search response: " + searchResponse);

return searchResponse;
}
}
}

// [END retail_search_for_products_with_filter]
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2022 Google LLC
*
* 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.
*/

// [START retail_search_for_products_with_ordering]

/*
* Call Retail API to search for a products in a catalog,
* order the results by different product fields.
*/

package search;

import com.google.cloud.retail.v2.SearchRequest;
import com.google.cloud.retail.v2.SearchResponse;
import com.google.cloud.retail.v2.SearchServiceClient;
import java.io.IOException;
import java.util.UUID;

public class SearchWithOrdering {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = System.getenv("PROJECT_ID");
String defaultCatalogName =
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
String defaultSearchPlacementName = defaultCatalogName + "/placements/default_search";

getSearchResponse(defaultSearchPlacementName);
}

public static SearchResponse getSearchResponse(String defaultSearchPlacementName)
throws IOException {
// TRY DIFFERENT ORDER BY EXPRESSION HERE:
String order = "price desc";
String queryPhrase = "Hoodie";
int pageSize = 10;
String visitorId = UUID.randomUUID().toString();

SearchRequest searchRequest =
SearchRequest.newBuilder()
.setPlacement(defaultSearchPlacementName)
.setQuery(queryPhrase)
.setOrderBy(order)
.setVisitorId(visitorId)
.setPageSize(pageSize)
.build();
System.out.println("Search request: " + searchRequest);

try (SearchServiceClient client = SearchServiceClient.create()) {
SearchResponse searchResponse = client.search(searchRequest).getPage().getResponse();
System.out.println("Search response: " + searchResponse);

return searchResponse;
}
}
}

// [END retail_search_for_products_with_ordering]
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2022 Google LLC
*
* 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.
*/

// [START retail_search_for_products_with_page_size]

/*
* Call Retail API to search for a products in a catalog,
* limit the number of the products per page and go to the next page
* using "next_page_token" or jump to chosen page using "offset".
*/

package search;

import com.google.cloud.retail.v2.SearchRequest;
import com.google.cloud.retail.v2.SearchResponse;
import com.google.cloud.retail.v2.SearchServiceClient;
import java.io.IOException;
import java.util.UUID;

public class SearchWithPagination {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = System.getenv("PROJECT_ID");
String defaultCatalogName =
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
String defaultSearchPlacementName = defaultCatalogName + "/placements/default_search";

getSearchResponse(defaultSearchPlacementName);
}

public static SearchResponse getSearchResponse(String defaultSearchPlacementName)
throws IOException {
// TRY DIFFERENT PAGINATION PARAMETERS HERE:
int pageSize = 6;
String queryPhrase = "Hoodie";
int offset = 0;
String pageToken = "";
String visitorId = UUID.randomUUID().toString();

SearchRequest searchRequest =
SearchRequest.newBuilder()
.setPlacement(defaultSearchPlacementName)
.setVisitorId(visitorId)
.setQuery(queryPhrase)
.setPageSize(pageSize)
.setOffset(offset)
.setPageToken(pageToken)
.build();
System.out.println("Search request: " + searchRequest);

try (SearchServiceClient client = SearchServiceClient.create()) {
SearchResponse searchResponseFirstPage = client.search(searchRequest).getPage().getResponse();
System.out.println("Search response: " + searchResponseFirstPage);

// PASTE CALL WITH NEXT PAGE TOKEN HERE:

// PASTE CALL WITH OFFSET HERE:

return searchResponseFirstPage;
}
}
}

// [END retail_search_for_products_with_page_size]
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2022 Google LLC
*
* 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.
*/

// [START retail_search_for_products_with_query_expansion_specification]

/*
* Call Retail API to search for a products in a catalog,
* enabling the query expansion feature to let the Google Retail Search
* build an automatic query expansion.
*/

package search;

import com.google.cloud.retail.v2.SearchRequest;
import com.google.cloud.retail.v2.SearchRequest.QueryExpansionSpec;
import com.google.cloud.retail.v2.SearchRequest.QueryExpansionSpec.Condition;
import com.google.cloud.retail.v2.SearchResponse;
import com.google.cloud.retail.v2.SearchServiceClient;
import java.io.IOException;
import java.util.UUID;

public class SearchWithQueryExpansionSpec {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = System.getenv("PROJECT_ID");
String defaultCatalogName =
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
String defaultSearchPlacementName = defaultCatalogName + "/placements/default_search";

getSearchResponse(defaultSearchPlacementName);
}

public static SearchResponse getSearchResponse(String defaultSearchPlacementName)
throws IOException {
// TRY DIFFERENT QUERY EXPANSION CONDITION HERE:
Condition condition = Condition.AUTO;
int pageSize = 10;
String queryPhrase = "Google Youth Hero Tee Grey";
String visitorId = UUID.randomUUID().toString();

QueryExpansionSpec queryExpansionSpec =
QueryExpansionSpec.newBuilder().setCondition(condition).build();

SearchRequest searchRequest =
SearchRequest.newBuilder()
.setPlacement(defaultSearchPlacementName)
.setQuery(queryPhrase)
.setVisitorId(visitorId)
.setQueryExpansionSpec(queryExpansionSpec)
.setPageSize(pageSize)
.build();
System.out.println("Search request: " + searchRequest);

try (SearchServiceClient client = SearchServiceClient.create()) {
SearchResponse searchResponse = client.search(searchRequest).getPage().getResponse();
System.out.println("Search response: " + searchResponse);

return searchResponse;
}
}
}

// [END retail_search_for_products_with_query_expansion_specification]
Loading

0 comments on commit a49c7ec

Please sign in to comment.