Adjust result size

This page provides an overview of query expansion.

Query expansion tutorial

This tutorial shows you how to enable the query expansion feature. When a shopper uses an ambiguous or a multi-word search phrase, they can get an empty response. After turning on query expansion, the request is analyzed and the expanded list of products based on the parsed search query gets returned.


To follow step-by-step guidance for this task directly in the Cloud Shell Editor, click Guide me:

Guide me


Example dataset

This page uses the following dataset as an example. Only the fields necessary for explanation are included.

Query expansion

Query expansion increases the recall for query terms with few results, especially long tail queries.

Java

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;

public static void searchProductsWithQueryExpansion(String query, int pageSize,
    Condition condition) throws IOException, InterruptedException {
  QueryExpansionSpec queryExpansionSpec = QueryExpansionSpec.newBuilder()
      .setCondition(condition)
      .build();

  SearchRequest searchRequest = SearchRequest.newBuilder()
      .setPlacement(DEFAULT_SEARCH_PLACEMENT_NAME)
      .setBranch(DEFAULT_BRANCH_NAME)
      .setVisitorId(VISITOR_ID)
      .setQuery(query)
      .setPageSize(pageSize)
      .setQueryExpansionSpec(queryExpansionSpec)
      .build();

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

For example, if you search "Google Pixel 5" without query expansion, you might only get "google_pixel_5" in the result. With query expansion, you might get "google_pixel_4a_with_5g", "google_pixel_4a" and "google_pixel_5_case" as well.