篩選及排序結果

本頁面說明如何使用搜尋功能篩選及排序。

語意篩選功能可最佳化非預設排序

AI Commerce Search 會顯示多種結果,包括熱門或趨勢商品,即使與搜尋字詞的關聯性不高。這項功能在預設搜尋 (依關聯性排序) 中很有效,但在使用 order_by 功能的非預設搜尋 (例如依價格排序) 中,關聯性較低的商品可能會因為價格較低而更顯眼。

為改善這個問題,AI Commerce Search 導入了語意嵌入式篩選功能,可從非預設搜尋結果中移除這些關聯性較低的項目,提升使用者體驗。

不過,這項預期行為可能會導致整體搜尋結果減少。停用篩選功能可能會影響搜尋結果品質。因此,如果您擔心搜尋結果減少,請與支援團隊聯絡。

教學課程:篩選功能

本教學課程會示範篩選功能。可讓您依據自己或客戶的需求精細調整搜尋要求。您可以按照一或多個欄位篩選,也可以按照文字和/或數值欄位篩選。您可以使用運算式語言為每個欄位建立述詞,或使用邏輯運算子合併不同的運算式。舉例來說,購物者想找鞋子時,可以使用篩選器縮小搜尋範圍,只顯示偏好的品牌和顏色。

教學課程:排序功能

這個教學課程會說明如何為搜尋回應中的項目排序。使用者在網站上搜尋產品時,系統會依據多個欄位排序結果。舉例來說,使用者正在搜尋價格和折扣最優惠的洋裝。價格和折扣是多個欄位。使用者會看到洋裝按價格排序,價格相同的洋裝則按折扣排序。

範例資料集

本頁面以下列資料集為例。範例中僅包含必要欄位。

篩選器

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;

下表匯總支援的文字欄位。

欄位 說明
「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" 產品材質。
「patterns」 Product.patterns。
「條件」 Product.conditions。
"attributes.key" Product 物件中的文字自訂屬性。如果屬性值為文字,鍵可以是 Product.attributes 對應中的任何鍵。
「pickupInStore」 「到店取貨」類型的 FulfillmentInfo.place_ids。
「shipToStore」 「送貨到店」類型的 FulfillmentInfo.place_ids。
"sameDayDelivery" 「當天到貨」類型的 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)" 商品目錄中的文字自訂屬性。

下表匯總支援的數值欄位。

欄位 說明
"price" PriceInfo.price。
「discount」 折扣。計算方式為 (original_price - price) / original_price。
"rating" Rating.average_rating。
"ratingCount" Rating.rating_count。
"attributes.key" 產品物件中的數值自訂屬性。如果屬性值為數值,鍵可以是 Product.attributes 對應中的任何鍵。
"inventory(place_id,price)" 商品目錄價格。
"inventory(place_id,original_price)" 原始商品目錄價格。
"inventory(place_id,attributes.key)" 商品目錄中的數值自訂屬性。

最多可有 10 個巢狀合取或析取。

舉例來說,如要分別在下列情況中搜尋 Google 產品,可以將 query 設為「Google」,並將 filter 設為下表所示的值:

情境 篩選器
非 Pixel 配件 "NOT categories: ANY(\"Pixel > featured accessories\")"
「低於 100 美元」 "price: IN(*, 100.0e)"
「Nest 音箱價格不低於 80 美元」 "(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);
  }
}

下表匯總支援的可排序欄位。

欄位 說明
「productId」 產品 ID (Product.name 的最後一個區隔)。
"title" Product.title。
「brands」 Product.brands。
"categories" Product.categories。
"genders" Audience.genders。
"ageGroups" Audience.age_groups。
"price" PriceInfo.price。
「discount」 折扣。計算方式為 (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」會依價格排序評分相同的商品。