对结果进行过滤和排序

本页介绍了如何使用搜索功能进行过滤和排序。

教程:过滤

本教程演示了过滤功能。它可让您根据自己或客户的需求微调搜索请求。您可以按单个或多个字段进行过滤,也可以按文本字段和/或数字字段进行过滤。您可以使用表达式语言为每个字段构造谓词,也可以使用逻辑运算符来组合不同的表达式。例如,正在寻找鞋子的买家可以使用过滤条件将搜索范围缩小到他们喜欢的品牌和颜色。


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

操作演示


教程:排序

本教程介绍了如何对搜索响应中的商品进行排序。当最终用户在您的网站上查找商品时,系统会显示按多个字段排序的结果。例如,用户正在搜索价格和折扣最优惠的连衣裙。此处为价格和折扣为多个字段。用户会看到按价格排序的连衣裙,以及价格相同的连衣裙(按折扣排序)。


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

操作演示


示例数据集

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

过滤

Java

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

public static void searchFilteredProducts(String query, int pageSize,
    String filter) throws IOException, InterruptedException {
  SearchRequest searchRequest = SearchRequest.newBuilder()
      .setPlacement(DEFAULT_SEARCH_PLACEMENT_NAME)
      .setBranch(DEFAULT_BRANCH_NAME)
      .setVisitorId(VISITOR_ID)
      .setQuery(query)
      .setPageSize(pageSize)
      .setFilter(filter)
      .build();

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

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

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

  # Expressions can be prefixed with "-" or "NOT" to express a negation.
  expression = [ "-" | "NOT " ],
    # A parenthetical expression.
    | "(", expression, ")"
    # A simple expression applying to a text field.
    # Function "ANY" returns true if the field contains any of the literals.
    ( text_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 that applies 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.
  # Explicitly specify inclusive bound with the character 'i' or exclusive bound
  # with the character 'e'.
  lower_bound = ( double, [ "e" | "i" ] ) | "*";

  # An upper_bound is either a double or "*", which represents infinity.
  # Explicitly specify inclusive bound with the character 'i' or exclusive bound
  # with the character 'e'.
  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;

  text_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,original_price)" 原始商品目录价格。
"inventory(place_id,attributes.key)" 商品目录中的数值自定义属性。

最多允许 10 个嵌套连词或析取运算。

例如,如需分别搜索 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

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

public static void searchOrderedProducts(String query, int pageSize,
    String orderBy) throws IOException, InterruptedException {
  SearchRequest searchRequest = SearchRequest.newBuilder()
      .setPlacement(DEFAULT_SEARCH_PLACEMENT_NAME)
      .setBranch(DEFAULT_BRANCH_NAME)
      .setVisitorId(VISITOR_ID)
      .setQuery(query)
      .setPageSize(pageSize)
      .setOrderBy(orderBy)
      .build();

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

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

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,original_price)" 原始商品目录价格。
"inventory(place_id,attributes.key)" 商品目录中的数值或文本自定义属性。

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

对于具有多个值的数字字段(例如重复字段或为多款式商品设置的字段),最小值用于按升序排序,最大值用于按降序排序。

可以按优先级顺序使用以英文逗号分隔的字段来排序多个字段。优先级较低的字段用于对优先级较高的字段具有相等值的项进行排序。例如,“rating desc, price”(分级 desc,价格)会按价格对具有相同评分的项进行排序。