查询扩展

查询扩展是指在最初未找到或找到的结果太少时,逐步放宽查询限制以纳入更多结果。这会导致调整每次查询的结果大小。

如果查询根本没有相关文档,查询扩展功能会返回相关程度较低的文档,以确保查询不会返回零个搜索结果。

教程:查询扩展

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


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

操作演示


示例数据集

本页以下面的数据集为例。展开该数据集,查看其中的字段。

商品数据集示例

ID title brands categories price_info.price
"nest_mini_2nd_gen" "Nest Mini (2nd gen)" ["Google", "Nest"] ["Nest > speakers and displays"] 49.00
"nest_audio" "Nest Audio" ["Google", "Nest"] ["Nest > speakers and displays"] 99.99
"nest_hub_max" "Nest Hub Max" ["Google", "Nest"] ["Nest > speakers and displays"] 229.00
"nest_hub" "Nest Hub" ["Google", "Nest"] ["Nest > speakers and displays"] 88.99
"google_home_max" "Google Home Max" ["Google", "Nest"] ["Nest > speakers and displays"] 299.00
"google_home_mini" "Google Home Mini" ["Google", "Nest"] ["Nest > speakers and displays"] 49.00
"google_pixel_5" "Google Pixel 5" ["Google", "Pixel"] ["Pixel > phones"] 699.00
"google_pixel_4a_with_5g" "Google Pixel 4a with 5G" ["Google", "Pixel"] ["Pixel > phones"] 499.00
"google_pixel_4a" "Google Pixel 4a Phones" ["Google", "Pixel"] ["Pixel > phones"] 349.00
"google_pixel_stand" "Google Pixel Stand" ["Google", "Pixel"] ["Pixel > featured accessories"] 79.00
"google_pixel_buds" "Google Pixel Buds" ["Google", "Pixel"] ["Pixel > featured accessories"] 179.00
"google_pixel_5_case" "Google Pixel 5 Case" ["Google", "Pixel"] ["Pixel > featured accessories"] 40.00
"google_pixel_4a_5g_case" "Google Pixel 4a (5G) Case" ["Google", "Pixel"] ["Pixel > featured accessories"] 40.00
"google_pixel_4a_case" "Google Pixel 4a Case" ["Google", "Pixel"] ["Pixel > featured accessories"] 40.00

查询扩展

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

此搜索功能由确定查询扩展条件的规范驱动。它包含一个 pinUnexpandedResults 选项,该选项默认处于关闭状态。如果设置为 true,则在搜索结果顶部显示未使用扩展功能的商品。顶部是展开的搜索结果。

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 ID。不过,通过查询扩展,您可能还会在示例商品说明数据集中获得 google_pixel_4a_with_5ggoogle_pixel_4agoogle_pixel_5_case ID。