クエリ拡張

クエリ拡張とは、最初に結果が見つからない場合や結果が少なすぎる場合に、クエリ制約を段階的に緩和してより多くの結果を含めることです。これにより、クエリあたりの結果サイズが調整されます。

クエリに関連するドキュメントがまったくない場合、クエリで検索結果が 1 件も返されないように、クエリ拡張機能は関連性の低いドキュメントを返します。

クエリ拡張のチュートリアル

このチュートリアルでは、クエリ拡張機能を有効にする方法について説明します。買い物客があいまいなまたは複数の単語からなる検索フレーズを使用すると、空のレスポンスが返される場合があります。クエリ拡張を有効にすると、リクエストが分析され、解析された検索クエリに基づいて商品の拡張リストが返されます。


このタスクを Cloud Shell エディタで直接行う際のガイダンスについては、「ガイドを表示」をクリックしてください。

ガイドを表示


データセットの例

このページでは、次のデータセットを例として使用します。展開すると、サンプル商品紹介データセット内のフィールドが表示されます。

プロダクト データセットの例

ID title brands categories price_info.price
「nest_mini_2nd_gen」 「Nest Mini(第 2 世代)」 ["Google", "Nest"] ["Nest > speakers and displays"] 49.00
「nest_audio」 「Nest Audio」 ["Google", "Nest"] ["Nest > speakers and displays"] 99.99
「nest_hub_max」 「Nest Hub Max」 ["Google", "Nest"] ["Nest > speakers and displays"] 229.00
「nest_hub」 「Nest Hub」 ["Google", "Nest"] ["Nest > speakers and displays"] 88.99
「google_home_max」 「Google Home Max」 ["Google", "Nest"] ["Nest > speakers and displays"] 299.00
「google_home_mini」 「Google Home Mini」 ["Google", "Nest"] ["Nest > speakers and displays"] 49.00
「google_pixel_5」 「Google Pixel 5」 ["Google", "Pixel"] ["Pixel > phones"] 699.00
「5G 版 google_pixel_4a」 「5G 版 Google Pixel 4a」 ["Google", "Pixel"] ["Pixel > phones"] 499.00
「google_pixel_4a」 「Google Pixel 4a Phones」 ["Google", "Pixel"] ["Pixel > phones"] 349.00
「google_pixel_stand」 「Google Pixel Stand」 ["Google", "Pixel"] ["Pixel > featured accessories"] 79.00
「google_pixel_buds」 「Google Pixel Buds」 ["Google", "Pixel"] ["Pixel > featured accessories"] 179.00
「google_pixel_5_case」 「Google Pixel 5 ケース」 ["Google", "Pixel"] ["Pixel > featured accessories"] 40.00
「google_pixel_4a (5G)_case」 「Google Pixel 4a (5G) ケース」 ["Google", "Pixel"] ["Pixel > featured accessories"] 40.00
「google_pixel_4a_case」 「Google Pixel 4a ケース」 ["Google", "Pixel"] ["Pixel > featured accessories"] 40.00

クエリ拡張

クエリ拡張によって、特にロングテール クエリの場合は、結果が少ないクエリ キーワードの取り消しが増加します。

この検索機能は、クエリ拡張条件を決定する仕様によって駆動されます。デフォルトでオフになっている pinUnexpandedResults オプションが含まれています。true に設定すると、検索結果の上部に拡張されていない商品が表示されます。上位の結果の後に、展開された結果が表示されます。

Java

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

public static void searchProductsWithQueryExpansion(String query, int pageSize,
    Condition condition) throws IOException, InterruptedException {
  QueryExpansionSpec queryExpansionSpec = QueryExpansionSpec.newBuilder()
      .setCondition(condition)
      .build();

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

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

たとえば、クエリ拡張を使用せずに Google Pixel 5 を検索すると、結果は google_pixel_5 ID に限定されます。ただし、クエリ拡張を使用すると、商品紹介の例のデータセットgoogle_pixel_4a_with_5ggoogle_pixel_4agoogle_pixel_5_case の ID を取得することもできます。