|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// [START retail_search_for_products_with_query_expansion_specification] |
| 18 | + |
| 19 | +/* |
| 20 | + * Call Retail API to search for a products in a catalog, |
| 21 | + * enabling the query expansion feature to let the Google Retail Search |
| 22 | + * build an automatic query expansion. |
| 23 | + */ |
| 24 | + |
| 25 | +package search; |
| 26 | + |
| 27 | +import com.google.cloud.retail.v2.SearchRequest; |
| 28 | +import com.google.cloud.retail.v2.SearchRequest.QueryExpansionSpec; |
| 29 | +import com.google.cloud.retail.v2.SearchRequest.QueryExpansionSpec.Condition; |
| 30 | +import com.google.cloud.retail.v2.SearchResponse; |
| 31 | +import com.google.cloud.retail.v2.SearchServiceClient; |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.UUID; |
| 34 | + |
| 35 | +public class SearchWithQueryExpansionSpec { |
| 36 | + |
| 37 | + public static void main(String[] args) throws IOException { |
| 38 | + // TODO(developer): Replace these variables before running the sample. |
| 39 | + String projectId = System.getenv("PROJECT_ID"); |
| 40 | + String defaultCatalogName = |
| 41 | + String.format("projects/%s/locations/global/catalogs/default_catalog", projectId); |
| 42 | + String defaultSearchPlacementName = defaultCatalogName + "/placements/default_search"; |
| 43 | + |
| 44 | + getSearchResponse(defaultSearchPlacementName); |
| 45 | + } |
| 46 | + |
| 47 | + public static SearchResponse getSearchResponse(String defaultSearchPlacementName) |
| 48 | + throws IOException { |
| 49 | + // TRY DIFFERENT QUERY EXPANSION CONDITION HERE: |
| 50 | + Condition condition = Condition.AUTO; |
| 51 | + int pageSize = 10; |
| 52 | + String queryPhrase = "Google Youth Hero Tee Grey"; |
| 53 | + String visitorId = UUID.randomUUID().toString(); |
| 54 | + |
| 55 | + QueryExpansionSpec queryExpansionSpec = |
| 56 | + QueryExpansionSpec.newBuilder().setCondition(condition).build(); |
| 57 | + |
| 58 | + SearchRequest searchRequest = |
| 59 | + SearchRequest.newBuilder() |
| 60 | + .setPlacement(defaultSearchPlacementName) |
| 61 | + .setQuery(queryPhrase) |
| 62 | + .setVisitorId(visitorId) |
| 63 | + .setQueryExpansionSpec(queryExpansionSpec) |
| 64 | + .setPageSize(pageSize) |
| 65 | + .build(); |
| 66 | + System.out.println("Search request: " + searchRequest); |
| 67 | + |
| 68 | + try (SearchServiceClient client = SearchServiceClient.create()) { |
| 69 | + SearchResponse searchResponse = client.search(searchRequest).getPage().getResponse(); |
| 70 | + System.out.println("Search response: " + searchResponse); |
| 71 | + |
| 72 | + return searchResponse; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// [END retail_search_for_products_with_query_expansion_specification] |
0 commit comments