결과 필터링 및 정렬

이 페이지에서는 검색을 사용한 필터링 및 순서 지정에 대해 설명합니다.

필터링 튜토리얼

이 튜토리얼에서는 필터링 기능을 설명합니다. 이를 통해 관리자는 사용자 또는 고객의 요구사항에 따라 검색 요청을 미세 조정할 수 있습니다. 단일 필드나 여러 필드별로 필터링하거나 텍스트 필드나 숫자 필드별로 또는 두 필드별로 필터링할 수 있습니다. 표현식 언어를 사용하여 각 필드의 조건자를 생성하거나 논리 연산자를 사용하여 여러 표현식을 결합할 수 있습니다. 예를 들어 구두를 찾고 있는 쇼핑객은 필터를 사용하여 선호하는 브랜드와 색상으로 검색 범위를 좁힐 수 있습니다.


Cloud Shell 편집기에서 이 태스크의 단계별 안내를 직접 수행하려면 둘러보기를 클릭합니다.

둘러보기


순서 지정 튜토리얼

이 튜토리얼에서는 검색 응답에서 항목의 순서를 지정하는 방법을 보여줍니다. 최종 사용자가 사이트에서 제품을 검색하면 여러 필드를 기준으로 정렬된 결과가 표시됩니다. 예를 들어 사용자가 가장 저렴하고 할인이 적용된 드레스를 검색할 수 있습니다. 여기서 여러 필드는 가격 및 할인입니다. 사용자는 가격별로 정렬된 드레스를 볼 수 있고 동일한 가격의 드레스에 대해서는 할인순으로 볼 수 있습니다.


Cloud Shell 편집기에서 이 태스크의 단계별 안내를 직접 수행하려면 둘러보기를 클릭합니다.

둘러보기


데이터 세트 예시

이 페이지에서는 다음 데이터 세트를 예시로 사용합니다. 예시에 필요한 필드만 포함됩니다.

필터

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' Product.materials입니다.
'patterns' Product.patterns입니다.
'conditions' Product.conditions입니다.
'attributes.key' 제품 객체의 텍스트 커스텀 속성입니다. 속성 값이 텍스트인 경우 키는 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)' 인벤토리의 텍스트 커스텀 속성입니다.

지원되는 숫자 필드는 다음 표에 요약되어 있습니다.

필드 설명
'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 액세서리가 아님 '카테고리가 아님: 전체(\'Pixel > 추천 액세서리\')'
'100 달러 미만' '가격: 인도(*, 100.0e)'
'80달러 미만의 Nest 스피커' '(카테고리: 전체(\'Nest > 스피커 및 디스플레이\')) 및 (가격: 인도(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.attributes 맵의 임의 키일 수 있습니다.
"inventory(place_id,price)" 인벤토리 가격입니다.
"inventory(place_id,original_price)" 원래 인벤토리 가격입니다.
'inventory(place_id,attributes.key)' 인벤토리의 숫자 또는 텍스트 커스텀 속성입니다.

기본적으로 순서는 오름차순입니다. 내림차순 순서는 'desc' 서픽스로 지정할 수 있습니다(예: 'rating desc').

여러 개의 값이 있는 숫자 필드(예: 반복되는 필드 또는 옵션이 있는 제품용으로 설정된 필드)의 경우 오름차순 정렬에 최솟값이 사용되고 최댓값이 내림차순 정렬에 사용됩니다.

여러 필드별 순서는 우선순위에 따라 쉼표로 구분된 필드를 사용하여 지원됩니다. 우선순위가 낮은 필드는 우선순위가 높은 필드에 대해 동일한 값으로 항목 순서를 지정하기 위해 사용됩니다. 예를 들어 'rating desc, price'는 가격 순위가 동일한 항목을 정렬합니다.