ベクトル検索を使用してエンベディングを検索する

このチュートリアルでは、VECTOR_SEARCH 関数と、必要に応じてベクトル インデックスを使用して、BigQuery テーブルに保存されているエンベディングに対して類似検索を実行する方法について説明します。

ベクトル インデックスで VECTOR_SEARCH を使用すると、VECTOR_SEARCH近似最近傍メソッドを使用してベクトル検索のパフォーマンスを向上させますが、再現率が低下するというトレードオフが伴います。そうすることで、より近似的な結果が返されます。ベクトル インデックスがない場合、VECTOR_SEARCHブルート フォース検索を使用して、すべてのレコードの距離を測定します。

必要な権限

このチュートリアルを実行するには、次の Identity and Access Management(IAM)権限が必要です。

  • データセットを作成するには、bigquery.datasets.create 権限が必要です。
  • テーブルを作成するには、次の権限が必要です。

    • bigquery.tables.create
    • bigquery.tables.updateData
    • bigquery.jobs.create
  • ベクトル インデックスを作成するには、インデックスを作成するテーブルに対する bigquery.tables.createIndex 権限が必要です。

  • ベクトル インデックスを破棄するには、インデックスを破棄するテーブルに対する bigquery.tables.deleteIndex 権限が必要です。

次の IAM 事前定義ロールには、ベクトル インデックスの操作に必要な権限が含まれています。

  • BigQuery データオーナー(roles/bigquery.dataOwner
  • BigQuery データ編集者(roles/bigquery.dataEditor

費用

VECTOR_SEARCH 関数では、BigQuery コンピューティングの料金が適用されます。類似性検索には、オンデマンド料金またはエディション料金が適用されます。

  • オンデマンド: ベーステーブル、インデックス、検索クエリでスキャンされたバイト数に対して課金されます。
  • エディションの料金: 予約エディション内でジョブを完了するために必要なスロットに対して課金されます。類似性計算の規模が大きく、複雑になるほど、料金が高くなります。

詳細については、BigQuery の料金をご覧ください。

始める前に

  1. In the Trusted Cloud console, on the project selector page, select or create a Trusted Cloud project.

    Go to project selector

  2. Verify that billing is enabled for your Trusted Cloud project.

  3. Enable the BigQuery API.

    Enable the API

データセットを作成する

BigQuery データセットを作成します。

  1. Trusted Cloud コンソールで、[BigQuery] ページに移動します。

    [BigQuery] ページに移動

  2. [エクスプローラ] ペインで、プロジェクト名をクリックします。

  3. 「アクションを表示」> [データセットを作成] をクリックします。

    チュートリアルで使用するオブジェクトを格納するデータセットを作成する。

  4. [データセットを作成する] ページで、次の操作を行います。

    • [データセット ID] に「vector_search」と入力します。

    • [ロケーション タイプ] で [マルチリージョン] を選択してから、[US (米国の複数のリージョン)] を選択します。

      一般公開データセットは US マルチリージョンに保存されています。わかりやすくするため、データセットを同じロケーションに保存します。

    • 残りのデフォルトの設定は変更せず、[データセットを作成] をクリックします。

テストテーブルを作成する

  1. Google 特許検索一般公開データセットのサブセットに基づいて、特許のエンベディングを含む patents テーブルを作成します。

    CREATE TABLE vector_search.patents AS
    SELECT * FROM `patents-public-data.google_patents_research.publications`
    WHERE ARRAY_LENGTH(embedding_v1) > 0
     AND publication_number NOT IN ('KR-20180122872-A')
    LIMIT 1000000;
  2. 最近傍を検索する特許のエンベディングを含む patents2 テーブルを作成します。

    CREATE TABLE vector_search.patents2 AS
    SELECT * FROM `patents-public-data.google_patents_research.publications`
    WHERE publication_number = 'KR-20180122872-A';

ベクトル インデックスを作成する

  1. patents テーブルの embeddings_v1 列に my_index ベクトル インデックスを作成します。

    CREATE OR REPLACE VECTOR INDEX my_index ON vector_search.patents(embedding_v1)
    STORING(publication_number, title)
    OPTIONS(distance_type='COSINE', index_type='IVF');
  2. ベクトル インデックスが作成されるまで数分待ってから、次のクエリを実行して、coverage_percentage 値が 100 であることを確認します。

    SELECT * FROM vector_search.INFORMATION_SCHEMA.VECTOR_INDEXES;

インデックスで VECTOR_SEARCH 関数を使用する

ベクトル インデックスを作成して入力した後で、VECTOR_SEARCH 関数を使用して、patents2 テーブルの embedding_v1 列でエンベディングの最近傍を見つけます。このクエリは検索でベクトル インデックスを使用するため、VECTOR_SEARCH近似最近傍メソッドを使用してエンベディングの最近傍を検索します。

インデックスで VECTOR_SEARCH 関数を使用します。

SELECT query.publication_number AS query_publication_number,
  query.title AS query_title,
  base.publication_number AS base_publication_number,
  base.title AS base_title,
  distance
FROM
  VECTOR_SEARCH(
    TABLE vector_search.patents,
    'embedding_v1',
    TABLE vector_search.patents2,
    top_k => 5,
    distance_type => 'COSINE',
    options => '{"fraction_lists_to_search": 0.005}');

結果は次のようになります。

+--------------------------+-------------------------------------------------------------+-------------------------+--------------------------------------------------------------------------------------------------------------------------+---------------------+
| query_publication_number |                         query_title                         | base_publication_number |                                                        base_title                                                        |      distance       |
+--------------------------+-------------------------------------------------------------+-------------------------+--------------------------------------------------------------------------------------------------------------------------+---------------------+
| KR-20180122872-A         | Rainwater management system based on rainwater keeping unit | CN-106599080-B          | A kind of rapid generation for keeping away big vast transfer figure based on GIS                                        | 0.14471956347590609 |
| KR-20180122872-A         | Rainwater management system based on rainwater keeping unit | CN-114118544-A          | Urban waterlogging detection method and device                                                                           | 0.17472108931171348 |
| KR-20180122872-A         | Rainwater management system based on rainwater keeping unit | KR-20200048143-A        | Method and system for mornitoring dry stream using unmanned aerial vehicle                                               | 0.17561990745619782 |
| KR-20180122872-A         | Rainwater management system based on rainwater keeping unit | KR-101721695-B1         | Urban Climate Impact Assessment method of Reflecting Urban Planning Scenarios and Analysis System using the same         | 0.17696129365559843 |
| KR-20180122872-A         | Rainwater management system based on rainwater keeping unit | CN-109000731-B          | The experimental rig and method that research inlet for stom water chocking-up degree influences water discharged amount | 0.17902723269642917 |
+--------------------------+-------------------------------------------------------------+-------------------------+--------------------------------------------------------------------------------------------------------------------------+---------------------+

ブルート フォースで VECTOR_SEARCH 関数を使用する

VECTOR_SEARCH 関数を使用して、patents2 テーブルの embedding_v1 列でエンベディングの最近傍を見つけます。次のクエリは検索でベクトル インデックスを使用しないため、VECTOR_SEARCH はエンベディングの正確な最近傍を検索します。

SELECT query.publication_number AS query_publication_number,
  query.title AS query_title,
  base.publication_number AS base_publication_number,
  base.title AS base_title,
  distance
FROM
  VECTOR_SEARCH(
    TABLE vector_search.patents,
    'embedding_v1',
    TABLE vector_search.patents2,
    top_k => 5,
    distance_type => 'COSINE',
    options => '{"use_brute_force":true}');

結果は次のようになります。

+--------------------------+-------------------------------------------------------------+-------------------------+--------------------------------------------------------------------------------------------------------------------------+---------------------+
| query_publication_number |                         query_title                         | base_publication_number |                                                        base_title                                                        |      distance       |
+--------------------------+-------------------------------------------------------------+-------------------------+--------------------------------------------------------------------------------------------------------------------------+---------------------+
| KR-20180122872-A         | Rainwater management system based on rainwater keeping unit | CN-106599080-B          | A kind of rapid generation for keeping away big vast transfer figure based on GIS                                        |  0.1447195634759062 |
| KR-20180122872-A         | Rainwater management system based on rainwater keeping unit | CN-114118544-A          | Urban waterlogging detection method and device                                                                           |  0.1747210893117136 |
| KR-20180122872-A         | Rainwater management system based on rainwater keeping unit | KR-20200048143-A        | Method and system for mornitoring dry stream using unmanned aerial vehicle                                               | 0.17561990745619782 |
| KR-20180122872-A         | Rainwater management system based on rainwater keeping unit | KR-101721695-B1         | Urban Climate Impact Assessment method of Reflecting Urban Planning Scenarios and Analysis System using the same         | 0.17696129365559843 |
| KR-20180122872-A         | Rainwater management system based on rainwater keeping unit | CN-109000731-B          | The experimental rig and method that research inlet for stom water chocking-up degree influences water discharged amount | 0.17902723269642928 |
+--------------------------+-------------------------------------------------------------+-------------------------+--------------------------------------------------------------------------------------------------------------------------+---------------------+

再現率を評価する

インデックスでベクトル検索を実行すると、近似結果が返されますが、再現率が低下するというトレードオフがあります。再現率を計算するには、インデックスによるベクトル検索で返された結果とブルース フォースによるベクトル検索で返された結果を比較します。このデータセットでは、publication_number 値によって特許が一意に識別されるため、この値が比較に使用されます。

WITH approx_results AS (
  SELECT query.publication_number AS query_publication_number,
    base.publication_number AS base_publication_number
  FROM
    VECTOR_SEARCH(
      TABLE vector_search.patents,
      'embedding_v1',
      TABLE vector_search.patents2,
      top_k => 5,
      distance_type => 'COSINE',
      options => '{"fraction_lists_to_search": 0.005}')
),
  exact_results AS (
  SELECT query.publication_number AS query_publication_number,
    base.publication_number AS base_publication_number
  FROM
    VECTOR_SEARCH(
      TABLE vector_search.patents,
      'embedding_v1',
      TABLE vector_search.patents2,
      top_k => 5,
      distance_type => 'COSINE',
      options => '{"use_brute_force":true}')
)

SELECT
  a.query_publication_number,
  SUM(CASE WHEN a.base_publication_number = e.base_publication_number THEN 1 ELSE 0 END) / 5 AS recall
FROM exact_results e LEFT JOIN approx_results a
  ON e.query_publication_number = a.query_publication_number
GROUP BY a.query_publication_number

再現率が予想よりも低い場合は fraction_lists_to_search 値を増やすことができますが、レイテンシとリソース使用量が高くなる可能性があります。ベクトル検索を調整するには、さまざまな引数値で VECTOR_SEARCH を複数回実行し、結果をテーブルに保存して結果を比較してみます。

クリーンアップ

  1. In the Trusted Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.