这是与 Recommendations AI、Retail Search 和新的 Retail 控制台相关的文档。如需在受限 GA 阶段使用 Retail Search,请与 Cloud 销售人员联系

如果您仅使用 Recommendations AI,请保留在 Recommendations 控制台上并参阅 Recommendations AI 文档

对结果进行过滤和排序

本页介绍了如何使用 Retail Search 进行过滤和排序。

示例数据集

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

过滤条件

Java

public static SearchResponse searchFilteredProducts(String query, int pageSize,
    String filter) throws IOException, InterruptedException {
  SearchServiceClient searchClient = getSearchServiceClient();

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

  SearchResponse response = searchClient.search(searchRequest).getPage().getResponse();

  searchClient.shutdownNow();
  searchClient.awaitTermination(2, TimeUnit.SECONDS);

  return response;
}

过滤条件表达式语法可按以下 EBNF 总结:

  # A single expression or multiple expressions that are joint by "AND" or "OR".
  filter = expression, { " AND " | "OR", expression };

  # A expressions can be prefixed with "-" or "NOT" to express a negation.
  expression = [ "-" | "NOT " ],
                    # A parenthesized expression
                    | "(", expression, ")"
                    # A simple expression applying to a textual field.
                    # Function "ANY" returns true if the field contains any of the literals.
                    ( textual_field, ":", "ANY", "(", literal, { ",", literal }, ")"
                    # A simple expression applying to a numerical field. Function "IN" returns true
                    # if a field value is within the range. By default, lower_bound is inclusive and
                    # upper_bound is exclusive.
                    | numerical_field, ":", "IN", "(", lower_bound, ",", upper_bound, ")"
                    # A simple expression applying to a numerical field and compares with a double value.
                    | numerical_field, comparison, double );

  # A lower_bound is either a double, or "*" which represents negative infinity.
  # Specify inclusive bound with character 'i' for inclusive or exclusive bound
  # with character 'e', explicitly.
  lower_bound = ( double, [ "e" | "i" ] ) | "*";

  # An upper_bound is either a double, or "*" which represents infinity.
  # Specify inclusive bound with character 'i' for inclusive or exclusive bound
  # with character 'e', explicitly.
  upper_bound = ( double, [ "e" | "i" ] ) | "*";

  # Supported comparison operators.
  comparison = "<=" | "<" | ">=" | ">" | "=";

  # A literal is any double quoted string. You must escape backslash (\) and
  # quote (") characters.
  literal = double quoted string;

  textual_field = see the table below;

  numerical_field = see the table below;

下表汇总了可支持的文本字段。

field 说明
"productId" 产品 ID(Product.name 最后一段)。
"brands" Product.brands。
"categories" Product.categories。
"genders" Audience.genders。
"ageGroups" Audience.age_groups。
"availability" Product.availability。值为“IN_STOCK”、“OUT_OF_STOCK”、“PREORDER”或“BACKORDER”中的一个。
"colorFamilies" ColorInfo.color_families。
"colors" ColorInfo.colors。
"sizes" Product.sizes。
"materials" Product.materials。
"patterns" Product.patterns。
"conditions" Product.conditions。
"attributes.key" Product 对象中的文本自定义属性。如果属性值是文本,则该键可以是 Product.attributes 映射中的任意键。
"pickupInStore" 类型为“pickup-in-store”的 FulfillmentInfo.place_ids。
"shipToStore" 类型为“ship-to-store”的 FulfillmentInfo.place_ids。
"sameDayDelivery" 类型为“same-day-delivery”的 FulfillmentInfo.place_ids。
"nextDayDelivery" 类型为“next-day-delivery”的 FulfillmentInfo.place_ids。
"customFulfillment1" 类型为“custom-type-1”的 FulfillmentInfo.place_ids。
"customFulfillment2" 类型为“custom-type-2”的 FulfillmentInfo.place_ids。
"customFulfillment3" 类型为“custom-type-3”的 FulfillmentInfo.place_ids。
"customFulfillment4" 类型为“custom-type-4”的 FulfillmentInfo.place_ids。
"customFulfillment5" 类型为“custom-type-5”的 FulfillmentInfo.place_ids。
"inventory(place_id,attributes.key)" 商品目录中的文本自定义属性。

下表汇总了可支持的数字字段。

field 说明
"price" PriceInfo.price。
"discount" discount。Computed by (original_price - price) / original_price。
"rating" Rating.average_rating。
"ratingCount" Rating.rating_count。
"attributes.key" Product 对象中的数值自定属性。如果属性值为数值,则该键可以是 Product.attributes 映射中的任意键。
"inventory(place_id,price)" 商品目录价格。
"inventory(place_id,attributes.key)" 商品目录中的数值自定义属性。

“AND”最多连接 20 个表达式。“OR”所连接的简单表达式的数量上限为 10。“ANY”函数中的最大字面量为 100。

例如,如需分别搜索 Google 产品,您可以将 query 设置为“Google”,并将 filter 设置为下表中显示的值:

scenario filter
not a Pixel accessory "NOT categories: ANY(\"Pixel > featured accessories\")"
"cheaper than 100 dollars" "price: IN(*, 100.0e)"
"Nest speaker not cheaper than 80 dollars" "(categories: ANY(\"Nest > speakers and displays\")) AND (price: IN(80.0i, *))"

排序

Java

public static SearchResponse searchOrderedProducts(String query, int pageSize,
    String orderBy) throws IOException, InterruptedException {
  SearchServiceClient searchClient = getSearchServiceClient();

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

  SearchResponse response = searchClient.search(searchRequest).getPage().getResponse();

  searchClient.shutdownNow();
  searchClient.awaitTermination(2, TimeUnit.SECONDS);

  return response;
}

下表汇总了可支持的可订购字段。

field 说明
"productId" 产品 ID(Product.name 最后一段)。
"title" Product.title。
"brands" Product.brands。
"categories" Product.categories。
"genders" Audience.genders。
"ageGroups" Audience.age_groups。
"price" PriceInfo.price。
"discount" discount。Computed by (original_price - price) / price.
"rating" Rating.average_rating。
"ratingCount" Rating.rating_count。
"attributes.key" Product 对象中的自定义属性。键可以是 Product.attributes 映射中的任何键。
"inventory(place_id,price)" 商品目录价格。
"inventory(place_id,attributes.key)" 商品目录中的数值或文本自定义属性。

默认的排序顺序是升序。降序可由“desc”后缀指定,例如“rating desc”。

可以按优先级顺序使用以英文逗号分隔的字段来排序多个字段。优先级较低的字段将用于排序优先级较高的字段。例如,如果选择“rating desc, price”,则会按价格对具有相同评分的商品进行排序。