调整结果大小

本页面简要介绍了查询扩展。

教程:查询扩展

本教程介绍了如何启用查询扩展功能。买家使用含糊不清或包含多个字词的搜索词组时,可能会收到空响应。启用查询扩展后,系统会对请求进行分析,并返回根据解析的搜索查询展开的商品列表。


如需在 Cloud Shell Editor 中直接遵循有关此任务的分步指导,请点击操作演示

操作演示


示例数据集

本页以下面的数据集为例。仅包含说明所需的字段。

查询扩展

查询扩展会增加查询字词的召回率,但结果很少,尤其是长尾查询。

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);
  }
}

例如,如果您在未进行查询扩展的情况下搜索“Google Pixel 5”,您可能只会在结果中看到“google_pixel_5”。而使用查询扩展时,您可能会收到“google_pixel_4a_with_5g”、“google_pixel_4a”和“google_pixel_5_case”。