Cassandra ドキュメント

バージョン

これはプレリリース版のドキュメントです。

ベクター検索の利用

ベクターキースペースの作成

ベクター検索テーブルで使用するキースペースを作成します。この例では、keyspace name として cycling を使用しています。

CREATE KEYSPACE IF NOT EXISTS cycling
   WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : '1' };

ベクターキースペースの使用

ベクター検索テーブルで使用するキースペースを選択します。この例では、keyspace name として cycling を使用しています。

USE cycling;

ベクターテーブルの作成

ベクター用の comments_vector 列を含む、キースペースに新しいテーブルを作成します。以下のコードでは、5 つの値を持つベクターを作成します。

CREATE TABLE IF NOT EXISTS cycling.comments_vs (
  record_id timeuuid,
  id uuid,
  commenter text,
  comment text,
  comment_vector VECTOR <FLOAT, 5>,
  created_at timestamp,
  PRIMARY KEY (id, created_at)
)
WITH CLUSTERING ORDER BY (created_at DESC);

オプションで、既存のテーブルを変更してベクター列を追加できます。

ALTER TABLE cycling.comments_vs
   ADD comment_vector VECTOR <FLOAT, 5>(1)

ベクターインデックスの作成

Storage Attached Indexing (SAI) を使用してカスタムインデックスを作成します。

CREATE INDEX IF NOT EXISTS ann_index
  ON cycling.comments_vs(comment_vector) USING 'sai';

SAI の詳細については、Storage Attached Indexing ドキュメントを参照してください。

インデックスは、類似性関数を定義するオプションを使用して作成できます。

CREATE INDEX IF NOT EXISTS ann_index
    ON vsearch.com(item_vector) USING 'sai'
WITH OPTIONS = { 'similarity_function': 'DOT_PRODUCT' };

similarity_function の有効な値は、DOT_PRODUCTCOSINE、または EUCLIDEAN です。

ベクターデータをデータベースにロードする

新しい型を使用してテーブルにデータを挿入します。

INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
   VALUES (
      now(),
      e7ae5cf3-d358-4d99-b900-85902fda9bb0,
      '2017-02-14 12:43:20-0800',
      'Raining too hard should have postponed',
      'Alex',
      [0.45, 0.09, 0.01, 0.2, 0.11]
);
INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
   VALUES (
      now(),
      e7ae5cf3-d358-4d99-b900-85902fda9bb0,
      '2017-03-21 13:11:09.999-0800',
      'Second rest stop was out of water',
      'Alex',
      [0.99, 0.5, 0.99, 0.1, 0.34]
);
INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
   VALUES (
      now(),
      e7ae5cf3-d358-4d99-b900-85902fda9bb0,
      '2017-04-01 06:33:02.16-0800',
      'LATE RIDERS SHOULD NOT DELAY THE START',
      'Alex',
      [0.9, 0.54, 0.12, 0.1, 0.95]
);

INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
   VALUES (
      now(),
      c7fceba0-c141-4207-9494-a29f9809de6f,
      totimestamp(now()),
      'The gift certificate for winning was the best',
      'Amy',
      [0.13, 0.8, 0.35, 0.17, 0.03]
);

INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
   VALUES (
      now(),
      c7fceba0-c141-4207-9494-a29f9809de6f,
      '2017-02-17 12:43:20.234+0400',
      'Glad you ran the race in the rain',
      'Amy',
      [0.3, 0.34, 0.2, 0.78, 0.25]
);

INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
   VALUES (
      now(),
      c7fceba0-c141-4207-9494-a29f9809de6f,
      '2017-03-22 5:16:59.001+0400',
      'Great snacks at all reststops',
      'Amy',
      [0.1, 0.4, 0.1, 0.52, 0.09]
);
INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
   VALUES (
      now(),
      c7fceba0-c141-4207-9494-a29f9809de6f,
      '2017-04-01 17:43:08.030+0400',
      'Last climb was a killer',
      'Amy',
      [0.3, 0.75, 0.2, 0.2, 0.5]
);

CQL でベクターデータをクエリする

ベクター検索を使用してデータをクエリするには、SELECT クエリを使用します。

SELECT * FROM cycling.comments_vs
    ORDER BY comment_vector ANN OF [0.15, 0.1, 0.1, 0.35, 0.55]
    LIMIT 3;

クエリデータに最も近いスコアリングノードの類似性計算を結果の一部として取得するには、SELECT クエリを使用します。

SELECT comment, similarity_cosine(comment_vector, [0.2, 0.15, 0.3, 0.2, 0.05])
    FROM cycling.comments_vs
    ORDER BY comment_vector ANN OF [0.1, 0.15, 0.3, 0.12, 0.05]
    LIMIT 1;

このタイプのクエリでサポートされている関数は次のとおりです。

  • similarity_dot_product

  • similarity_cosine

  • similarity_euclidean

パラメータは (<vector_column>, <embedding_value>) です。両方のパラメータはベクターを表します。

  • 制限は 1,000 以下にする必要があります。

  • ベクター検索は、ほとんどの場合、正確な一致とほぼ同じくらい優れた結果をもたらす近似最近傍探索 (ANN) を利用します。スケーリングは、正確最近傍探索 (KNN) よりも優れています。

  • 最も類似性の低い検索はサポートされていません。

  • ベクター検索は、item_vector 列のオーバーライトまたは削除がないテーブルで最適に動作します。変更のある item_vector 列の場合は、検索結果が遅くなることが予想されます。