提升成效

本页简要介绍了如何提升搜索结果。

教程:升位

本教程介绍了一些商品升位示例。


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

操作演示


示例数据集

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

前提条件

如需了解过滤器表达式语法,请参阅对结果进行过滤和排序

增强

借助提升功能,您可以通过应用提升来降低结果的优先级,从而控制结果排名。

例如,如果您搜索“Google speaker”,那么您会收到无序的“nest_mini_2nd_gen”、“nest_audio”、“nest_hub_max”、“nest_hub”、“google_home_max”和“google_home_mini”。

Java

import com.google.cloud.retail.v2.SearchRequest;
import com.google.cloud.retail.v2.SearchRequest.BoostSpec;
import com.google.cloud.retail.v2.SearchRequest.BoostSpec.ConditionBoostSpec;
import com.google.cloud.retail.v2.SearchResponse;
import com.google.cloud.retail.v2.SearchServiceClient;

public static void searchProductsWithBoostSpec(String query, int pageSize,
    String condition, float boostStrength) throws IOException, InterruptedException {
  BoostSpec boostSpec = BoostSpec.newBuilder()
      .addConditionBoostSpecs(ConditionBoostSpec.newBuilder()
          .setCondition(condition)
          .setBoost(boostStrength)
          .build())
      .build();

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

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

假设您想要优先使用较便宜的产品(低于 95 美元),并降低价格较高的产品(超过 95 美元)的优先级。您可以通过以下方式应用提升规范:

JSON

{
  condition_boost_specs {
    condition: "price: IN(*, 95.0e)"
    boost: 0.5
  }
  condition_boost_specs {
    condition: "price: IN(95.0e, *)"
    boost: -0.5
  }
}

在该结果中,“nest_mini_2nd_gen”“google_home_mini”和“nest_hub”可能是前三个,而“nest_audio”“nest_hub_max”和“google_home_max”可能是最后三个。但不保证具体的顺序,这与按价格排序不同,如过滤结果和排序结果中所述。