Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

docs(samples): Refactoring search package #415

Merged
merged 14 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ If you are using Maven, add this to your pom.xml file:
If you are using Gradle without BOM, add this to your dependencies

```Groovy
implementation 'com.google.cloud:google-cloud-retail:2.0.19'
implementation 'com.google.cloud:google-cloud-retail:2.3.0'
```

If you are using SBT, add this to your dependencies

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-retail" % "2.0.19"
libraryDependencies += "com.google.cloud" % "google-cloud-retail" % "2.3.0"
```

## Authentication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@
public class SearchSimpleQuery {

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

getSearchResponse(defaultSearchPlacementName);
searchResponse(defaultSearchPlacementName);
}

public static SearchResponse getSearchResponse(String defaultSearchPlacementName)
throws IOException {
public static void searchResponse(String defaultSearchPlacementName) throws IOException {
// TRY DIFFERENT QUERY PHRASES HERE:
String queryPhrase = "Hoodie";
String visitorId = UUID.randomUUID().toString();
Expand All @@ -58,11 +56,13 @@ public static SearchResponse getSearchResponse(String defaultSearchPlacementName
.build();
System.out.println("Search request: " + searchRequest);

// Initialize client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests. After
// completing all of your requests, call the "close" method on the client to
// safely clean up any remaining background resources.
try (SearchServiceClient client = SearchServiceClient.create()) {
SearchResponse searchResponse = client.search(searchRequest).getPage().getResponse();
System.out.println("Search response: " + searchResponse);

return searchResponse;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,15 @@
public class SearchWithBoostSpec {

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

getSearchResponse(defaultSearchPlacementName);
searchResponse(defaultSearchPlacementName);
}

public static SearchResponse getSearchResponse(String defaultSearchPlacementName)
throws IOException {
public static void searchResponse(String defaultSearchPlacementName) throws IOException {
// TRY DIFFERENT CONDITIONS HERE:
String searchQuery = "Tee";
String condition = "(colorFamilies: ANY(\"Blue\"))";
Expand All @@ -69,11 +67,13 @@ public static SearchResponse getSearchResponse(String defaultSearchPlacementName
.build();
System.out.println("Search request: " + searchRequest);

// Initialize client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests. After
// completing all of your requests, call the "close" method on the client to
// safely clean up any remaining background resources.
try (SearchServiceClient client = SearchServiceClient.create()) {
SearchResponse searchResponse = client.search(searchRequest).getPage().getResponse();
System.out.println("Search response: " + searchResponse);

return searchResponse;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,15 @@
public class SearchWithFacetSpec {

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

getSearchResponse(defaultSearchPlacementName);
searchResponse(defaultSearchPlacementName);
}

public static SearchResponse getSearchResponse(String defaultSearchPlacementName)
throws IOException {
public static void searchResponse(String defaultSearchPlacementName) throws IOException {
// TRY DIFFERENT CONDITIONS HERE:
String searchQuery = "Tee";
String facetKeyParam = "colorFamilies";
Expand All @@ -60,11 +58,13 @@ public static SearchResponse getSearchResponse(String defaultSearchPlacementName
.build();
System.out.println("Search request: " + searchRequest);

// Initialize client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests. After
// completing all of your requests, call the "close" method on the client to
// safely clean up any remaining background resources.
try (SearchServiceClient client = SearchServiceClient.create()) {
SearchResponse searchResponse = client.search(searchRequest).getPage().getResponse();
System.out.println("Search response: " + searchResponse);

return searchResponse;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@
public class SearchWithFiltering {

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

getSearchResponse(defaultSearchPlacementName);
searchResponse(defaultSearchPlacementName);
}

public static SearchResponse getSearchResponse(String defaultSearchPlacementName)
throws IOException {
public static void searchResponse(String defaultSearchPlacementName) throws IOException {
// TRY DIFFERENT FILTER EXPRESSIONS HERE:
String filter = "(colorFamilies: ANY(\"Black\"))";
String queryPhrase = "Tee";
Expand All @@ -61,11 +59,13 @@ public static SearchResponse getSearchResponse(String defaultSearchPlacementName

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

// Initialize client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests. After
// completing all of your requests, call the "close" method on the client to
// safely clean up any remaining background resources.
try (SearchServiceClient client = SearchServiceClient.create()) {
SearchResponse searchResponse = client.search(searchRequest).getPage().getResponse();
System.out.println("Search response: " + searchResponse);

return searchResponse;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@
public class SearchWithOrdering {

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

getSearchResponse(defaultSearchPlacementName);
searchResponse(defaultSearchPlacementName);
}

public static SearchResponse getSearchResponse(String defaultSearchPlacementName)
throws IOException {
public static void searchResponse(String defaultSearchPlacementName) throws IOException {
// TRY DIFFERENT ORDER BY EXPRESSION HERE:
String order = "price desc";
String queryPhrase = "Hoodie";
Expand All @@ -60,11 +58,13 @@ public static SearchResponse getSearchResponse(String defaultSearchPlacementName
.build();
System.out.println("Search request: " + searchRequest);

// Initialize client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests. After
// completing all of your requests, call the "close" method on the client to
// safely clean up any remaining background resources.
try (SearchServiceClient client = SearchServiceClient.create()) {
SearchResponse searchResponse = client.search(searchRequest).getPage().getResponse();
System.out.println("Search response: " + searchResponse);

return searchResponse;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,15 @@
public class SearchWithPagination {

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

getSearchResponse(defaultSearchPlacementName);
searchResponse(defaultSearchPlacementName);
}

public static SearchResponse getSearchResponse(String defaultSearchPlacementName)
throws IOException {
public static void searchResponse(String defaultSearchPlacementName) throws IOException {
// TRY DIFFERENT PAGINATION PARAMETERS HERE:
int pageSize = 6;
String queryPhrase = "Hoodie";
Expand All @@ -63,15 +61,17 @@ public static SearchResponse getSearchResponse(String defaultSearchPlacementName
.build();
System.out.println("Search request: " + searchRequest);

// Initialize client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests. After
// completing all of your requests, call the "close" method on the client to
// safely clean up any remaining background resources.
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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,15 @@
public class SearchWithQueryExpansionSpec {

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

getSearchResponse(defaultSearchPlacementName);
searchResponse(defaultSearchPlacementName);
}

public static SearchResponse getSearchResponse(String defaultSearchPlacementName)
throws IOException {
public static void searchResponse(String defaultSearchPlacementName) throws IOException {
// TRY DIFFERENT QUERY EXPANSION CONDITION HERE:
Condition condition = Condition.AUTO;
int pageSize = 10;
Expand All @@ -66,11 +64,13 @@ public static SearchResponse getSearchResponse(String defaultSearchPlacementName
.build();
System.out.println("Search request: " + searchRequest);

// Initialize client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests. After
// completing all of your requests, call the "close" method on the client to
// safely clean up any remaining background resources.
try (SearchServiceClient client = SearchServiceClient.create()) {
SearchResponse searchResponse = client.search(searchRequest).getPage().getResponse();
System.out.println("Search response: " + searchResponse);

return searchResponse;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package search;

import static com.google.common.truth.Truth.assertThat;
import static search.SearchSimpleQuery.getSearchResponse;
import static search.SearchSimpleQuery.searchResponse;

import com.google.cloud.ServiceOptions;
import java.io.ByteArrayOutputStream;
Expand All @@ -27,7 +27,10 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class SearchSimpleQueryTest {

private ByteArrayOutputStream bout;
Expand All @@ -44,7 +47,7 @@ public void setUp() throws IOException, InterruptedException, ExecutionException
originalPrintStream = System.out;
System.setOut(out);

getSearchResponse(defaultSearchPlacementName);
searchResponse(defaultSearchPlacementName);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package search;

import static com.google.common.truth.Truth.assertThat;
import static search.SearchWithBoostSpec.getSearchResponse;
import static search.SearchWithBoostSpec.searchResponse;

import com.google.cloud.ServiceOptions;
import java.io.ByteArrayOutputStream;
Expand All @@ -27,7 +27,10 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class SearchWithBoostSpecTest {
private ByteArrayOutputStream bout;
private PrintStream originalPrintStream;
Expand All @@ -43,7 +46,7 @@ public void setUp() throws IOException, InterruptedException, ExecutionException
originalPrintStream = System.out;
System.setOut(out);

getSearchResponse(defaultSearchPlacementName);
searchResponse(defaultSearchPlacementName);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package search;

import static com.google.common.truth.Truth.assertThat;
import static search.SearchWithFacetSpec.getSearchResponse;
import static search.SearchWithFacetSpec.searchResponse;

import com.google.cloud.ServiceOptions;
import java.io.ByteArrayOutputStream;
Expand All @@ -27,7 +27,10 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class SearchWithFacetSpecTest {
private ByteArrayOutputStream bout;
private PrintStream originalPrintStream;
Expand All @@ -43,7 +46,7 @@ public void setUp() throws IOException, InterruptedException, ExecutionException
originalPrintStream = System.out;
System.setOut(out);

getSearchResponse(defaultSearchPlacementName);
searchResponse(defaultSearchPlacementName);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package search;

import static com.google.common.truth.Truth.assertThat;
import static search.SearchSimpleQuery.getSearchResponse;
import static search.SearchWithFiltering.searchResponse;

import com.google.cloud.ServiceOptions;
import java.io.ByteArrayOutputStream;
Expand All @@ -27,7 +27,10 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class SearchWithFilteringTest {

private ByteArrayOutputStream bout;
Expand All @@ -44,7 +47,7 @@ public void setUp() throws IOException, InterruptedException, ExecutionException
originalPrintStream = System.out;
System.setOut(out);

getSearchResponse(defaultSearchPlacementName);
searchResponse(defaultSearchPlacementName);
}

@Test
Expand Down
Loading